Skip to content

Commit 86c2c88

Browse files
author
James Brundage
committed
VariableExpressionAst.GetVariableType - Enabling InvokeMemberExpression (Fixes #490)
1 parent 1e6eda9 commit 86c2c88

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

Types/VariableExpressionAST/GetVariableType.ps1

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313
$y = 2
1414
$x + $y
1515
}.Ast.EndBlock.Statements[-1].PipelineElements[0].Expression.Left.GetVariableType()
16+
# Should -Be ([int])
1617
.EXAMPLE
1718
{
1819
$x = Get-Process
1920
$x + $y
2021
}.Ast.EndBlock.Statements[-1].PipelineElements[0].Expression.Left.GetVariableType()
22+
# Should -Be ([Diagnostics.Process])
23+
.EXAMPLE
24+
{
25+
$x = [type].name
26+
$x
27+
}.Ast.EndBlock.Statements[-1].PipelineElements[0].Expression.GetVariableType()
28+
2129
#>
2230
if ($this.VariablePath.userPath -eq 'psBoundParmeters') {
2331
return [Management.Automation.PSBoundParametersDictionary]
@@ -59,7 +67,7 @@ $isMultiAssignment =$closestAssignment.Left -is [Management.Automation.Language.
5967

6068
# If the left side is not multiple assignment, but the right side is an array
6169
if (-not $isMultiAssignment -and
62-
$closestAssignment.Right.Expression -is [Management.Automation.ArrayExpressionAst]) {
70+
$closestAssignment.Right.Expression -is [Management.Automation.Language.ArrayExpressionAst]) {
6371
# then the object is an array.
6472
return [Object[]]
6573
}
@@ -83,7 +91,24 @@ if ($closestAssignment.Right.Expression -is [Management.Automation.Language.Conv
8391
}
8492
}
8593

86-
94+
if ($closestAssignment.Right.Expression -is [Management.Automation.Language.MemberExpressionAst]) {
95+
$invokeMemberExpr = $closestAssignment.Right.Expression
96+
$memberName = $invokeMemberExpr.Member.ToString()
97+
if ($invokeMemberExpr.Expression.TypeName) {
98+
$invokeType = $invokeMemberExpr.Expression.TypeName.GetReflectionType()
99+
if ($invokeType) {
100+
$potentialTypes = @(
101+
foreach ($invokeableMember in $invokeType.GetMember($memberName, "Public, IgnoreCase,$(if ($invokeMemberExpr.Static) { 'Static'} else { 'Instance'})")) {
102+
if ($invokeableMember.PropertyType) {
103+
$invokeableMember.PropertyType
104+
} elseif ($invokeableMember.ReturnType) {
105+
$invokeableMember.ReturnType
106+
}
107+
})
108+
return $potentialTypes | Select-Object -Unique
109+
}
110+
}
111+
}
87112

88113

89114
# The right side could be a pipeline

0 commit comments

Comments
 (0)