@@ -3,10 +3,10 @@ function ConvertToAst {
3
3
. SYNOPSIS
4
4
Parses the given code and returns an object with the AST, Tokens and ParseErrors
5
5
#>
6
- [CmdletBinding (DefaultParameterSetName = " Path" )]
6
+ [CmdletBinding (DefaultParameterSetName = " Path" )]
7
7
param (
8
8
# The script content, or script or module file path to parse
9
- [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName , ParameterSetName = " Code" )]
9
+ [Parameter (Mandatory , ValueFromPipelineByPropertyName , ParameterSetName = " Code" )]
10
10
[Alias (" ScriptBlock" )]
11
11
$Code ,
12
12
@@ -49,11 +49,26 @@ function ConvertToAst {
49
49
Write-Debug " Parse Code as ScriptBlock"
50
50
if ($Code -is [System.Management.Automation.ScriptBlock ]) {
51
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 )
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
+ }
52
71
}
53
- if (! $Path ) {
54
- $Path = " scriptblock"
55
- }
56
- $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Code , $Path , [ref ]$Tokens , [ref ]$ParseErrors )
57
72
}
58
73
}
59
74
0 commit comments