|
| 1 | +using namespace System.Management.Automation.Language |
| 2 | +using namespace System.Collections.Generic |
| 3 | + |
| 4 | +# This is used only to parse the parameters to New|Set|Remove-Alias |
| 5 | +# NOTE: this is _part of_ the implementation of AliasVisitor, but ... |
| 6 | +# PowerShell can't handle nested classes so I left it outside, |
| 7 | +# but I kept it here in this file. |
| 8 | +class AliasParameterVisitor : AstVisitor { |
| 9 | + [string]$Parameter = $null |
| 10 | + [string]$Command = $null |
| 11 | + [string]$Name = $null |
| 12 | + [string]$Value = $null |
| 13 | + [string]$Scope = $null |
| 14 | + |
| 15 | + # Parameter Names |
| 16 | + [AstVisitAction] VisitCommandParameter([CommandParameterAst]$ast) { |
| 17 | + $this.Parameter = $ast.ParameterName |
| 18 | + return [AstVisitAction]::Continue |
| 19 | + } |
| 20 | + |
| 21 | + # Parameter Values |
| 22 | + [AstVisitAction] VisitStringConstantExpression([StringConstantExpressionAst]$ast) { |
| 23 | + # The FIRST command element is always the command name |
| 24 | + if (!$this.Command) { |
| 25 | + $this.Command = $ast.Value |
| 26 | + return [AstVisitAction]::Continue |
| 27 | + } else { |
| 28 | + # Nobody should use minimal parameters like -N for -Name ... |
| 29 | + # But if they do, our parser works anyway! |
| 30 | + switch -Wildcard ($this.Parameter) { |
| 31 | + "S*" { |
| 32 | + $this.Scope = $ast.Value |
| 33 | + } |
| 34 | + "N*" { |
| 35 | + $this.Name = $ast.Value |
| 36 | + } |
| 37 | + "Va*" { |
| 38 | + $this.Value = $ast.Value |
| 39 | + } |
| 40 | + "F*" { |
| 41 | + if ($ast.Value) { |
| 42 | + # Force parameter was passed as named parameter with a positional parameter after it which is alias name |
| 43 | + $this.Name = $ast.Value |
| 44 | + } |
| 45 | + } |
| 46 | + default { |
| 47 | + if (!$this.Parameter) { |
| 48 | + # For bare arguments, the order is Name, Value: |
| 49 | + if (!$this.Name) { |
| 50 | + $this.Name = $ast.Value |
| 51 | + } else { |
| 52 | + $this.Value = $ast.Value |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + $this.Parameter = $null |
| 59 | + |
| 60 | + # If we have enough information, stop the visit |
| 61 | + # For -Scope global or Remove-Alias, we don't want to export these |
| 62 | + if ($this.Name -and $this.Command -eq "Remove-Alias") { |
| 63 | + $this.Command = "Remove-Alias" |
| 64 | + return [AstVisitAction]::StopVisit |
| 65 | + } elseif ($this.Name -and $this.Scope -eq "Global") { |
| 66 | + return [AstVisitAction]::StopVisit |
| 67 | + } |
| 68 | + return [AstVisitAction]::Continue |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [AliasParameterVisitor] Clear() { |
| 73 | + $this.Command = $null |
| 74 | + $this.Parameter = $null |
| 75 | + $this.Name = $null |
| 76 | + $this.Value = $null |
| 77 | + $this.Scope = $null |
| 78 | + return $this |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +# This visits everything at the top level of the script |
| 83 | +class AliasVisitor : AstVisitor { |
| 84 | + [HashSet[String]]$Aliases = @() |
| 85 | + [AliasParameterVisitor]$Parameters = @{} |
| 86 | + |
| 87 | + # The [Alias(...)] attribute on functions matters, but we can't export aliases that are defined inside a function |
| 88 | + [AstVisitAction] VisitFunctionDefinition([FunctionDefinitionAst]$ast) { |
| 89 | + @($ast.Body.ParamBlock.Attributes.Where{ |
| 90 | + $_.TypeName.Name -eq "Alias" |
| 91 | + }.PositionalArguments.Value).ForEach{ |
| 92 | + if ($_) { |
| 93 | + $this.Aliases.Add($_) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return [AstVisitAction]::SkipChildren |
| 98 | + } |
| 99 | + |
| 100 | + # Top-level commands matter, but only if they're alias commands |
| 101 | + [AstVisitAction] VisitCommand([CommandAst]$ast) { |
| 102 | + if ($ast.CommandElements[0].Value -imatch "(New|Set|Remove)-Alias") { |
| 103 | + $ast.Visit($this.Parameters.Clear()) |
| 104 | + |
| 105 | + # We COULD just remove it (even if we didn't add it) ... |
| 106 | + if ($this.Parameters.Command -ieq "Remove-Alias") { |
| 107 | + # But Write-Verbose for logging purposes |
| 108 | + if ($this.Aliases.Contains($this.Parameters.Name)) { |
| 109 | + Write-Verbose -Message "Alias '$($this.Parameters.Name)' is removed by line $($ast.Extent.StartLineNumber): $($ast.Extent.Text)" |
| 110 | + $this.Aliases.Remove($this.Parameters.Name) |
| 111 | + } |
| 112 | + # We don't need to export global aliases, because they broke out already |
| 113 | + } elseif ($this.Parameters.Name -and $this.Parameters.Scope -ine 'Global') { |
| 114 | + $this.Aliases.Add($this.Parameters.Name) |
| 115 | + } |
| 116 | + } |
| 117 | + return [AstVisitAction]::SkipChildren |
| 118 | + } |
| 119 | +} |
0 commit comments