|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Management.Automation.Language; |
| 8 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 9 | +#if !CORECLR |
| 10 | +using System.ComponentModel.Composition; |
| 11 | +#endif |
| 12 | +using System.Globalization; |
| 13 | + |
| 14 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// ReviewUnusedParameter: Check that all declared parameters are used in the script body. |
| 18 | + /// </summary> |
| 19 | +#if !CORECLR |
| 20 | +[Export(typeof(IScriptRule))] |
| 21 | +#endif |
| 22 | + public class ReviewUnusedParameter : IScriptRule |
| 23 | + { |
| 24 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 25 | + { |
| 26 | + if (ast == null) |
| 27 | + { |
| 28 | + throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 29 | + } |
| 30 | + |
| 31 | + IEnumerable<Ast> scriptBlockAsts = ast.FindAll(oneAst => oneAst is ScriptBlockAst, true); |
| 32 | + if (scriptBlockAsts == null) |
| 33 | + { |
| 34 | + yield break; |
| 35 | + } |
| 36 | + |
| 37 | + foreach (ScriptBlockAst scriptBlockAst in scriptBlockAsts) |
| 38 | + { |
| 39 | + // find all declared parameters |
| 40 | + IEnumerable<Ast> parameterAsts = scriptBlockAst.FindAll(oneAst => oneAst is ParameterAst, false); |
| 41 | + |
| 42 | + // list all variables |
| 43 | + IDictionary<string, int> variableCount = scriptBlockAst.FindAll(oneAst => oneAst is VariableExpressionAst, false) |
| 44 | + .Select(variableExpressionAst => ((VariableExpressionAst)variableExpressionAst).VariablePath.UserPath) |
| 45 | + .GroupBy(variableName => variableName, StringComparer.OrdinalIgnoreCase) |
| 46 | + .ToDictionary(variableName => variableName.Key, variableName => variableName.Count(), StringComparer.OrdinalIgnoreCase); |
| 47 | + |
| 48 | + // all bets are off if the script uses PSBoundParameters |
| 49 | + if (variableCount.ContainsKey("PSBoundParameters")) |
| 50 | + { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + foreach (ParameterAst parameterAst in parameterAsts) |
| 55 | + { |
| 56 | + // there should be at least two usages of the variable since the parameter declaration counts as one |
| 57 | + variableCount.TryGetValue(parameterAst.Name.VariablePath.UserPath, out int variableUsageCount); |
| 58 | + if (variableUsageCount >= 2) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + yield return new DiagnosticRecord( |
| 64 | + string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterError, parameterAst.Name.VariablePath.UserPath), |
| 65 | + parameterAst.Name.Extent, |
| 66 | + GetName(), |
| 67 | + DiagnosticSeverity.Warning, |
| 68 | + fileName, |
| 69 | + parameterAst.Name.VariablePath.UserPath |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// GetName: Retrieves the name of this rule. |
| 77 | + /// </summary> |
| 78 | + /// <returns>The name of this rule</returns> |
| 79 | + public string GetName() |
| 80 | + { |
| 81 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ReviewUnusedParameterName); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// GetCommonName: Retrieves the common name of this rule. |
| 86 | + /// </summary> |
| 87 | + /// <returns>The common name of this rule</returns> |
| 88 | + public string GetCommonName() |
| 89 | + { |
| 90 | + return string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterCommonName); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// GetDescription: Retrieves the description of this rule. |
| 95 | + /// </summary> |
| 96 | + /// <returns>The description of this rule</returns> |
| 97 | + public string GetDescription() |
| 98 | + { |
| 99 | + return string.Format(CultureInfo.CurrentCulture, Strings.ReviewUnusedParameterDescription); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. |
| 104 | + /// </summary> |
| 105 | + public SourceType GetSourceType() |
| 106 | + { |
| 107 | + return SourceType.Builtin; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 112 | + /// </summary> |
| 113 | + /// <returns></returns> |
| 114 | + public RuleSeverity GetSeverity() |
| 115 | + { |
| 116 | + return RuleSeverity.Warning; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 121 | + /// </summary> |
| 122 | + public string GetSourceName() |
| 123 | + { |
| 124 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments