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 @@ -1241,7 +1241,18 @@ object ICustomAstVisitor.VisitDoUntilStatement(DoUntilStatementAst doUntilStatem

object ICustomAstVisitor.VisitAssignmentStatement(AssignmentStatementAst assignmentStatementAst)
{
return assignmentStatementAst.Left.Accept(this);
ExpressionAst child = assignmentStatementAst.Left;
while (child is AttributedExpressionAst attributeChild)
{
if (attributeChild is ConvertExpressionAst convert)
{
return new List<PSTypeName>() { new(convert.Type.TypeName) };
}

child = attributeChild.Child;
}

return assignmentStatementAst.Right.Accept(this);
}

object ICustomAstVisitor.VisitPipeline(PipelineAst pipelineAst)
Expand Down
20 changes: 20 additions & 0 deletions test/powershell/engine/Api/TypeInference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,26 @@ Describe "Type inference Tests" -tags "CI" {
$res.Count | Should -Be 0
}

It 'Infers right side of assignment expression' {
$res = [AstTypeInference]::InferTypeOf( { $Test1 = "Hello" }.Ast.Find({param($ast) $ast -is [Language.AssignmentStatementAst]}, $true))
$res.Count | Should -Be 1
$res.Name | Should -Be "System.String"
}

It 'Infers left side of assignment expression when it is a ConvertExpression' {
$res = [AstTypeInference]::InferTypeOf( { [string]$Test1 = 42 }.Ast.Find({param($ast) $ast -is [Language.AssignmentStatementAst]}, $true))
$res.Count | Should -Be 1
$res.Name | Should -Be "System.String"
}

It 'Infers left side of assignment expression when there is a ConvertExpression among other attributes' {
$res = [AstTypeInference]::InferTypeOf( {
[ValidateLength()] [string] [ValidatePattern()]$Test1 = 42
}.Ast.Find({param($ast) $ast -is [Language.AssignmentStatementAst]}, $true))
$res.Count | Should -Be 1
$res.Name | Should -Be "System.String"
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see tests cases from the PR description:

This will fix the type inference for multiple variable assignments like: $Test1 = [string]$Test2 = $Test3 = $Test4 = 10 and also variables assigned with parentheses: $Test1 = ($Test2 = ls)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because the examples from the description are practical examples of where the effect of this change can be felt. However, since the API lets us specify the exact Ast we want to infer, there is no need for the practical examples in the tests as we can make them more specific.

It 'Infers type of all scope variable after variable assignment' {
$res = [AstTypeInference]::InferTypeOf( { $true = "Hello";$true }.Ast)
$res.Count | Should -Be 1
Expand Down
Loading