mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Improve type inference for Get-Random (#18972)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user