@@ -18,11 +18,12 @@ function Search-PipeScript {
1818 [OutputType (' Search.PipeScript.Result' )]
1919 [Alias (' Search-ScriptBlock' )]
2020 param (
21- # The ScriptBlock that will be searched.
21+ # The input to search.
22+ # This can be a `[string]`, `[ScriptBlock]`, `[IO.FileInfo]`, or `[Ast]`.
2223 [Parameter (ValueFromPipeline , ValueFromPipelineByPropertyName )]
2324 [Alias (' ScriptBlock' , ' Text' )]
2425 [ValidateScript ({
25- $validTypeList = [System.String ], [System.Management.Automation.ScriptBlock ], [System.IO.FileInfo ]
26+ $validTypeList = [System.String ], [System.Management.Automation.ScriptBlock ], [System.IO.FileInfo ], [ System.Management.Automation.Language.Ast ]
2627 $thisType = $_.GetType ()
2728 $IsTypeOk =
2829 $ (@ ( foreach ($validType in $validTypeList ) {
@@ -31,14 +32,14 @@ function Search-PipeScript {
3132 }
3233 }))
3334 if (-not $isTypeOk ) {
34- throw " Unexpected type '$ ( @ ($thisType )[0 ]) '. Must be 'string','scriptblock','System.IO.FileInfo'."
35+ throw " Unexpected type '$ ( @ ($thisType )[0 ]) '. Must be 'string','scriptblock','System.IO.FileInfo','System.Management.Automation.Language.Ast' ."
3536 }
3637 return $true
3738 })]
3839
3940 $InputObject ,
4041 # The AST Condition.
41- # These Script Blocks
42+ # These conditions will apply when the input is a `[ScriptBlock]`, `[Ast]`, or PowerShell file.
4243 [Parameter (ValueFromPipelineByPropertyName )]
4344 [Alias (' ASTDelegate' )]
4445 [ScriptBlock []]
@@ -115,19 +116,26 @@ function Search-PipeScript {
115116 # Read the file contents as text.
116117 $text = [IO.File ]::ReadAllText($inputCommand.Source )
117118 }
118- }
119+ }
120+ $ast = $null
119121 # If the inputObject was a [ScriptBlock]
120122 if ($InputObject -is [scriptblock ]) {
121123 $scriptBlock = $InputObject # set $ScriptBlock
122- }
124+ $ast = $scriptBlock.Ast # and set $ast to the $ScriptBlock`s AST.
125+ # Reset $text to the ScriptBlock contents.
126+ $Text = " $scriptBlock "
127+ } elseif ($InputObject -is [Management.Automation.Language.Ast ]) {
128+ # If the input was an [AST]
129+ # set the AST.
130+ $ast = $InputObject
131+ $text = $ast.Extent.ToString ()
132+ }
123133 # If the InputObject is a string
124134 if ($InputObject -is [string ]) {
125135 $Text = $InputObject # set $Text.
126136 }
127137 # If we have a ScriptBlock
128- if ($scriptBlock ) {
129- # Reset $text to the ScriptBlock contents.
130- $Text = " $scriptBlock "
138+ if ($ast ) {
131139 # If we have an ASTType to find
132140 if ($AstType ) {
133141 foreach ($astTypeName in $AstType ) {
@@ -180,7 +188,7 @@ function Search-PipeScript {
180188 if ($AstCondition ) {
181189 foreach ($condition in $AstCondition ) {
182190 # Find all of the results.
183- $ScriptBlock . Ast.FindAll ($condition , ($Recurse -as [bool ])) |
191+ $Ast.FindAll ($condition , ($Recurse -as [bool ])) |
184192 & { process {
185193 $in = $this = $_
186194 [PSCustomObject ][Ordered ]@ {
0 commit comments