|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Linq; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Management.Automation.Language; |
| 17 | +using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; |
| 18 | +using System.ComponentModel.Composition; |
| 19 | +using System.Globalization; |
| 20 | + |
| 21 | +namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// ProvideDefaultParameterValue: Check if any uninitialized variable is used. |
| 25 | + /// </summary> |
| 26 | + [Export(typeof(IScriptRule))] |
| 27 | + public class ProvideDefaultParameterValue : IScriptRule |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// AnalyzeScript: Check if any uninitialized variable is used. |
| 31 | + /// </summary> |
| 32 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 33 | + { |
| 34 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 35 | + |
| 36 | + // Finds all functionAst |
| 37 | + IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true); |
| 38 | + |
| 39 | + // Checks whether this is a dsc resource file (we don't raise this rule for get, set and test-target resource |
| 40 | + bool isDscResourceFile = Helper.Instance.IsDscResourceModule(fileName); |
| 41 | + |
| 42 | + List<string> targetResourcesFunctions = new List<string>(new string[] { "get-targetresource", "set-targetresource", "test-targetresource" }); |
| 43 | + |
| 44 | + |
| 45 | + foreach (FunctionDefinitionAst funcAst in functionAsts) |
| 46 | + { |
| 47 | + // Finds all ParamAsts. |
| 48 | + IEnumerable<Ast> varAsts = funcAst.FindAll(testAst => testAst is VariableExpressionAst, true); |
| 49 | + |
| 50 | + // Iterrates all ParamAsts and check if their names are on the list. |
| 51 | + |
| 52 | + HashSet<string> dscVariables = new HashSet<string>(); |
| 53 | + if (isDscResourceFile && targetResourcesFunctions.Contains(funcAst.Name, StringComparer.OrdinalIgnoreCase)) |
| 54 | + { |
| 55 | + // don't raise the rules for variables in the param block. |
| 56 | + if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null) |
| 57 | + { |
| 58 | + dscVariables.UnionWith(funcAst.Body.ParamBlock.Parameters.Select(paramAst => paramAst.Name.VariablePath.UserPath)); |
| 59 | + } |
| 60 | + } |
| 61 | + // only raise the rules for variables in the param block. |
| 62 | + if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Parameters != null) |
| 63 | + { |
| 64 | + foreach (var paramAst in funcAst.Body.ParamBlock.Parameters) |
| 65 | + { |
| 66 | + if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst) && !dscVariables.Contains(paramAst.Name.VariablePath.UserPath)) |
| 67 | + { |
| 68 | + yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, paramAst.Name.VariablePath.UserPath), |
| 69 | + paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (funcAst.Parameters != null) |
| 75 | + { |
| 76 | + foreach (var paramAst in funcAst.Parameters) |
| 77 | + { |
| 78 | + if (Helper.Instance.IsUninitialized(paramAst.Name, funcAst) && !dscVariables.Contains(paramAst.Name.VariablePath.UserPath)) |
| 79 | + { |
| 80 | + yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueError, paramAst.Name.VariablePath.UserPath), |
| 81 | + paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// GetName: Retrieves the name of this rule. |
| 90 | + /// </summary> |
| 91 | + /// <returns>The name of this rule</returns> |
| 92 | + public string GetName() |
| 93 | + { |
| 94 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.ProvideDefaultParameterValueName); |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// GetCommonName: Retrieves the common name of this rule. |
| 99 | + /// </summary> |
| 100 | + /// <returns>The common name of this rule</returns> |
| 101 | + public string GetCommonName() |
| 102 | + { |
| 103 | + return string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueCommonName); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// GetDescription: Retrieves the description of this rule. |
| 108 | + /// </summary> |
| 109 | + /// <returns>The description of this rule</returns> |
| 110 | + public string GetDescription() |
| 111 | + { |
| 112 | + return string.Format(CultureInfo.CurrentCulture, Strings.ProvideDefaultParameterValueDescription); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Method: Retrieves the type of the rule: builtin, managed or module. |
| 117 | + /// </summary> |
| 118 | + public SourceType GetSourceType() |
| 119 | + { |
| 120 | + return SourceType.Builtin; |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 125 | + /// </summary> |
| 126 | + /// <returns></returns> |
| 127 | + public RuleSeverity GetSeverity() |
| 128 | + { |
| 129 | + return RuleSeverity.Warning; |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Method: Retrieves the module/assembly name the rule is from. |
| 134 | + /// </summary> |
| 135 | + public string GetSourceName() |
| 136 | + { |
| 137 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments