|
| 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.Collections.Generic; |
| 15 | +using System.Linq; |
| 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 | + /// AvoidUsingNullOrEmptyHelpMessageAttribute: Check if the HelpMessage parameter is set to a non-empty string. |
| 25 | + /// </summary> |
| 26 | + [Export(typeof(IScriptRule))] |
| 27 | + public class AvoidNullOrEmptyHelpMessageAttribute : IScriptRule |
| 28 | + { |
| 29 | + /// <summary> |
| 30 | + /// AvoidUsingNullOrEmptyHelpMessageAttribute: Check if the HelpMessage parameter is set to a non-empty string. |
| 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 | + foreach (FunctionDefinitionAst funcAst in functionAsts) |
| 40 | + { |
| 41 | + if (funcAst.Body == null || funcAst.Body.ParamBlock == null |
| 42 | + || funcAst.Body.ParamBlock.Attributes == null || funcAst.Body.ParamBlock.Parameters == null) |
| 43 | + { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + foreach (var paramAst in funcAst.Body.ParamBlock.Parameters) |
| 48 | + { |
| 49 | + foreach (var paramAstAttribute in paramAst.Attributes) |
| 50 | + { |
| 51 | + if (!(paramAstAttribute is AttributeAst)) |
| 52 | + { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + var namedArguments = (paramAstAttribute as AttributeAst).NamedArguments; |
| 57 | + |
| 58 | + if (namedArguments == null) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + foreach (NamedAttributeArgumentAst namedArgument in namedArguments) |
| 64 | + { |
| 65 | + if (!(namedArgument.ArgumentName.Equals("HelpMessage", StringComparison.OrdinalIgnoreCase))) |
| 66 | + { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + string errCondition; |
| 71 | + if (namedArgument.ExpressionOmitted |
| 72 | + || namedArgument.Argument.Extent.Text.Equals("\"\"") |
| 73 | + || namedArgument.Argument.Extent.Text.Equals("\'\'")) |
| 74 | + { |
| 75 | + errCondition = "empty"; |
| 76 | + } |
| 77 | + else if (namedArgument.Argument.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase)) |
| 78 | + { |
| 79 | + errCondition = "null"; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + errCondition = null; |
| 84 | + } |
| 85 | + |
| 86 | + if (!String.IsNullOrEmpty(errCondition)) |
| 87 | + { |
| 88 | + string message = string.Format(CultureInfo.CurrentCulture, |
| 89 | + Strings.AvoidNullOrEmptyHelpMessageAttributeError, |
| 90 | + paramAst.Name.VariablePath.UserPath); |
| 91 | + yield return new DiagnosticRecord(message, |
| 92 | + paramAst.Extent, |
| 93 | + GetName(), |
| 94 | + DiagnosticSeverity.Warning, |
| 95 | + fileName, |
| 96 | + paramAst.Name.VariablePath.UserPath); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// GetName: Retrieves the name of this rule. |
| 107 | + /// </summary> |
| 108 | + /// <returns>The name of this rule</returns> |
| 109 | + public string GetName() |
| 110 | + { |
| 111 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidNullOrEmptyHelpMessageAttributeName); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// GetCommonName: Retrieves the common name of this rule. |
| 116 | + /// </summary> |
| 117 | + /// <returns>The common name of this rule</returns> |
| 118 | + public string GetCommonName() |
| 119 | + { |
| 120 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidNullOrEmptyHelpMessageAttributeCommonName); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// GetDescription: Retrieves the description of this rule. |
| 125 | + /// </summary> |
| 126 | + /// <returns>The description of this rule</returns> |
| 127 | + public string GetDescription() |
| 128 | + { |
| 129 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidNullOrEmptyHelpMessageAttributeDescription); |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// GetSourceType: Retrieves the type of the rule, builtin, managed or module. |
| 134 | + /// </summary> |
| 135 | + public SourceType GetSourceType() |
| 136 | + { |
| 137 | + return SourceType.Builtin; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 142 | + /// </summary> |
| 143 | + /// <returns></returns> |
| 144 | + public RuleSeverity GetSeverity() |
| 145 | + { |
| 146 | + return RuleSeverity.Warning; |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 151 | + /// </summary> |
| 152 | + public string GetSourceName() |
| 153 | + { |
| 154 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments