Skip to content

Commit

Permalink
enhancement: Extending Microsoft.CodeAnalysis.SyntaxTree (Fixes #549)
Browse files Browse the repository at this point in the history
  • Loading branch information
StartAutomating authored and StartAutomating committed Nov 5, 2023
1 parent b5b0fbb commit d6d13c8
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
System.Management.Automation.Language.VariableExpressionAst.GetVariableType()
-----------------------------------------------------------------------------




### Synopsis
Gets a Variable's Likely Type



---


### Description

Determines the type of a variable.

This looks for the closest assignment statement and uses this to determine what type the variable is likely to be.



---


### Examples
> EXAMPLE 1
```PowerShell
{
[int]$x = 1
$y = 2
$x + $y
}.Ast.EndBlock.Statements[-1].PipelineElements[0].Expression.Left.GetVariableType()
# Should -Be ([int])
```
> EXAMPLE 2
```PowerShell
{
$x = Get-Process
$x + $y
}.Ast.EndBlock.Statements[-1].PipelineElements[0].Expression.Left.GetVariableType()
# Should -Be ([Diagnostics.Process])
```
> EXAMPLE 3
```PowerShell
{
$x = [type].name
$x
}.Ast.EndBlock.Statements[-1].PipelineElements[0].Expression.GetVariableType()
```


---


### Notes
Subject to revision and improvement. While this covers many potential scenarios, it does not always



---

0 comments on commit d6d13c8

Please sign in to comment.