mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Improve type inference for $_ (#17716)
This commit is contained in:
@@ -1079,6 +1079,42 @@ Describe "Type inference Tests" -tags "CI" {
|
||||
$res.Name | Should -Be System.Exception
|
||||
}
|
||||
|
||||
It 'Infers type of variable $_ in pipeline with more than one element' {
|
||||
$memberAst = { Get-Date | New-Guid | Select-Object -Property {$_} }.Ast.Find({ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] }, $true)
|
||||
$res = [AstTypeInference]::InferTypeOf($memberAst)
|
||||
|
||||
$res | Should -HaveCount 1
|
||||
$res.Name | Should -Be System.Guid
|
||||
}
|
||||
It 'Infers type of variable $_ in array of calculated properties' {
|
||||
$variableAst = { New-TimeSpan | Select-Object -Property Day,@{n="min";e={$_}} }.Ast.Find({ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] }, $true)
|
||||
$res = [AstTypeInference]::InferTypeOf($variableAst)
|
||||
|
||||
$res | Should -HaveCount 1
|
||||
$res.Name | Should -Be System.TimeSpan
|
||||
}
|
||||
|
||||
It 'Infers type of variable $_ in switch statement' {
|
||||
$variableAst = {
|
||||
switch ("Hello","World")
|
||||
{
|
||||
'Hello'
|
||||
{
|
||||
$_
|
||||
}
|
||||
} }.Ast.Find({ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] }, $true)
|
||||
$res = [AstTypeInference]::InferTypeOf($variableAst)
|
||||
|
||||
$res | Should -HaveCount 1
|
||||
$res.Name | Should -Be System.String
|
||||
}
|
||||
|
||||
It 'Does not infer string in pipeline as char' {
|
||||
$variableAst = { "Hello" | Select-Object -Property @{n="min";e={$_}} }.Ast.Find({ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] }, $true)
|
||||
$res = [AstTypeInference]::InferTypeOf($variableAst)
|
||||
$res.Name | Should -Be System.String
|
||||
}
|
||||
|
||||
$catchClauseTypes = @(
|
||||
@{ Type = 'System.ArgumentException' }
|
||||
@{ Type = 'System.ArgumentNullException' }
|
||||
@@ -1147,6 +1183,55 @@ Describe "Type inference Tests" -tags "CI" {
|
||||
$res[1].Name | Should -Be System.Exception
|
||||
}
|
||||
|
||||
It 'falls back to a generic ErrorRecord if catch exception type is invalid' {
|
||||
$VariableAst = {
|
||||
try {}
|
||||
catch [ThisTypeDoesNotExist] { $_ }
|
||||
}.Ast.Find(
|
||||
{ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] },
|
||||
$true
|
||||
)
|
||||
$res = [AstTypeInference]::InferTypeOf($VariableAst)
|
||||
|
||||
$res.Name | Should -Be System.Management.Automation.ErrorRecord
|
||||
}
|
||||
|
||||
It 'Infers type of trap statement' {
|
||||
$VariableAst = {
|
||||
trap { $_ }
|
||||
}.Ast.Find(
|
||||
{ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] },
|
||||
$true
|
||||
)
|
||||
$res = [AstTypeInference]::InferTypeOf($VariableAst)
|
||||
|
||||
$res.Name | Should -Be System.Management.Automation.ErrorRecord
|
||||
}
|
||||
|
||||
It 'Infers type of exception in typed trap statement' {
|
||||
$memberAst = {
|
||||
trap [System.DivideByZeroException] { $_.Exception }
|
||||
}.Ast.Find(
|
||||
{ param($a) $a -is [System.Management.Automation.Language.MemberExpressionAst] },
|
||||
$true
|
||||
)
|
||||
$res = [AstTypeInference]::InferTypeOf($memberAst)
|
||||
|
||||
$res.Name | Should -Be System.DivideByZeroException
|
||||
}
|
||||
|
||||
It 'falls back to a generic ErrorRecord if trap exception type is invalid' {
|
||||
$VariableAst = {
|
||||
trap [ThisTypeDoesNotExist] { $_ }
|
||||
}.Ast.Find(
|
||||
{ param($a) $a -is [System.Management.Automation.Language.VariableExpressionAst] },
|
||||
$true
|
||||
)
|
||||
$res = [AstTypeInference]::InferTypeOf($VariableAst)
|
||||
|
||||
$res.Name | Should -Be System.Management.Automation.ErrorRecord
|
||||
}
|
||||
|
||||
It 'Infers type of function member' {
|
||||
$res = [AstTypeInference]::InferTypeOf( {
|
||||
class X {
|
||||
|
||||
Reference in New Issue
Block a user