Skip to content

Commit f3f0a07

Browse files
authored
Correct casing of parameters as well in PSUseCorrectCasing formatter rule (#1391)
* Correct casing of parameters. TODO: diagnostic messages * Update message strings * update docs
1 parent 0300c4a commit f3f0a07

File tree

5 files changed

+67
-14
lines changed

5 files changed

+67
-14
lines changed

RuleDocumentation/UseCorrectCasing.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
44

55
## Description
66

7-
This is a style/formatting rule. PowerShell is case insensitive where applicable. The casing of cmdlet names does not matter but this rule ensures that the casing matches for consistency and also because most cmdlets start with an upper case and using that improves readability to the human eye.
7+
This is a style/formatting rule. PowerShell is case insensitive where applicable. The casing of cmdlet names or parameters does not matter but this rule ensures that the casing matches for consistency and also because most cmdlets/parameters start with an upper case and using that improves readability to the human eye.
88

99
## How
1010

11-
Use exact casing of the cmdlet, e.g. `Invoke-Command`.
11+
Use exact casing of the cmdlet and its parameters, e.g. `Invoke-Command { 'foo' } -RunAsAdministrator`.
1212

1313
## Example
1414

1515
### Wrong
1616

1717
``` PowerShell
18-
invoke-command { 'foo' }
19-
}
18+
invoke-command { 'foo' } -runasadministrator
2019
```
2120

2221
### Correct
2322

2423
``` PowerShell
25-
Invoke-Command { 'foo' }
26-
}
24+
Invoke-Command { 'foo' } -RunAsAdministrator
2725
```

Rules/Strings.Designer.cs

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rules/Strings.resx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1081,13 +1081,13 @@
10811081
<value>Use space before pipe.</value>
10821082
</data>
10831083
<data name="UseCorrectCasingCommonName" xml:space="preserve">
1084-
<value>Use exact casing of cmdlet/function name.</value>
1084+
<value>Use exact casing of cmdlet/function/parameter name.</value>
10851085
</data>
10861086
<data name="UseCorrectCasingDescription" xml:space="preserve">
1087-
<value>For better readability and consistency, use the exact casing of the cmdlet/function.</value>
1087+
<value>For better readability and consistency, use the exact casing of the cmdlet/function/parameter.</value>
10881088
</data>
10891089
<data name="UseCorrectCasingError" xml:space="preserve">
1090-
<value>Cmdlet/Function does not match its exact casing '{0}'.</value>
1090+
<value>Cmdlet/Function/Parameter does not match its exact casing '{0}'.</value>
10911091
</data>
10921092
<data name="UseCorrectCasingName" xml:space="preserve">
10931093
<value>UseCorrectCasing</value>

Rules/UseCorrectCasing.cs

+46-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using System.Management.Automation.Language;
77
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
88
using System.Management.Automation;
9-
using System.IO;
10-
using System.Runtime.InteropServices;
9+
using System.Linq;
1110
#if !CORECLR
1211
using System.ComponentModel.Composition;
1312
#endif
@@ -67,6 +66,30 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6766
commandName,
6867
suggestedCorrections: GetCorrectionExtent(commandAst, correctlyCasedCommandName));
6968
}
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+
}
7093
}
7194
}
7295

@@ -109,6 +132,27 @@ private IEnumerable<CorrectionExtent> GetCorrectionExtent(CommandAst commandAst,
109132
yield return correction;
110133
}
111134

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+
112156
/// <summary>
113157
/// GetName: Retrieves the name of this rule.
114158
/// </summary>

Tests/Rules/UseCorrectCasing.tests.ps1

+11
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,15 @@ Describe "UseCorrectCasing" {
5454
$scriptDefinition = ". $uncPath"
5555
Invoke-Formatter $scriptDefinition | Should -Be $scriptDefinition
5656
}
57+
58+
It "Corrects parameter casing" {
59+
function Invoke-DummyFunction ($ParameterName) { }
60+
61+
Invoke-Formatter 'Invoke-DummyFunction -parametername $parameterValue' |
62+
Should -Be 'Invoke-DummyFunction -ParameterName $parameterValue'
63+
Invoke-Formatter 'Invoke-DummyFunction -parametername:$parameterValue' |
64+
Should -Be 'Invoke-DummyFunction -ParameterName:$parameterValue'
65+
Invoke-Formatter 'Invoke-DummyFunction -parametername: $parameterValue' |
66+
Should -Be 'Invoke-DummyFunction -ParameterName: $parameterValue'
67+
}
5768
}

0 commit comments

Comments
 (0)