|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// |
| 3 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 4 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 5 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 6 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 7 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 8 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 9 | +// THE SOFTWARE. |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +#if !CORECLR |
| 14 | +using System.ComponentModel.Composition; |
| 15 | +#endif |
| 16 | +using System.Globalization; |
| 17 | +using System.Linq; |
| 18 | +using System.Management.Automation; |
| 19 | +using System.Management.Automation.Language; |
| 20 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 21 | + |
| 22 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// AvoidExclamationPointOperator: Checks for use of the exclamation point operator |
| 26 | + /// </summary> |
| 27 | +#if !CORECLR |
| 28 | + [Export(typeof(IScriptRule))] |
| 29 | +#endif |
| 30 | + public class AvoidExclamationPointOperator : ConfigurableRule |
| 31 | + { |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Construct an object of AvoidExclamationPointOperator type. |
| 35 | + /// </summary> |
| 36 | + public AvoidExclamationPointOperator() { |
| 37 | + Enable = false; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Analyzes the given ast to find the [violation] |
| 42 | + /// </summary> |
| 43 | + /// <param name="ast">AST to be analyzed. This should be non-null</param> |
| 44 | + /// <param name="fileName">Name of file that corresponds to the input AST.</param> |
| 45 | + /// <returns>A an enumerable type containing the violations</returns> |
| 46 | + public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 47 | + { |
| 48 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 49 | + |
| 50 | + var diagnosticRecords = new List<DiagnosticRecord>(); |
| 51 | + |
| 52 | + IEnumerable<Ast> foundAsts = ast.FindAll(testAst => testAst is UnaryExpressionAst, true); |
| 53 | + if (foundAsts != null) { |
| 54 | + var CorrectionDescription = Strings.AvoidExclamationPointOperatorCorrectionDescription; |
| 55 | + foreach (UnaryExpressionAst foundAst in foundAsts) { |
| 56 | + if (foundAst.TokenKind == TokenKind.Exclaim) { |
| 57 | + // If the exclaim is not followed by a space, add one |
| 58 | + var replaceWith = "-not"; |
| 59 | + if (foundAst.Child != null && foundAst.Child.Extent.StartColumnNumber == foundAst.Extent.StartColumnNumber + 1) { |
| 60 | + replaceWith = "-not "; |
| 61 | + } |
| 62 | + var corrections = new List<CorrectionExtent> { |
| 63 | + new CorrectionExtent( |
| 64 | + foundAst.Extent.StartLineNumber, |
| 65 | + foundAst.Extent.EndLineNumber, |
| 66 | + foundAst.Extent.StartColumnNumber, |
| 67 | + foundAst.Extent.StartColumnNumber + 1, |
| 68 | + replaceWith, |
| 69 | + fileName, |
| 70 | + CorrectionDescription |
| 71 | + ) |
| 72 | + }; |
| 73 | + diagnosticRecords.Add(new DiagnosticRecord( |
| 74 | + string.Format( |
| 75 | + CultureInfo.CurrentCulture, |
| 76 | + Strings.AvoidExclamationPointOperatorError |
| 77 | + ), |
| 78 | + foundAst.Extent, |
| 79 | + GetName(), |
| 80 | + GetDiagnosticSeverity(), |
| 81 | + fileName, |
| 82 | + suggestedCorrections: corrections |
| 83 | + )); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return diagnosticRecords; |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Retrieves the common name of this rule. |
| 92 | + /// </summary> |
| 93 | + public override string GetCommonName() |
| 94 | + { |
| 95 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclamationPointOperatorCommonName); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Retrieves the description of this rule. |
| 100 | + /// </summary> |
| 101 | + public override string GetDescription() |
| 102 | + { |
| 103 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidExclamationPointOperatorDescription); |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Retrieves the name of this rule. |
| 108 | + /// </summary> |
| 109 | + public override string GetName() |
| 110 | + { |
| 111 | + return string.Format( |
| 112 | + CultureInfo.CurrentCulture, |
| 113 | + Strings.NameSpaceFormat, |
| 114 | + GetSourceName(), |
| 115 | + Strings.AvoidExclamationPointOperatorName); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Retrieves the severity of the rule: error, warning or information. |
| 120 | + /// </summary> |
| 121 | + public override RuleSeverity GetSeverity() |
| 122 | + { |
| 123 | + return RuleSeverity.Warning; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Gets the severity of the returned diagnostic record: error, warning, or information. |
| 128 | + /// </summary> |
| 129 | + /// <returns></returns> |
| 130 | + public DiagnosticSeverity GetDiagnosticSeverity() |
| 131 | + { |
| 132 | + return DiagnosticSeverity.Warning; |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Retrieves the name of the module/assembly the rule is from. |
| 137 | + /// </summary> |
| 138 | + public override string GetSourceName() |
| 139 | + { |
| 140 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Retrieves the type of the rule, Builtin, Managed or Module. |
| 145 | + /// </summary> |
| 146 | + public override SourceType GetSourceType() |
| 147 | + { |
| 148 | + return SourceType.Builtin; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments