@@ -3,42 +3,66 @@ function ConvertToAst {
33 . SYNOPSIS
44 Parses the given code and returns an object with the AST, Tokens and ParseErrors
55 #>
6+ [CmdletBinding (DefaultParameterSetName = " Path" )]
67 param (
78 # The script content, or script or module file path to parse
8- [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName )]
9- [Alias (" Path" , " PSPath" , " Definition" , " ScriptBlock" , " Module" )]
10- $Code
9+ [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = " Code" )]
10+ [Alias (" ScriptBlock" )]
11+ $Code ,
12+
13+ [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = " Command" , Position = 0 )]
14+ [System.Management.Automation.FunctionInfo ]$Command ,
15+
16+ [Parameter (ValueFromPipelineByPropertyName , Position = 1 )]
17+ [Alias (" PSPath" , " File" , " Definition" )]
18+ [string ]$Path
1119 )
1220 process {
13- Write-Debug " ENTER: ConvertToAst $ ( ( $ Code -split " [\r\n]" )[0 ]) "
21+ Write-Debug " ENTER: ConvertToAst $Path $ ( ( " $Command$ Code" -split " [\r\n]" )[0 ]) "
1422 $ParseErrors = $null
1523 $Tokens = $null
1624
17- if ($Code -is [System.Management.Automation.FunctionInfo ]) {
18- Write-Debug " Parse Code as Function"
19- $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Code.Definition , " function:$ ( $Code.Name ) " , [ref ]$Tokens , [ref ]$ParseErrors )
20- } else {
21- $Provider = $null
22- if (@ ($Code ).Count -eq 1 -and $Code -notmatch " \n" ) {
25+ switch ($PSCmdlet.ParameterSetName ) {
26+ " Path" {
27+ $Provider = $null
2328 try {
24- [ string []] $Files = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath ($Code , [ref ]$Provider )
29+ $Path = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath ($Path , [ref ]$Provider )[ 0 ]
2530 } catch {
26- Write-Debug (" Exception resolving Code as Path " + $_.Exception.Message )
31+ Write-Debug (" Exception resolving $Path as Path " + $_.Exception.Message )
32+ }
33+ if ($Provider.Name -eq " FileSystem" ) {
34+ Write-Debug " Parse File Path: $Path "
35+ $AST = [System.Management.Automation.Language.Parser ]::ParseFile($Path , [ref ]$Tokens , [ref ]$ParseErrors )
36+ } else {
37+ Write-Debug " Parse Code $ ( ($Path -split " [\r\n]" )[0 ]) "
38+ $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Path , [ref ]$Tokens , [ref ]$ParseErrors )
39+ }
40+ }
41+ " Command" {
42+ Write-Debug " Parse Function"
43+ if (! $Path ) {
44+ $Path = " function:$ ( $Command.Name ) "
2745 }
46+ $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Command.Definition , $Path , [ref ]$Tokens , [ref ]$ParseErrors )
2847 }
29- if ($Provider.Name -eq " FileSystem" -and $Files.Count -gt 0 ) {
30- Write-Debug " Parse Code as File Path"
31- $AST = [System.Management.Automation.Language.Parser ]::ParseFile(($Files [0 ] | Convert-Path ), [ref ]$Tokens , [ref ]$ParseErrors )
32- } else {
33- Write-Debug " Parse Code as String"
34- $AST = [System.Management.Automation.Language.Parser ]::ParseInput([String ]$Code , [ref ]$Tokens , [ref ]$ParseErrors )
48+ " Code" {
49+ Write-Debug " Parse Code as ScriptBlock"
50+ if ($Code -is [System.Management.Automation.ScriptBlock ]) {
51+ $Code = $Code.GetNewClosure ().Invoke().ToString()
52+ }
53+ if (! $Path ) {
54+ $Path = " scriptblock"
55+ }
56+ $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Code , $Path , [ref ]$Tokens , [ref ]$ParseErrors )
3557 }
3658 }
37- Write-Debug " EXIT: ConvertToAst"
59+
60+ Write-Debug " EXIT: ConvertToAst $Path "
3861 [PSCustomObject ]@ {
3962 PSTypeName = " PoshCode.ModuleBuilder.ParseResults"
4063 ParseErrors = $ParseErrors
4164 Tokens = $Tokens
65+ Path = $Path
4266 AST = $AST
4367 }
4468 }
0 commit comments