@@ -3,10 +3,10 @@ function ConvertToAst {
33 . SYNOPSIS
44 Parses the given code and returns an object with the AST, Tokens and ParseErrors
55 #>
6- [CmdletBinding (DefaultParameterSetName = " Path" )]
6+ [CmdletBinding (DefaultParameterSetName = " Path" )]
77 param (
88 # The script content, or script or module file path to parse
9- [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = " Code" )]
9+ [Parameter (Mandatory , ValueFromPipelineByPropertyName , ParameterSetName = " Code" )]
1010 [Alias (" ScriptBlock" )]
1111 $Code ,
1212
@@ -49,11 +49,26 @@ function ConvertToAst {
4949 Write-Debug " Parse Code as ScriptBlock"
5050 if ($Code -is [System.Management.Automation.ScriptBlock ]) {
5151 $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 )
57+ } else {
58+ $Provider = $null
59+ try {
60+ $Path = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath ($Code , [ref ]$Provider )[0 ]
61+ } catch {
62+ Write-Debug (" Failed to resolve Code as Path " + $_.Exception.Message )
63+ }
64+ if ($Provider.Name -eq " FileSystem" ) {
65+ Write-Debug " Parse File Path: $Path "
66+ $AST = [System.Management.Automation.Language.Parser ]::ParseFile($Path , [ref ]$Tokens , [ref ]$ParseErrors )
67+ } else {
68+ Write-Debug " Parse Code $ ( ($Path -split " [\r\n]" )[0 ]) "
69+ $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Code , [ref ]$Tokens , [ref ]$ParseErrors )
70+ }
5271 }
53- if (! $Path ) {
54- $Path = " scriptblock"
55- }
56- $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Code , $Path , [ref ]$Tokens , [ref ]$ParseErrors )
5772 }
5873 }
5974
0 commit comments