|
| 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.Management.Automation.Language; |
| 11 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 12 | + |
| 13 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// AvoidUsingDoubleQuotesForConstantStrings: Checks if a string that uses double quotes contains a constant string, which could be simplified to a single quote. |
| 17 | + /// </summary> |
| 18 | +#if !CORECLR |
| 19 | + [Export(typeof(IScriptRule))] |
| 20 | +#endif |
| 21 | + public class AvoidUsingDoubleQuotesForConstantString : ConfigurableRule |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// Construct an object of type <seealso cref="AvoidUsingDoubleQuotesForConstantStrings"/>. |
| 25 | + /// </summary> |
| 26 | + public AvoidUsingDoubleQuotesForConstantString() |
| 27 | + { |
| 28 | + Enable = false; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Analyzes the given ast to find occurences of StringConstantExpressionAst where double quotes are used |
| 33 | + /// and could be replaced with single quotes. |
| 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 | + var stringConstantExpressionAsts = ast.FindAll(testAst => testAst is StringConstantExpressionAst, searchNestedScriptBlocks: true); |
| 46 | + foreach (StringConstantExpressionAst stringConstantExpressionAst in stringConstantExpressionAsts) |
| 47 | + { |
| 48 | + switch (stringConstantExpressionAst.StringConstantType) |
| 49 | + { |
| 50 | + /* |
| 51 | + If an interpolated string (i.e. using double quotes) uses single quotes (or @' in case of a here-string), then do not warn. |
| 52 | + This because one would then have to escape this single quote, which would make the string less readable. |
| 53 | + If an interpolated string contains a backtick character, then do not warn as well. |
| 54 | + This is because we'd have to replace the backtick in some cases like `$ and in others like `a it would not be possible at all. |
| 55 | + Therefore to keep it simple, all interpolated strings with a backtick are being excluded. |
| 56 | + */ |
| 57 | + case StringConstantType.DoubleQuoted: |
| 58 | + if (stringConstantExpressionAst.Value.Contains("'") || stringConstantExpressionAst.Extent.Text.Contains("`")) |
| 59 | + { |
| 60 | + break; |
| 61 | + } |
| 62 | + yield return GetDiagnosticRecord(stringConstantExpressionAst, |
| 63 | + $"'{stringConstantExpressionAst.Value}'"); |
| 64 | + break; |
| 65 | + |
| 66 | + case StringConstantType.DoubleQuotedHereString: |
| 67 | + if (stringConstantExpressionAst.Value.Contains("@'") || stringConstantExpressionAst.Extent.Text.Contains("`")) |
| 68 | + { |
| 69 | + break; |
| 70 | + } |
| 71 | + yield return GetDiagnosticRecord(stringConstantExpressionAst, |
| 72 | + $"@'{Environment.NewLine}{stringConstantExpressionAst.Value}{Environment.NewLine}'@"); |
| 73 | + break; |
| 74 | + |
| 75 | + default: |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private DiagnosticRecord GetDiagnosticRecord(StringConstantExpressionAst stringConstantExpressionAst, |
| 82 | + string suggestedCorrection) |
| 83 | + { |
| 84 | + return new DiagnosticRecord( |
| 85 | + Strings.AvoidUsingDoubleQuotesForConstantStringError, |
| 86 | + stringConstantExpressionAst.Extent, |
| 87 | + GetName(), |
| 88 | + GetDiagnosticSeverity(), |
| 89 | + stringConstantExpressionAst.Extent.File, |
| 90 | + suggestedCorrections: new[] { |
| 91 | + new CorrectionExtent( |
| 92 | + stringConstantExpressionAst.Extent, |
| 93 | + suggestedCorrection, |
| 94 | + stringConstantExpressionAst.Extent.File |
| 95 | + ) |
| 96 | + } |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Retrieves the common name of this rule. |
| 102 | + /// </summary> |
| 103 | + public override string GetCommonName() |
| 104 | + { |
| 105 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDoubleQuotesForConstantStringCommonName); |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Retrieves the description of this rule. |
| 110 | + /// </summary> |
| 111 | + public override string GetDescription() |
| 112 | + { |
| 113 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingDoubleQuotesForConstantStringDescription); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Retrieves the name of this rule. |
| 118 | + /// </summary> |
| 119 | + public override string GetName() |
| 120 | + { |
| 121 | + return string.Format( |
| 122 | + CultureInfo.CurrentCulture, |
| 123 | + Strings.NameSpaceFormat, |
| 124 | + GetSourceName(), |
| 125 | + Strings.AvoidUsingDoubleQuotesForConstantStringName); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Retrieves the severity of the rule: error, warning or information. |
| 130 | + /// </summary> |
| 131 | + public override RuleSeverity GetSeverity() |
| 132 | + { |
| 133 | + return RuleSeverity.Information; |
| 134 | + } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Gets the severity of the returned diagnostic record: error, warning, or information. |
| 138 | + /// </summary> |
| 139 | + /// <returns></returns> |
| 140 | + public DiagnosticSeverity GetDiagnosticSeverity() |
| 141 | + { |
| 142 | + return DiagnosticSeverity.Information; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Retrieves the name of the module/assembly the rule is from. |
| 147 | + /// </summary> |
| 148 | + public override string GetSourceName() |
| 149 | + { |
| 150 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Retrieves the type of the rule, Builtin, Managed or Module. |
| 155 | + /// </summary> |
| 156 | + public override SourceType GetSourceType() |
| 157 | + { |
| 158 | + return SourceType.Builtin; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments