|
| 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 | +using System.Text.RegularExpressions; |
| 21 | + |
| 22 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// MisleadingBacktick: Checks that lines don't end with a backtick followed by whitespace |
| 26 | + /// </summary> |
| 27 | + [Export(typeof(IScriptRule))] |
| 28 | + public class MisleadingBacktick : IScriptRule |
| 29 | + { |
| 30 | + private static Regex TrailingEscapedWhitespaceRegex = new Regex(@"`\s+$"); |
| 31 | + private static Regex NewlineRegex = new Regex("\r?\n"); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// MisleadingBacktick: Checks that lines don't end with a backtick followed by a whitespace |
| 35 | + /// </summary> |
| 36 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 37 | + { |
| 38 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 39 | + |
| 40 | + string[] lines = NewlineRegex.Split(ast.Extent.Text); |
| 41 | + |
| 42 | + if((ast.Extent.EndLineNumber - ast.Extent.StartLineNumber + 1) != lines.Length) |
| 43 | + { |
| 44 | + // Did not match the number of lines that the extent indicated |
| 45 | + yield break; |
| 46 | + } |
| 47 | + |
| 48 | + foreach (int i in Enumerable.Range(0, lines.Length)) |
| 49 | + { |
| 50 | + string line = lines[i]; |
| 51 | + |
| 52 | + Match match = TrailingEscapedWhitespaceRegex.Match(line); |
| 53 | + |
| 54 | + if(match.Success) |
| 55 | + { |
| 56 | + int lineNumber = ast.Extent.StartLineNumber + i; |
| 57 | + |
| 58 | + ScriptPosition start = new ScriptPosition(fileName, lineNumber, match.Index, line); |
| 59 | + ScriptPosition end = new ScriptPosition(fileName, lineNumber, match.Index + match.Length, line); |
| 60 | + |
| 61 | + yield return new DiagnosticRecord( |
| 62 | + string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickError), |
| 63 | + new ScriptExtent(start, end), GetName(), DiagnosticSeverity.Warning, fileName); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// GetName: Retrieves the name of this rule. |
| 70 | + /// </summary> |
| 71 | + /// <returns>The name of this rule</returns> |
| 72 | + public string GetName() |
| 73 | + { |
| 74 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.MisleadingBacktickName); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// GetCommonName: Retrieves the common name of this rule. |
| 79 | + /// </summary> |
| 80 | + /// <returns>The common name of this rule</returns> |
| 81 | + public string GetCommonName() |
| 82 | + { |
| 83 | + return string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickCommonName); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// GetDescription: Retrieves the description of this rule. |
| 88 | + /// </summary> |
| 89 | + /// <returns>The description of this rule</returns> |
| 90 | + public string GetDescription() |
| 91 | + { |
| 92 | + return string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickDescription); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. |
| 97 | + /// </summary> |
| 98 | + public SourceType GetSourceType() |
| 99 | + { |
| 100 | + return SourceType.Builtin; |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 105 | + /// </summary> |
| 106 | + /// <returns></returns> |
| 107 | + public RuleSeverity GetSeverity() |
| 108 | + { |
| 109 | + return RuleSeverity.Warning; |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 114 | + /// </summary> |
| 115 | + public string GetSourceName() |
| 116 | + { |
| 117 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments