|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +#if !CORECLR |
| 4 | +using System.ComponentModel.Composition; |
| 5 | +#endif |
| 6 | +using System.Globalization; |
| 7 | +using System.Management.Automation.Language; |
| 8 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 9 | + |
| 10 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 11 | +{ |
| 12 | +#if !CORECLR |
| 13 | + [Export(typeof(IScriptRule))] |
| 14 | +#endif |
| 15 | + class AvoidGlobalAliases : AstVisitor, IScriptRule |
| 16 | + { |
| 17 | + private List<DiagnosticRecord> records; |
| 18 | + private string fileName; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Analyzes the ast to check that global aliases are not used. |
| 22 | + /// </summary> |
| 23 | + /// <param name="ast">The script's ast</param> |
| 24 | + /// <param name="fileName">The script's file name</param> |
| 25 | + /// <returns>A List of diagnostic results of this rule</returns> |
| 26 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 27 | + { |
| 28 | + if (ast == null) |
| 29 | + { |
| 30 | + throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 31 | + } |
| 32 | + |
| 33 | + records = new List<DiagnosticRecord>(); |
| 34 | + this.fileName = fileName; |
| 35 | + |
| 36 | + if (IsScriptModule()) |
| 37 | + { |
| 38 | + ast.Visit(this); |
| 39 | + } |
| 40 | + |
| 41 | + return records; |
| 42 | + } |
| 43 | + |
| 44 | + #region VisitCommand functions |
| 45 | + /// <summary> |
| 46 | + /// Analyzes a CommandAst, if it is a New-Alias command, the AST is further analyzed. |
| 47 | + /// </summary> |
| 48 | + /// <param name="commandAst">The CommandAst to be analyzed</param> |
| 49 | + /// <returns>AstVisitAction to continue to analyze the ast's children</returns> |
| 50 | + public override AstVisitAction VisitCommand(CommandAst commandAst) |
| 51 | + { |
| 52 | + if (IsNewAliasCmdlet(commandAst)) |
| 53 | + { |
| 54 | + // check the parameters of the New-Alias cmdlet for scope |
| 55 | + var parameterBindings = StaticParameterBinder.BindCommand(commandAst); |
| 56 | + |
| 57 | + if (parameterBindings.BoundParameters.ContainsKey("Scope")) |
| 58 | + { |
| 59 | + var scopeValue = parameterBindings.BoundParameters["Scope"].ConstantValue; |
| 60 | + |
| 61 | + if ((scopeValue != null) && (scopeValue.ToString().Equals("Global", StringComparison.OrdinalIgnoreCase))) |
| 62 | + { |
| 63 | + records.Add(new DiagnosticRecord( |
| 64 | + string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesError), |
| 65 | + commandAst.Extent, |
| 66 | + GetName(), |
| 67 | + DiagnosticSeverity.Warning, |
| 68 | + fileName)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return AstVisitAction.SkipChildren; |
| 74 | + } |
| 75 | + #endregion |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Determines if CommandAst is for the "New-Alias" command, checking aliases. |
| 79 | + /// </summary> |
| 80 | + /// <param name="commandAst">CommandAst to validate</param> |
| 81 | + /// <returns>True if the CommandAst is for the "New-Alias" command</returns> |
| 82 | + private bool IsNewAliasCmdlet(CommandAst commandAst) |
| 83 | + { |
| 84 | + if (commandAst == null || commandAst.GetCommandName() == null) |
| 85 | + { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + var AliasList = Helper.Instance.CmdletNameAndAliases("New-Alias"); |
| 90 | + if (AliasList.Contains(commandAst.GetCommandName())) |
| 91 | + { |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Determines if analyzing a script module. |
| 100 | + /// </summary> |
| 101 | + /// <returns>True is file name ends with ".psm1"</returns> |
| 102 | + private bool IsScriptModule() |
| 103 | + { |
| 104 | + return fileName.EndsWith(".psm1"); |
| 105 | + } |
| 106 | + |
| 107 | + public string GetCommonName() |
| 108 | + { |
| 109 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesCommonName); |
| 110 | + } |
| 111 | + |
| 112 | + public string GetDescription() |
| 113 | + { |
| 114 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesDescription); |
| 115 | + } |
| 116 | + |
| 117 | + public string GetName() |
| 118 | + { |
| 119 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalAliasesName); |
| 120 | + } |
| 121 | + |
| 122 | + public RuleSeverity GetSeverity() |
| 123 | + { |
| 124 | + return RuleSeverity.Warning; |
| 125 | + } |
| 126 | + |
| 127 | + public string GetSourceName() |
| 128 | + { |
| 129 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 130 | + } |
| 131 | + |
| 132 | + public SourceType GetSourceType() |
| 133 | + { |
| 134 | + return SourceType.Builtin; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments