|
| 1 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Management.Automation.Language; |
| 8 | +using System.Globalization; |
| 9 | +using System.ComponentModel.Composition; |
| 10 | + |
| 11 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 12 | +{ |
| 13 | + [Export(typeof(IScriptRule))] |
| 14 | + public class AvoidGlobalFunctions : AstVisitor, IScriptRule |
| 15 | + { |
| 16 | + List<DiagnosticRecord> records; |
| 17 | + string fileName; |
| 18 | + |
| 19 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 20 | + { |
| 21 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 22 | + |
| 23 | + records = new List<DiagnosticRecord>(); |
| 24 | + this.fileName = fileName; |
| 25 | + |
| 26 | + ast.Visit(this); |
| 27 | + |
| 28 | + return records; |
| 29 | + } |
| 30 | + |
| 31 | + #region VisitComman functions |
| 32 | + public override AstVisitAction VisitCommand(CommandAst commandAst) |
| 33 | + { |
| 34 | + if (!IsNewAliasCmdlet(commandAst)) |
| 35 | + { |
| 36 | + return AstVisitAction.SkipChildren; |
| 37 | + } |
| 38 | + |
| 39 | + return AstVisitAction.Continue; |
| 40 | + } |
| 41 | + |
| 42 | + public override AstVisitAction VisitCommandParameter(CommandParameterAst commandParameterAst) |
| 43 | + { |
| 44 | + if(isScopeParameterForNewAliasCmdlet(commandParameterAst)) |
| 45 | + { |
| 46 | + if ((commandParameterAst.Argument != null) // if the cmdlet looks like -Scope:Global check Parameter.Argument |
| 47 | + && (commandParameterAst.Argument.ToString().Equals("Global", StringComparison.OrdinalIgnoreCase))) |
| 48 | + { |
| 49 | + records.Add(new DiagnosticRecord( |
| 50 | + string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsAliasError), |
| 51 | + commandParameterAst.Extent, |
| 52 | + GetName(), |
| 53 | + DiagnosticSeverity.Warning, |
| 54 | + fileName, |
| 55 | + commandParameterAst.ParameterName)); |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + var nextAst = FindNextAst(commandParameterAst); |
| 60 | + |
| 61 | + if((nextAst is StringConstantExpressionAst) // if the cmdlet looks like -Scope Global |
| 62 | + && (((StringConstantExpressionAst)nextAst).Value.ToString().Equals("Global", StringComparison.OrdinalIgnoreCase))) |
| 63 | + { |
| 64 | + records.Add(new DiagnosticRecord( |
| 65 | + string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsAliasError), |
| 66 | + ((StringConstantExpressionAst)nextAst).Extent, |
| 67 | + GetName(), |
| 68 | + DiagnosticSeverity.Warning, |
| 69 | + fileName, |
| 70 | + ((StringConstantExpressionAst)nextAst).Value)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return AstVisitAction.SkipChildren; |
| 76 | + } |
| 77 | + |
| 78 | + public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst functionDefinitionAst) |
| 79 | + { |
| 80 | + if (functionDefinitionAst.Name.StartsWith("Global:", StringComparison.OrdinalIgnoreCase)) |
| 81 | + { |
| 82 | + records.Add(new DiagnosticRecord( |
| 83 | + string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsError), |
| 84 | + functionDefinitionAst.Extent, |
| 85 | + GetName(), |
| 86 | + DiagnosticSeverity.Warning, |
| 87 | + fileName, |
| 88 | + functionDefinitionAst.Name)); |
| 89 | + } |
| 90 | + |
| 91 | + return AstVisitAction.Continue; |
| 92 | + } |
| 93 | + #endregion |
| 94 | + |
| 95 | + private Ast FindNextAst(Ast ast) |
| 96 | + { |
| 97 | + IEnumerable<Ast> matchingLevelAsts = ast.Parent.FindAll(item => item is Ast, true ); |
| 98 | + |
| 99 | + Ast currentClosest = null; |
| 100 | + foreach(var matchingLevelAst in matchingLevelAsts) |
| 101 | + { |
| 102 | + if (currentClosest == null) |
| 103 | + { |
| 104 | + if (isAstAfter(ast, matchingLevelAst)) |
| 105 | + { |
| 106 | + currentClosest = matchingLevelAst; |
| 107 | + } |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + if((isAstAfter(ast, matchingLevelAst)) && (isAstAfter(matchingLevelAst, currentClosest))) |
| 112 | + { |
| 113 | + currentClosest = matchingLevelAst; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return currentClosest; |
| 119 | + } |
| 120 | + |
| 121 | + private bool isAstAfter(Ast ast1, Ast ast2) |
| 122 | + { |
| 123 | + if(ast1.Extent.EndLineNumber > ast2.Extent.StartLineNumber) // ast1 ends on a line after ast2 starts |
| 124 | + { |
| 125 | + return false; |
| 126 | + } |
| 127 | + else if(ast1.Extent.EndLineNumber == ast2.Extent.StartLineNumber) |
| 128 | + { |
| 129 | + if(ast2.Extent.StartColumnNumber > ast1.Extent.EndColumnNumber) |
| 130 | + { |
| 131 | + return true; |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + return false; |
| 136 | + } |
| 137 | + } |
| 138 | + else // ast2 starts on a line after ast 1 ends |
| 139 | + { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private bool isScopeParameterForNewAliasCmdlet(CommandParameterAst commandParameterAst) |
| 145 | + { |
| 146 | + if (commandParameterAst == null || commandParameterAst.ParameterName == null) |
| 147 | + return false; |
| 148 | + |
| 149 | + if(commandParameterAst.ParameterName.Equals("Scope", StringComparison.OrdinalIgnoreCase) |
| 150 | + && (commandParameterAst.Parent is CommandAst) |
| 151 | + && IsNewAliasCmdlet((CommandAst)commandParameterAst.Parent)) |
| 152 | + { |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + private bool IsNewAliasCmdlet(CommandAst commandAst) |
| 160 | + { |
| 161 | + if (commandAst == null || commandAst.GetCommandName() == null) |
| 162 | + return false; |
| 163 | + |
| 164 | + var AliasList = Helper.Instance.CmdletNameAndAliases("New-Alias"); |
| 165 | + if (AliasList.Contains(commandAst.GetCommandName())) |
| 166 | + { |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + public string GetCommonName() |
| 174 | + { |
| 175 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsCommonName); |
| 176 | + } |
| 177 | + |
| 178 | + public string GetDescription() |
| 179 | + { |
| 180 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsDescription); |
| 181 | + } |
| 182 | + |
| 183 | + public string GetName() |
| 184 | + { |
| 185 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidGlobalFunctionsName); |
| 186 | + } |
| 187 | + |
| 188 | + public RuleSeverity GetSeverity() |
| 189 | + { |
| 190 | + return RuleSeverity.Warning; |
| 191 | + } |
| 192 | + |
| 193 | + public string GetSourceName() |
| 194 | + { |
| 195 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 196 | + } |
| 197 | + |
| 198 | + public SourceType GetSourceType() |
| 199 | + { |
| 200 | + return SourceType.Builtin; |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments