Skip to content

Commit 62e792f

Browse files
author
James Brundage
committed
feat: [Ast].ByType (Fixes #639)
1 parent 308c967 commit 62e792f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Types/AST/get_ByType.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<#
2+
.SYNOPSIS
3+
Gets PowerShell AST Nodes by type
4+
.DESCRIPTION
5+
Gets a dictionary of all nodes in a PowerShell AST beneath this point, grouped by type.
6+
.EXAMPLE
7+
{"hello world"}.Ast.ByType
8+
#>
9+
if (-not $this.'.ByType') {
10+
$ByType = [Collections.Generic.Dictionary[Type,Collections.Generic.List[PSObject]]]::new()
11+
foreach ($node in $this.FindAll({$true}, $true)) {
12+
$nodeType = $node.GetType()
13+
14+
if (-not $ByType[$nodeType]) {
15+
$ByType[$nodeType] = [Collections.Generic.List[PSObject]]::new()
16+
}
17+
$ByType[$nodeType].Add($node)
18+
}
19+
Add-Member -InputObject $this -MemberType NoteProperty -Name '.ByType' -Value $ByType -Force
20+
}
21+
22+
$this.'.ByType'

0 commit comments

Comments
 (0)