|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +#if !CORECLR |
| 7 | +using System.ComponentModel.Composition; |
| 8 | +#endif |
| 9 | +using System.Globalization; |
| 10 | +using System.Linq; |
| 11 | +using System.Management.Automation; |
| 12 | +using System.Management.Automation.Language; |
| 13 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 14 | + |
| 15 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// AvoidExclaimOperator: Checks for use of the exclaim operator |
| 19 | + /// </summary> |
| 20 | +#if !CORECLR |
| 21 | + [Export(typeof(IScriptRule))] |
| 22 | +#endif |
| 23 | + public class AvoidExclaimOperator : ConfigurableRule |
| 24 | + { |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Construct an object of AvoidExclaimOperator type. |
| 28 | + /// </summary> |
| 29 | + public AvoidExclaimOperator() { |
| 30 | + Enable = false; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Analyzes the given ast to find the [violation] |
| 35 | + /// </summary> |
| 36 | + /// <param name="ast">AST to be analyzed. This should be non-null</param> |
| 37 | + /// <param name="fileName">Name of file that corresponds to the input AST.</param> |
| 38 | + /// <returns>A an enumerable type containing the violations</returns> |
| 39 | + public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 40 | + { |
| 41 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 42 | + |
| 43 | + var diagnosticRecords = new List<DiagnosticRecord>(); |
| 44 | + |
| 45 | + IEnumerable<Ast> foundAsts = ast.FindAll(testAst => testAst is UnaryExpressionAst, true); |
| 46 | + if (foundAsts != null) { |
| 47 | + var correctionDescription = Strings.AvoidExclaimOperatorCorrectionDescription; |
| 48 | + foreach (UnaryExpressionAst unaryExpressionAst in foundAsts) { |
| 49 | + if (unaryExpressionAst.TokenKind == TokenKind.Exclaim) { |
| 50 | + var replaceWith = "-not"; |
| 51 | + // The UnaryExpressionAST should have a single child, the argument that the unary operator is acting upon. |
| 52 | + // If the child's extent starts 1 after the parent's extent then there's no whitespace between the exclaim |
| 53 | + // token and any variable/expression; in that case the replacement -not should include a space |
| 54 | + if (unaryExpressionAst.Child != null && unaryExpressionAst.Child.Extent.StartColumnNumber == unaryExpressionAst.Extent.StartColumnNumber + 1) { |
| 55 | + replaceWith = "-not "; |
| 56 | + } |
| 57 | + var corrections = new List<CorrectionExtent> { |
| 58 | + new CorrectionExtent( |
| 59 | + unaryExpressionAst.Extent.StartLineNumber, |
| 60 | + unaryExpressionAst.Extent.EndLineNumber, |
| 61 | + unaryExpressionAst.Extent.StartColumnNumber, |
| 62 | + unaryExpressionAst.Extent.StartColumnNumber + 1, |
| 63 | + replaceWith, |
| 64 | + fileName, |
| 65 | + correctionDescription |
| 66 | + ) |
| 67 | + }; |
| 68 | + diagnosticRecords.Add(new DiagnosticRecord( |
| 69 | + string.Format( |
| 70 | + CultureInfo.CurrentCulture, |
| 71 | + Strings.AvoidExclaimOperatorError |
| 72 | + ), |
| 73 | + unaryExpressionAst.Extent, |
| 74 | + GetName(), |
| 75 | + GetDiagnosticSeverity(), |
| 76 | + fileName, |
| 77 | + suggestedCorrections: corrections |
| 78 | + )); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return diagnosticRecords; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Retrieves the common name of this rule. |
| 87 | + /// </summary> |
| 88 | + public override string GetCommonName() |
| 89 | + { |
| 90 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorCommonName); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Retrieves the description of this rule. |
| 95 | + /// </summary> |
| 96 | + public override string GetDescription() |
| 97 | + { |
| 98 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclaimOperatorDescription); |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Retrieves the name of this rule. |
| 103 | + /// </summary> |
| 104 | + public override string GetName() |
| 105 | + { |
| 106 | + return string.Format( |
| 107 | + CultureInfo.CurrentCulture, |
| 108 | + Strings.NameSpaceFormat, |
| 109 | + GetSourceName(), |
| 110 | + Strings.AvoidExclaimOperatorName); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Retrieves the severity of the rule: error, warning or information. |
| 115 | + /// </summary> |
| 116 | + public override RuleSeverity GetSeverity() |
| 117 | + { |
| 118 | + return RuleSeverity.Warning; |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Gets the severity of the returned diagnostic record: error, warning, or information. |
| 123 | + /// </summary> |
| 124 | + /// <returns></returns> |
| 125 | + public DiagnosticSeverity GetDiagnosticSeverity() |
| 126 | + { |
| 127 | + return DiagnosticSeverity.Warning; |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Retrieves the name of the module/assembly the rule is from. |
| 132 | + /// </summary> |
| 133 | + public override string GetSourceName() |
| 134 | + { |
| 135 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Retrieves the type of the rule, Builtin, Managed or Module. |
| 140 | + /// </summary> |
| 141 | + public override SourceType GetSourceType() |
| 142 | + { |
| 143 | + return SourceType.Builtin; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
0 commit comments