Improve type inference for Get-Random (#18972)

This commit is contained in:
MartinGC94
2023-03-13 15:44:38 -07:00
committed by GitHub
parent e95f7fb50f
commit 0ae521b7b5
2 changed files with 34 additions and 1 deletions
@@ -1059,10 +1059,25 @@ namespace System.Management.Automation
inferredTypes.AddRange(inferTypesFromObjectCmdlets);
return;
}
if (cmdletInfo.ImplementingType.FullName.EqualsOrdinalIgnoreCase("Microsoft.PowerShell.Commands.GetRandomCommand")
&& pseudoBinding.BoundArguments.TryGetValue("InputObject", out var value))
{
if (value.ParameterArgumentType == AstParameterArgumentType.PipeObject)
{
InferTypesFromPreviousCommand(commandAst, inferredTypes);
}
else if (value.ParameterArgumentType == AstParameterArgumentType.AstPair)
{
inferredTypes.AddRange(InferTypes(((AstPair)value).Argument));
}
return;
}
}
// The OutputType property ignores the parameter set specified in the OutputTypeAttribute.
// With psuedo-binding, we actually know the candidate parameter sets, so we could take
// With pseudo-binding, we actually know the candidate parameter sets, so we could take
// advantage of it here, but I opted for the simpler code because so few cmdlets use
// ParameterSetName in OutputType and of the ones I know about, it isn't that useful.
inferredTypes.AddRange(commandInfo.OutputType);
@@ -524,6 +524,24 @@ Describe "Type inference Tests" -tags "CI" {
$res.Name | Should -Be "System.String"
}
It "Infers typeof Get-Random with pipeline input" {
$res = [AstTypeInference]::InferTypeOf( { "Hello","World" | Get-Random }.Ast)
$res.Count | Should -Be 1
$res.Name | Should -Be "System.String"
}
It "Infers typeof Get-Random with astpair input" {
$res = [AstTypeInference]::InferTypeOf( { Get-Random -InputObject Hello,World }.Ast)
$res.Count | Should -Be 1
$res.Name | Should -Be "System.String[]"
}
It "Infers typeof Get-Random with no input" {
$res = [AstTypeInference]::InferTypeOf( { Get-Random }.Ast)
$res.Count | Should -Be 3
$res.Name -join ', ' | Should -Be "System.Int32, System.Int64, System.Double"
}
It "Infers typeof Group-Object Group" {
$res = [AstTypeInference]::InferTypeOf( { Get-ChildItem | Group-Object | ForEach-Object Group }.Ast)
$res.Count | Should -Be 3