|
| 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.Language; |
| 12 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 13 | + |
| 14 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// AvoidSemicolonsAsLineTerminators: Checks for lines to don't end with semicolons |
| 18 | + /// </summary> |
| 19 | +#if !CORECLR |
| 20 | + [Export(typeof(IScriptRule))] |
| 21 | +#endif |
| 22 | + public class AvoidSemicolonsAsLineTerminators : ConfigurableRule |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// Construct an object of AvoidSemicolonsAsLineTerminators type. |
| 26 | + /// </summary> |
| 27 | + public AvoidSemicolonsAsLineTerminators() |
| 28 | + { |
| 29 | + Enable = false; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Analyzes the given ast to find violations. |
| 34 | + /// </summary> |
| 35 | + /// <param name="ast">AST to be analyzed. This should be non-null</param> |
| 36 | + /// <param name="fileName">Name of file that corresponds to the input AST.</param> |
| 37 | + /// <returns>A an enumerable type containing the violations</returns> |
| 38 | + public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 39 | + { |
| 40 | + if (ast == null) |
| 41 | + { |
| 42 | + throw new ArgumentNullException(nameof(ast)); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + var diagnosticRecords = new List<DiagnosticRecord>(); |
| 47 | + |
| 48 | + IEnumerable<Ast> assignmentStatements = ast.FindAll(item => item is AssignmentStatementAst, true); |
| 49 | + |
| 50 | + var tokens = Helper.Instance.Tokens; |
| 51 | + for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++) |
| 52 | + { |
| 53 | + |
| 54 | + var token = tokens[tokenIndex]; |
| 55 | + var semicolonTokenExtent = token.Extent; |
| 56 | + |
| 57 | + var isSemicolonToken = token.Kind is TokenKind.Semi; |
| 58 | + if (!isSemicolonToken) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + var isPartOfAnyAssignmentStatement = assignmentStatements.Any(assignmentStatement => (assignmentStatement.Extent.EndOffset == semicolonTokenExtent.StartOffset + 1)); |
| 64 | + if (isPartOfAnyAssignmentStatement) |
| 65 | + { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + var nextTokenIndex = tokenIndex + 1; |
| 70 | + var isNextTokenIsNewLine = tokens[nextTokenIndex].Kind is TokenKind.NewLine; |
| 71 | + var isNextTokenIsEndOfInput = tokens[nextTokenIndex].Kind is TokenKind.EndOfInput; |
| 72 | + |
| 73 | + if (!isNextTokenIsNewLine && !isNextTokenIsEndOfInput) |
| 74 | + { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + var violationExtent = new ScriptExtent( |
| 79 | + new ScriptPosition( |
| 80 | + ast.Extent.File, |
| 81 | + semicolonTokenExtent.StartLineNumber, |
| 82 | + semicolonTokenExtent.StartColumnNumber, |
| 83 | + semicolonTokenExtent.StartScriptPosition.Line |
| 84 | + ), |
| 85 | + new ScriptPosition( |
| 86 | + ast.Extent.File, |
| 87 | + semicolonTokenExtent.EndLineNumber, |
| 88 | + semicolonTokenExtent.EndColumnNumber, |
| 89 | + semicolonTokenExtent.EndScriptPosition.Line |
| 90 | + )); |
| 91 | + |
| 92 | + var suggestedCorrections = new List<CorrectionExtent>(); |
| 93 | + suggestedCorrections.Add(new CorrectionExtent( |
| 94 | + violationExtent, |
| 95 | + string.Empty, |
| 96 | + ast.Extent.File |
| 97 | + )); |
| 98 | + |
| 99 | + var record = new DiagnosticRecord( |
| 100 | + String.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsError), |
| 101 | + violationExtent, |
| 102 | + GetName(), |
| 103 | + GetDiagnosticSeverity(), |
| 104 | + ast.Extent.File, |
| 105 | + null, |
| 106 | + suggestedCorrections |
| 107 | + ); |
| 108 | + diagnosticRecords.Add(record); |
| 109 | + } |
| 110 | + |
| 111 | + return diagnosticRecords; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Retrieves the common name of this rule. |
| 116 | + /// </summary> |
| 117 | + public override string GetCommonName() |
| 118 | + { |
| 119 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsCommonName); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Retrieves the description of this rule. |
| 124 | + /// </summary> |
| 125 | + public override string GetDescription() |
| 126 | + { |
| 127 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidSemicolonsAsLineTerminatorsDescription); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Retrieves the name of this rule. |
| 132 | + /// </summary> |
| 133 | + public override string GetName() |
| 134 | + { |
| 135 | + return string.Format( |
| 136 | + CultureInfo.CurrentCulture, |
| 137 | + Strings.NameSpaceFormat, |
| 138 | + GetSourceName(), |
| 139 | + Strings.AvoidSemicolonsAsLineTerminatorsName); |
| 140 | + } |
| 141 | + |
| 142 | + /// <summary> |
| 143 | + /// Retrieves the severity of the rule: error, warning or information. |
| 144 | + /// </summary> |
| 145 | + public override RuleSeverity GetSeverity() |
| 146 | + { |
| 147 | + return RuleSeverity.Warning; |
| 148 | + } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Gets the severity of the returned diagnostic record: error, warning, or information. |
| 152 | + /// </summary> |
| 153 | + /// <returns></returns> |
| 154 | + public DiagnosticSeverity GetDiagnosticSeverity() |
| 155 | + { |
| 156 | + return DiagnosticSeverity.Warning; |
| 157 | + } |
| 158 | + |
| 159 | + /// <summary> |
| 160 | + /// Retrieves the name of the module/assembly the rule is from. |
| 161 | + /// </summary> |
| 162 | + public override string GetSourceName() |
| 163 | + { |
| 164 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Retrieves the type of the rule, Builtin, Managed or Module. |
| 169 | + /// </summary> |
| 170 | + public override SourceType GetSourceType() |
| 171 | + { |
| 172 | + return SourceType.Builtin; |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments