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 @@ -2462,6 +2462,14 @@ private void InferTypeFrom(VariableExpressionAst variableExpressionAst, List<PST
{
if (currentAst is IParameterMetadataProvider)
{
if (currentAst is ScriptBlockAst && currentAst.Parent is FunctionDefinitionAst)
{
// If this scriptblock belongs to a function we want to visit that instead so we can get the parameters
// function X ($Param1){}
currentAst = currentAst.Parent;
}

assignmentVisitor.ScopeDefinitionAst = currentAst;
currentAst.Visit(assignmentVisitor);

if (assignmentVisitor.LocalScopeOnly
Expand Down Expand Up @@ -2912,6 +2920,11 @@ private sealed class VariableAssignmentVisitor : AstVisitor2
/// Whether or not the last assignment was via command redirection.
/// </summary>
internal bool RedirectionAssignment;

/// <summary>
/// The Ast of the scope we are currently analyzing.
/// </summary>
internal Ast ScopeDefinitionAst;
internal int StopSearchOffset;
private int LastAssignmentOffset = -1;

Expand Down Expand Up @@ -3259,7 +3272,9 @@ public override AstVisitAction VisitDataStatement(DataStatementAst dataStatement

public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst)
{
return AstVisitAction.SkipChildren;
return functionDefinitionAst == ScopeDefinitionAst
? AstVisitAction.Continue
: AstVisitAction.SkipChildren;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/powershell/engine/Api/TypeInference.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ Describe "Type inference Tests" -tags "CI" {
$res.Name | Should -Be 'System.Management.ManagementObject#root\cimv2\Win32_Process'
}

It "Infers type from parameter in classic function definition" {
$res = [AstTypeInference]::InferTypeOf(({
function MyFunction ([int]$param1)
{
$param1
}
}.Ast.FindAll({param($Ast) $Ast -is [Language.VariableExpressionAst]}, $true) | Select-Object -Last 1 ))
$res.Name | Should -Be 'System.Int32'
}

It "Infers type from binary expression with a bool operator as bool" {
$res = [AstTypeInference]::InferTypeOf( {
(1..10) -contains 5
Expand Down
Loading