Skip to content

Commit 68336bd

Browse files
Jaykulrjmholt
andauthored
Apply suggestions from code review
Co-authored-by: Robert Holt <[email protected]>
1 parent df1e30d commit 68336bd

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

RuleDocumentation/UseConsistentCasing.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
## Description
66

7-
This is a style/formatting rule. PowerShell is case insensitive where applicable. The casing of keywords and operators does not matter but this rule ensures the use of lowercase for consistency and also because it helps distinguish keywords from commands (e.g. `foreach` is a keyword, but `ForEach-Object` is the command) and parameters (which should start with a capital letter).
7+
Prescribes lowercase characters for PowerShell keywords and operators.
8+
PowerShell is case-insensitive for keywords and operators,
9+
however maintaining a single casing convention aids consistency,
10+
and using lowercase helps to distinguish keywords from commands
11+
and operators from parameters.
12+
For example, this rule ensures the keyword `foreach` is distinguishable from the `ForEach-Object` command.
813

914
## How
1015

Rules/Strings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,4 +1167,4 @@
11671167
<data name="InvalidSyntaxAroundProcessBlockError" xml:space="preserve">
11681168
<value>When using an explicit process block, no preceding code is allowed, only begin, end and dynamicparams blocks.</value>
11691169
</data>
1170-
</root>
1170+
</root>

Rules/UseConsistentCasing.cs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2020
#if !CORECLR
2121
[Export(typeof(IScriptRule))]
2222
#endif
23-
2423
public class UseConsistentCasing : ConfigurableRule
2524
{
2625
/// <summary>If true, require the case of all operators to match the specified casing.</summary>
27-
[ConfigurableRuleProperty(defaultValue: "true")]
26+
[ConfigurableRuleProperty(defaultValue: true)]
2827
public bool CheckOperator { get; set; }
2928

3029
/// <summary>If true, require the case of all keywords to match the specified casing.</summary>
31-
[ConfigurableRuleProperty(defaultValue: "true")]
30+
[ConfigurableRuleProperty(defaultValue: true)]
3231
public bool CheckKeyword { get; set; }
3332

3433
/*
@@ -60,7 +59,10 @@ public string Case
6059
/// </summary>
6160
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6261
{
63-
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
62+
if (ast is null)
63+
{
64+
throw new ArgumentNullException(Strings.NullAstErrorMessage);
65+
}
6466

6567
Token[] tokens;
6668
ParseError[] errors;
@@ -70,10 +72,17 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
7072
Parser.ParseInput(ast.Extent.Text, out tokens, out errors);
7173

7274
// Iterates all keywords and check the case
73-
foreach (Token keyword in tokens.Where(t => t.TokenFlags.HasFlag(TokenFlags.Keyword)))
75+
for (int i = 0; i < Helper.Tokens.Length; i++)
7476
{
75-
string correctCase = keyword.Text.ToLowerInvariant();
76-
if (!keyword.Text.Equals(correctCase, StringComparison.Ordinal))
77+
Token currToken = Helper.Tokens[i];
78+
79+
if ((currToken.TokenFlags & TokenFlags.Keyword) == 0)
80+
{
81+
continue;
82+
}
83+
84+
string correctCase = currToken.Text.ToLowerInvariant();
85+
if (!currToken.Text.Equals(correctCase, StringComparison.Ordinal))
7786
{
7887
yield return GetDiagnosticRecord(keyword, fileName, correctCase);
7988
}
@@ -85,39 +94,42 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
8594
foreach (ExpressionAst oper in ast.FindAll(a => a is UnaryExpressionAst || a is BinaryExpressionAst, true))
8695
{
8796
// but handle them separately
88-
UnaryExpressionAst unary = oper as UnaryExpressionAst;
89-
if (unary != null) {
90-
string correctCase = "-" + unary.TokenKind.ToString().ToLowerInvariant();
91-
string actualCase = unary.Extent.Text.Substring(0, correctCase.Length);
92-
if (!actualCase.Equals(correctCase, StringComparison.Ordinal))
93-
{
94-
yield return GetDiagnosticRecord(unary, fileName, correctCase);
95-
}
96-
}
97-
else
97+
switch (oper)
9898
{
99-
BinaryExpressionAst binary = oper as BinaryExpressionAst;
100-
string correctCase = binary.ErrorPosition.Text.ToLowerInvariant();
101-
if (!binary.ErrorPosition.Text.Equals(correctCase, StringComparison.Ordinal))
102-
{
103-
yield return GetDiagnosticRecord(binary, fileName, correctCase);
104-
}
99+
case UnaryExpressionAst unary:
100+
string correctCase = "-" + unary.TokenKind.ToString().ToLowerInvariant();
101+
string actualCase = unary.Extent.Text.Substring(0, correctCase.Length);
102+
if (!actualCase.Equals(correctCase, StringComparison.Ordinal))
103+
{
104+
yield return GetDiagnosticRecord(unary, fileName, correctCase);
105+
}
106+
continue;
107+
108+
case BinaryExpressionAst binary:
109+
string correctCase = binary.ErrorPosition.Text.ToLowerInvariant();
110+
if (!binary.ErrorPosition.Text.Equals(correctCase, StringComparison.Ordinal))
111+
{
112+
yield return GetDiagnosticRecord(binary, fileName, correctCase);
113+
}
114+
continue;
105115
}
106116
}
107117
}
108118
}
109119

110120
private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, string correction)
111121
{
112-
var extents = new CorrectionExtent[] {
122+
var extents = new[]
123+
{
113124
new CorrectionExtent(
114125
token.Extent.StartLineNumber,
115126
token.Extent.EndLineNumber,
116127
token.Extent.StartColumnNumber,
117128
token.Extent.EndColumnNumber,
118129
correction,
119130
token.Extent.File,
120-
GetDescription())};
131+
GetDescription())
132+
};
121133

122134
return new DiagnosticRecord(
123135
string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentCasingKeywordError, token.Text, correction),

0 commit comments

Comments
 (0)