|
6 | 6 | using System.Management.Automation.Language;
|
7 | 7 | using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
|
8 | 8 | using System.Management.Automation;
|
9 |
| -using System.IO; |
10 |
| -using System.Runtime.InteropServices; |
| 9 | +using System.Linq; |
11 | 10 | #if !CORECLR
|
12 | 11 | using System.ComponentModel.Composition;
|
13 | 12 | #endif
|
@@ -67,6 +66,30 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
|
67 | 66 | commandName,
|
68 | 67 | suggestedCorrections: GetCorrectionExtent(commandAst, correctlyCasedCommandName));
|
69 | 68 | }
|
| 69 | + |
| 70 | + var commandParameterAsts = commandAst.FindAll( |
| 71 | + testAst => testAst is CommandParameterAst, true).Cast<CommandParameterAst>(); |
| 72 | + var availableParameters = commandInfo.Parameters; |
| 73 | + foreach (var commandParameterAst in commandParameterAsts) |
| 74 | + { |
| 75 | + var parameterName = commandParameterAst.ParameterName; |
| 76 | + var parameterMetaData = availableParameters[parameterName]; |
| 77 | + if (parameterMetaData != null) |
| 78 | + { |
| 79 | + var correctlyCasedParameterName = parameterMetaData.Name; |
| 80 | + if (!parameterName.Equals(correctlyCasedParameterName, StringComparison.Ordinal)) |
| 81 | + { |
| 82 | + yield return new DiagnosticRecord( |
| 83 | + string.Format(CultureInfo.CurrentCulture, Strings.UseCorrectCasingError, commandName, parameterName), |
| 84 | + GetCommandExtent(commandAst), |
| 85 | + GetName(), |
| 86 | + DiagnosticSeverity.Warning, |
| 87 | + fileName, |
| 88 | + commandName, |
| 89 | + suggestedCorrections: GetCorrectionExtent(commandParameterAst, correctlyCasedParameterName)); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
70 | 93 | }
|
71 | 94 | }
|
72 | 95 |
|
@@ -109,6 +132,27 @@ private IEnumerable<CorrectionExtent> GetCorrectionExtent(CommandAst commandAst,
|
109 | 132 | yield return correction;
|
110 | 133 | }
|
111 | 134 |
|
| 135 | + private IEnumerable<CorrectionExtent> GetCorrectionExtent(CommandParameterAst commandParameterAst, string correctlyCaseName) |
| 136 | + { |
| 137 | + var description = string.Format( |
| 138 | + CultureInfo.CurrentCulture, |
| 139 | + Strings.UseCorrectCasingDescription, |
| 140 | + correctlyCaseName, |
| 141 | + correctlyCaseName); |
| 142 | + var cmdExtent = commandParameterAst.Extent; |
| 143 | + var correction = new CorrectionExtent( |
| 144 | + cmdExtent.StartLineNumber, |
| 145 | + cmdExtent.EndLineNumber, |
| 146 | + // +1 because of the dash before the parameter name |
| 147 | + cmdExtent.StartColumnNumber + 1, |
| 148 | + // do not use EndColumnNumber property as it would not cover the case where the colon syntax: -ParameterName:$ParameterValue |
| 149 | + cmdExtent.StartColumnNumber + 1 + commandParameterAst.ParameterName.Length, |
| 150 | + correctlyCaseName, |
| 151 | + commandParameterAst.Extent.File, |
| 152 | + description); |
| 153 | + yield return correction; |
| 154 | + } |
| 155 | + |
112 | 156 | /// <summary>
|
113 | 157 | /// GetName: Retrieves the name of this rule.
|
114 | 158 | /// </summary>
|
|
0 commit comments