Skip to content

Commit fa3a58a

Browse files
committed
Switch back to using Tokens for everything
1 parent c08c0a3 commit fa3a58a

File tree

1 file changed

+16
-92
lines changed

1 file changed

+16
-92
lines changed

Rules/UseConsistentCasing.cs

Lines changed: 16 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -63,61 +63,35 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
6363
{
6464
throw new ArgumentNullException(Strings.NullAstErrorMessage);
6565
}
66+
TokenFlags operators = TokenFlags.BinaryOperator | TokenFlags.UnaryOperator;
6667

67-
Token[] tokens;
68-
ParseError[] errors;
69-
70-
if (CheckKeyword) {
71-
// sorry, but we had to re-parse, because the AST doesn't give us keywords...
72-
Parser.ParseInput(ast.Extent.Text, out tokens, out errors);
68+
// Iterates all keywords and check the case
69+
for (int i = 0; i < Helper.Instance.Tokens.Length; i++)
70+
{
71+
Token token = Helper.Instance.Tokens[i];
7372

74-
// Iterates all keywords and check the case
75-
for (int i = 0; i < Helper.Tokens.Length; i++)
73+
if (CheckKeyword && ((token.TokenFlags & TokenFlags.Keyword) != 0))
7674
{
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))
75+
string correctCase = token.Text.ToLowerInvariant();
76+
if (!token.Text.Equals(correctCase, StringComparison.Ordinal))
8677
{
87-
yield return GetDiagnosticRecord(keyword, fileName, correctCase);
78+
yield return GetDiagnosticRecord(token, fileName, correctCase, Strings.UseConsistentCasingKeywordError);
8879
}
80+
continue;
8981
}
90-
}
9182

92-
if (CheckOperator) {
93-
// find them all at once for performance reasons
94-
foreach (ExpressionAst oper in ast.FindAll(a => a is UnaryExpressionAst || a is BinaryExpressionAst, true))
83+
if (CheckOperator && ((token.TokenFlags & operators) != 0))
9584
{
96-
// but handle them separately
97-
switch (oper)
85+
string correctCase = token.Text.ToLowerInvariant();
86+
if (!token.Text.Equals(correctCase, StringComparison.Ordinal))
9887
{
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;
88+
yield return GetDiagnosticRecord(token, fileName, correctCase, Strings.UseConsistentCasingOperatorError);
11589
}
11690
}
11791
}
11892
}
11993

120-
private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, string correction)
94+
private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, string correction, string message)
12195
{
12296
var extents = new[]
12397
{
@@ -132,7 +106,7 @@ private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, strin
132106
};
133107

134108
return new DiagnosticRecord(
135-
string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentCasingKeywordError, token.Text, correction),
109+
string.Format(CultureInfo.CurrentCulture, message, token.Text, correction),
136110
token.Extent,
137111
GetName(),
138112
DiagnosticSeverity.Information,
@@ -141,56 +115,6 @@ private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, strin
141115
suggestedCorrections: extents);
142116
}
143117

144-
private DiagnosticRecord GetDiagnosticRecord(UnaryExpressionAst unary, string fileName, string correction)
145-
{
146-
var extent = new ScriptExtent(
147-
new ScriptPosition(
148-
unary.Extent.StartScriptPosition.File,
149-
unary.Extent.StartScriptPosition.LineNumber,
150-
unary.Extent.StartScriptPosition.ColumnNumber,
151-
unary.Extent.StartScriptPosition.Line),
152-
new ScriptPosition(
153-
unary.Extent.StartScriptPosition.File,
154-
unary.Extent.StartLineNumber,
155-
unary.Extent.StartColumnNumber + unary.TokenKind.ToString().Length + 1, // add one for the -
156-
unary.Extent.StartScriptPosition.Line));
157-
158-
var extents = new CorrectionExtent[] {
159-
new CorrectionExtent(
160-
extent,
161-
correction,
162-
fileName,
163-
GetDescription())};
164-
165-
return new DiagnosticRecord(
166-
string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentCasingOperatorError, extent.Text, correction),
167-
extent,
168-
GetName(),
169-
DiagnosticSeverity.Information,
170-
fileName,
171-
correction,
172-
suggestedCorrections: extents);
173-
}
174-
175-
private DiagnosticRecord GetDiagnosticRecord(BinaryExpressionAst binary, string fileName, string correction)
176-
{
177-
var extents = new CorrectionExtent[] {
178-
new CorrectionExtent(
179-
binary.ErrorPosition,
180-
correction,
181-
fileName,
182-
GetDescription())};
183-
184-
return new DiagnosticRecord(
185-
string.Format(CultureInfo.CurrentCulture, Strings.UseConsistentCasingOperatorError, binary.ErrorPosition.Text, correction),
186-
binary.ErrorPosition,
187-
GetName(),
188-
DiagnosticSeverity.Information,
189-
fileName,
190-
correction,
191-
suggestedCorrections: extents);
192-
}
193-
194118

195119
/// <summary>
196120
/// GetName: Retrieves the name of this rule.

0 commit comments

Comments
 (0)