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 @@ -584,6 +584,20 @@ object ICustomAstVisitor.VisitHashtable(HashtableAst hashtableAst)
if (hashtableAst.KeyValuePairs.Count > 0)
{
var properties = new List<PSMemberNameAndType>();
void AddInferredTypes(Ast ast, string keyName)
{
bool foundAnyTypes = false;
foreach (PSTypeName item in InferTypes(ast))
{
foundAnyTypes = true;
properties.Add(new PSMemberNameAndType(keyName, item));
}

if (!foundAnyTypes)
{
properties.Add(new PSMemberNameAndType(keyName, new PSTypeName("System.Object")));
}
}

foreach (var kv in hashtableAst.KeyValuePairs)
{
Expand Down Expand Up @@ -615,31 +629,18 @@ object ICustomAstVisitor.VisitHashtable(HashtableAst hashtableAst)
_ = SafeExprEvaluator.TrySafeEval(expression, _context.ExecutionContext, out value);
}

PSTypeName valueType;
if (value is null)
{
valueType = new PSTypeName("System.Object");
}
else
{
valueType = new PSTypeName(value.GetType());
AddInferredTypes(expression, name);
continue;
}

PSTypeName valueType = new(value.GetType());
properties.Add(new PSMemberNameAndType(name, valueType, value));
}
else
{
bool foundAnyTypes = false;
foreach (var item in InferTypes(kv.Item2))
{
foundAnyTypes = true;
properties.Add(new PSMemberNameAndType(name, item));
}

if (!foundAnyTypes)
{
properties.Add(new PSMemberNameAndType(name, new PSTypeName("System.Object")));
}
AddInferredTypes(kv.Item2, name);
}
}
}
Expand Down Expand Up @@ -1639,7 +1640,7 @@ private IEnumerable<PSTypeName> InferTypesFrom(MemberExpressionAst memberExpress
var memberNameList = new List<string> { memberAsStringConst.Value };
foreach (var type in exprType)
{
if (type.Type == typeof(PSObject))
if (type.Type == typeof(PSObject) && type is not PSSyntheticTypeName)
{
continue;
}
Expand Down
12 changes: 12 additions & 0 deletions test/powershell/engine/Api/TypeInference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1379,12 +1379,24 @@ Describe "Type inference Tests" -tags "CI" {
$res.Name -join ' ' | Should -Be "System.IO.FileInfo System.IO.DirectoryInfo"
}

It 'Falls back to type inference for hashtable assignments with pure expression with no value' {
$res = [AstTypeInference]::InferTypeOf( {$KeyWithNoValue = Get-ChildItem $HOME; (@{RandomKey = $KeyWithNoValue}).RandomKey }.Ast)
$Res.Count | Should -Be 2
$res.Name -join ' ' | Should -Be "System.IO.FileInfo System.IO.DirectoryInfo"
}

It 'Infers type of index expression on hashtable with synthetic type' {
$res = [AstTypeInference]::InferTypeOf( { (@{RandomKey = Get-ChildItem $HOME})['RandomKey'] }.Ast)
$res.Count | Should -Be 2
$res.Name -join ' ' | Should -Be "System.IO.FileInfo System.IO.DirectoryInfo"
}

It 'Infers type of member expression on a custom object' {
$res = [AstTypeInference]::InferTypeOf( { ([pscustomobject]@{RandomProp1 = Get-ChildItem $HOME}).RandomProp1 }.Ast)
$res.Count | Should -Be 2
$res.Name -join ' ' | Should -Be "System.IO.FileInfo System.IO.DirectoryInfo"
}

It 'Infers closest variable type' {
$res = [AstTypeInference]::InferTypeOf( { [string]$TestVar = "";[hashtable]$TestVar = @{};$TestVar }.Ast)
$res.Name | Select-Object -Last 1 | Should -Be "System.Collections.Hashtable"
Expand Down