Add type inference for functions without OutputType attribute and anonymous functions (#21127)

This commit is contained in:
MartinGC94 authored and GitHub committed 2025-02-28 17:48:59 +00:00
1 parent 2c769d65e3
commit 33ed509ca4
2 files changed
+39 -2

No files matched your search

@@ -650,8 +650,10 @@ Describe "Type inference Tests" -tags "CI" {
}
It "Infers type from variable with AllowSafeEval" {
function Hide-GetProcess { Get-Process }
$p = Hide-GetProcess
# Invoke-Expression is used to "hide" Get-Process from the type inference.
# If the typeinference code is updated to handle Invoke-Expression, this test will need to find some other way to set $p
# so that the type inference can't figure it out without evaluating the variable value
$p = Invoke-Expression -Command 'Get-Process'
$res = [AstTypeInference]::InferTypeOf( { $p }.Ast, [TypeInferenceRuntimePermissions]::AllowSafeEval)
$res.Name | Should -Be 'System.Diagnostics.Process'
}
@@ -1492,6 +1494,17 @@ Describe "Type inference Tests" -tags "CI" {
$null = [AstTypeInference]::InferTypeOf($FoundAst)
}
It 'Should infer output from anonymous function' {
$res = [AstTypeInference]::InferTypeOf( { & {"Hello"} }.Ast)
$res.Name | Should -Be 'System.String'
}
It 'Should infer output from function without OutputType attribute' {
function MyHello{"Hello"}
$res = [AstTypeInference]::InferTypeOf( { MyHello }.Ast)
$res.Name | Should -Be 'System.String'
}
It 'Infers type of command with all streams redirected to Success stream' {
$res = [AstTypeInference]::InferTypeOf( { Get-PSDrive *>&1 }.Ast)
$ExpectedTypeNames = @(