@@ -3,42 +3,66 @@ 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
7
param (
7
8
# 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
11
19
)
12
20
process {
13
- Write-Debug " ENTER: ConvertToAst $ ( ( $ Code -split " [\r\n]" )[0 ]) "
21
+ Write-Debug " ENTER: ConvertToAst $Path $ ( ( " $Command$ Code" -split " [\r\n]" )[0 ]) "
14
22
$ParseErrors = $null
15
23
$Tokens = $null
16
24
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
23
28
try {
24
- [ string []] $Files = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath ($Code , [ref ]$Provider )
29
+ $Path = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath ($Path , [ref ]$Provider )[ 0 ]
25
30
} 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 ) "
27
45
}
46
+ $AST = [System.Management.Automation.Language.Parser ]::ParseInput($Command.Definition , $Path , [ref ]$Tokens , [ref ]$ParseErrors )
28
47
}
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 )
35
57
}
36
58
}
37
- Write-Debug " EXIT: ConvertToAst"
59
+
60
+ Write-Debug " EXIT: ConvertToAst $Path "
38
61
[PSCustomObject ]@ {
39
62
PSTypeName = " PoshCode.ModuleBuilder.ParseResults"
40
63
ParseErrors = $ParseErrors
41
64
Tokens = $Tokens
65
+ Path = $Path
42
66
AST = $AST
43
67
}
44
68
}
0 commit comments