Exclude outvariable assignments within the same CommandAst when inferring variables (#25224)

This commit is contained in:
MartinGC94
2025-03-26 18:00:46 +00:00
committed by GitHub
parent c1241ede43
commit 0c96975d1e
2 changed files with 20 additions and 11 deletions
@@ -1197,6 +1197,12 @@ Describe "Type inference Tests" -tags "CI" {
$res.Name | Should -Be System.String
}
It 'Ignores assignment when a variable is declared and used within the same commandAst' {
$variableAst = { Get-Random 2>variable:RandomError1 -InputObject ($RandomError1) }.Ast.FindAll({ param($a) $a -is [Language.VariableExpressionAst] }, $true) | select -Last 1
$res = [AstTypeInference]::InferTypeOf($variableAst)
$res.Count | Should -Be 0
}
It 'Ignores the last assignment when a variable is reused' {
$variableAst = { $x = New-Guid; $x = $x.Where{$_} }.Ast.FindAll({ param($a) $a -is [Language.VariableExpressionAst] }, $true) | select -Last 1
$res = [AstTypeInference]::InferTypeOf($variableAst)