Fix the regression when doing type inference for $_ (#21223)

This commit is contained in:
MartinGC94
2024-02-26 15:11:19 -08:00
committed by GitHub
parent 05ed2f270c
commit 338d1dcfa1
2 changed files with 17 additions and 1 deletions
@@ -1983,8 +1983,19 @@ namespace System.Management.Automation
{
if (switchErrorStatement.Conditions?.Count > 0)
{
parent = switchErrorStatement.Conditions[0];
if (switchErrorStatement.Conditions[0].Extent.EndOffset < variableExpressionAst.Extent.StartOffset)
{
parent = switchErrorStatement.Conditions[0];
break;
}
else
{
// $_ is inside the condition that is being declared, eg: Get-Process | Sort-Object -Property {switch ($_.Proc<Tab>
parent = switchErrorStatement.Parent;
continue;
}
}
break;
}
else if (parent is ScriptBlockExpressionAst)
@@ -1860,6 +1860,11 @@ class InheritedClassTest : System.Attribute
$res.CompletionMatches[0].CompletionText | Should -BeExactly 'Black' # 0 = NonPositive
}
It 'Tab completion of $_ inside incomplete switch condition' {
$res = TabExpansion2 -inputScript 'Get-PSDrive | Sort-Object -Property {switch ($_.nam'
$res.CompletionMatches[0].CompletionText | Should -Be 'Name'
}
It "Test [CommandCompletion]::GetNextResult" {
$inputStr = "Get-Command -Type Alias,c"
$res = TabExpansion2 -inputScript $inputStr -cursorColumn $inputStr.Length