diff --git a/RuleDocumentation/UseCorrectCasing.md b/RuleDocumentation/UseCorrectCasing.md
index c0d7a68f7..60a93f262 100644
--- a/RuleDocumentation/UseCorrectCasing.md
+++ b/RuleDocumentation/UseCorrectCasing.md
@@ -4,24 +4,22 @@
## Description
-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.
+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.
## How
-Use exact casing of the cmdlet, e.g. `Invoke-Command`.
+Use exact casing of the cmdlet and its parameters, e.g. `Invoke-Command { 'foo' } -RunAsAdministrator`.
## Example
### Wrong
``` PowerShell
-invoke-command { 'foo' }
-}
+invoke-command { 'foo' } -runasadministrator
```
### Correct
``` PowerShell
-Invoke-Command { 'foo' }
-}
+Invoke-Command { 'foo' } -RunAsAdministrator
```
diff --git a/Rules/Strings.Designer.cs b/Rules/Strings.Designer.cs
index 133676ef0..c0cb02ba6 100644
--- a/Rules/Strings.Designer.cs
+++ b/Rules/Strings.Designer.cs
@@ -2473,7 +2473,7 @@ internal static string UseConsistentWhitespaceName {
}
///
- /// Looks up a localized string similar to Use exact casing of cmdlet/function name..
+ /// Looks up a localized string similar to Use exact casing of cmdlet/function/parameter name..
///
internal static string UseCorrectCasingCommonName {
get {
@@ -2482,7 +2482,7 @@ internal static string UseCorrectCasingCommonName {
}
///
- /// Looks up a localized string similar to For better readability and consistency, use the exact casing of the cmdlet/function..
+ /// Looks up a localized string similar to For better readability and consistency, use the exact casing of the cmdlet/function/parameter..
///
internal static string UseCorrectCasingDescription {
get {
@@ -2491,7 +2491,7 @@ internal static string UseCorrectCasingDescription {
}
///
- /// Looks up a localized string similar to Cmdlet/Function does not match its exact casing '{0}'..
+ /// Looks up a localized string similar to Cmdlet/Function/Parameter does not match its exact casing '{0}'..
///
internal static string UseCorrectCasingError {
get {
diff --git a/Rules/Strings.resx b/Rules/Strings.resx
index 89af4f09d..fc47fc6bd 100644
--- a/Rules/Strings.resx
+++ b/Rules/Strings.resx
@@ -1081,13 +1081,13 @@
Use space before pipe.
- Use exact casing of cmdlet/function name.
+ Use exact casing of cmdlet/function/parameter name.
- For better readability and consistency, use the exact casing of the cmdlet/function.
+ For better readability and consistency, use the exact casing of the cmdlet/function/parameter.
- Cmdlet/Function does not match its exact casing '{0}'.
+ Cmdlet/Function/Parameter does not match its exact casing '{0}'.
UseCorrectCasing
diff --git a/Rules/UseCorrectCasing.cs b/Rules/UseCorrectCasing.cs
index 6936e0d67..3e1b4b2fd 100644
--- a/Rules/UseCorrectCasing.cs
+++ b/Rules/UseCorrectCasing.cs
@@ -6,8 +6,7 @@
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
using System.Management.Automation;
-using System.IO;
-using System.Runtime.InteropServices;
+using System.Linq;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
@@ -67,6 +66,30 @@ public override IEnumerable AnalyzeScript(Ast ast, string file
commandName,
suggestedCorrections: GetCorrectionExtent(commandAst, correctlyCasedCommandName));
}
+
+ var commandParameterAsts = commandAst.FindAll(
+ testAst => testAst is CommandParameterAst, true).Cast();
+ var availableParameters = commandInfo.Parameters;
+ foreach (var commandParameterAst in commandParameterAsts)
+ {
+ var parameterName = commandParameterAst.ParameterName;
+ var parameterMetaData = availableParameters[parameterName];
+ if (parameterMetaData != null)
+ {
+ var correctlyCasedParameterName = parameterMetaData.Name;
+ if (!parameterName.Equals(correctlyCasedParameterName, StringComparison.Ordinal))
+ {
+ yield return new DiagnosticRecord(
+ string.Format(CultureInfo.CurrentCulture, Strings.UseCorrectCasingError, commandName, parameterName),
+ GetCommandExtent(commandAst),
+ GetName(),
+ DiagnosticSeverity.Warning,
+ fileName,
+ commandName,
+ suggestedCorrections: GetCorrectionExtent(commandParameterAst, correctlyCasedParameterName));
+ }
+ }
+ }
}
}
@@ -109,6 +132,27 @@ private IEnumerable GetCorrectionExtent(CommandAst commandAst,
yield return correction;
}
+ private IEnumerable GetCorrectionExtent(CommandParameterAst commandParameterAst, string correctlyCaseName)
+ {
+ var description = string.Format(
+ CultureInfo.CurrentCulture,
+ Strings.UseCorrectCasingDescription,
+ correctlyCaseName,
+ correctlyCaseName);
+ var cmdExtent = commandParameterAst.Extent;
+ var correction = new CorrectionExtent(
+ cmdExtent.StartLineNumber,
+ cmdExtent.EndLineNumber,
+ // +1 because of the dash before the parameter name
+ cmdExtent.StartColumnNumber + 1,
+ // do not use EndColumnNumber property as it would not cover the case where the colon syntax: -ParameterName:$ParameterValue
+ cmdExtent.StartColumnNumber + 1 + commandParameterAst.ParameterName.Length,
+ correctlyCaseName,
+ commandParameterAst.Extent.File,
+ description);
+ yield return correction;
+ }
+
///
/// GetName: Retrieves the name of this rule.
///
diff --git a/Tests/Rules/UseCorrectCasing.tests.ps1 b/Tests/Rules/UseCorrectCasing.tests.ps1
index ae716264c..968a93ae7 100644
--- a/Tests/Rules/UseCorrectCasing.tests.ps1
+++ b/Tests/Rules/UseCorrectCasing.tests.ps1
@@ -54,4 +54,15 @@ Describe "UseCorrectCasing" {
$scriptDefinition = ". $uncPath"
Invoke-Formatter $scriptDefinition | Should -Be $scriptDefinition
}
+
+ It "Corrects parameter casing" {
+ function Invoke-DummyFunction ($ParameterName) { }
+
+ Invoke-Formatter 'Invoke-DummyFunction -parametername $parameterValue' |
+ Should -Be 'Invoke-DummyFunction -ParameterName $parameterValue'
+ Invoke-Formatter 'Invoke-DummyFunction -parametername:$parameterValue' |
+ Should -Be 'Invoke-DummyFunction -ParameterName:$parameterValue'
+ Invoke-Formatter 'Invoke-DummyFunction -parametername: $parameterValue' |
+ Should -Be 'Invoke-DummyFunction -ParameterName: $parameterValue'
+ }
}