|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +#if !CORECLR |
| 8 | +using System.ComponentModel.Composition; |
| 9 | +#endif |
| 10 | +using System.Globalization; |
| 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 | + /// AvoidLongLines: Checks for lines longer than 120 characters |
| 18 | + /// </summary> |
| 19 | +#if !CORECLR |
| 20 | + [Export(typeof(IScriptRule))] |
| 21 | +#endif |
| 22 | + public class AvoidLongLines : ConfigurableRule |
| 23 | + { |
| 24 | + /// <summary> |
| 25 | + /// Construct an object of AvoidLongLines type. |
| 26 | + /// </summary> |
| 27 | + public AvoidLongLines() |
| 28 | + { } |
| 29 | + |
| 30 | + [ConfigurableRuleProperty(defaultValue: 120)] |
| 31 | + public int MaximumLineLength { get; set; } |
| 32 | + |
| 33 | + private readonly string[] s_lineSeparators = new[] { "\r\n", "\n" }; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Analyzes the given ast to find violations. |
| 37 | + /// </summary> |
| 38 | + /// <param name="ast">AST to be analyzed. This should be non-null</param> |
| 39 | + /// <param name="fileName">Name of file that corresponds to the input AST.</param> |
| 40 | + /// <returns>A an enumerable type containing the violations</returns> |
| 41 | + public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 42 | + { |
| 43 | + if (ast == null) |
| 44 | + { |
| 45 | + throw new ArgumentNullException(nameof(ast)); |
| 46 | + } |
| 47 | + |
| 48 | + var diagnosticRecords = new List<DiagnosticRecord>(); |
| 49 | + |
| 50 | + string[] lines = ast.Extent.Text.Split(s_lineSeparators, StringSplitOptions.None); |
| 51 | + |
| 52 | + for (int lineNumber = 0; lineNumber < lines.Length; lineNumber++) |
| 53 | + { |
| 54 | + string line = lines[lineNumber]; |
| 55 | + |
| 56 | + if (line.Length <= MaximumLineLength) |
| 57 | + { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + int startLine = lineNumber + 1; |
| 62 | + int endLine = startLine; |
| 63 | + int startColumn = 1; |
| 64 | + int endColumn = line.Length; |
| 65 | + |
| 66 | + var violationExtent = new ScriptExtent( |
| 67 | + new ScriptPosition( |
| 68 | + ast.Extent.File, |
| 69 | + startLine, |
| 70 | + startColumn, |
| 71 | + line |
| 72 | + ), |
| 73 | + new ScriptPosition( |
| 74 | + ast.Extent.File, |
| 75 | + endLine, |
| 76 | + endColumn, |
| 77 | + line |
| 78 | + )); |
| 79 | + |
| 80 | + var record = new DiagnosticRecord( |
| 81 | + String.Format(CultureInfo.CurrentCulture, |
| 82 | + String.Format(Strings.AvoidLongLinesError, MaximumLineLength)), |
| 83 | + violationExtent, |
| 84 | + GetName(), |
| 85 | + GetDiagnosticSeverity(), |
| 86 | + ast.Extent.File, |
| 87 | + null |
| 88 | + ); |
| 89 | + diagnosticRecords.Add(record); |
| 90 | + } |
| 91 | + |
| 92 | + return diagnosticRecords; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Retrieves the common name of this rule. |
| 97 | + /// </summary> |
| 98 | + public override string GetCommonName() |
| 99 | + { |
| 100 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidLongLinesCommonName); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Retrieves the description of this rule. |
| 105 | + /// </summary> |
| 106 | + public override string GetDescription() |
| 107 | + { |
| 108 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidLongLinesDescription); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Retrieves the name of this rule. |
| 113 | + /// </summary> |
| 114 | + public override string GetName() |
| 115 | + { |
| 116 | + return string.Format( |
| 117 | + CultureInfo.CurrentCulture, |
| 118 | + Strings.NameSpaceFormat, |
| 119 | + GetSourceName(), |
| 120 | + Strings.AvoidLongLinesName); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Retrieves the severity of the rule: error, warning or information. |
| 125 | + /// </summary> |
| 126 | + public override RuleSeverity GetSeverity() |
| 127 | + { |
| 128 | + return RuleSeverity.Warning; |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Gets the severity of the returned diagnostic record: error, warning, or information. |
| 133 | + /// </summary> |
| 134 | + /// <returns></returns> |
| 135 | + public DiagnosticSeverity GetDiagnosticSeverity() |
| 136 | + { |
| 137 | + return DiagnosticSeverity.Warning; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Retrieves the name of the module/assembly the rule is from. |
| 142 | + /// </summary> |
| 143 | + public override string GetSourceName() |
| 144 | + { |
| 145 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Retrieves the type of the rule, Builtin, Managed or Module. |
| 150 | + /// </summary> |
| 151 | + public override SourceType GetSourceType() |
| 152 | + { |
| 153 | + return SourceType.Builtin; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments