Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1048,10 +1048,25 @@ private void InferTypesFrom(CommandAst commandAst, List<PSTypeName> inferredType
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);
Expand Down
18 changes: 18 additions & 0 deletions test/powershell/engine/Api/TypeInference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down