@@ -63,61 +63,35 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
63
63
{
64
64
throw new ArgumentNullException ( Strings . NullAstErrorMessage ) ;
65
65
}
66
+ TokenFlags operators = TokenFlags . BinaryOperator | TokenFlags . UnaryOperator ;
66
67
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 ] ;
73
72
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 ) )
76
74
{
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 ) )
86
77
{
87
- yield return GetDiagnosticRecord ( keyword , fileName , correctCase ) ;
78
+ yield return GetDiagnosticRecord ( token , fileName , correctCase , Strings . UseConsistentCasingKeywordError ) ;
88
79
}
80
+ continue ;
89
81
}
90
- }
91
82
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 ) )
95
84
{
96
- // but handle them separately
97
- switch ( oper )
85
+ string correctCase = token . Text . ToLowerInvariant ( ) ;
86
+ if ( ! token . Text . Equals ( correctCase , StringComparison . Ordinal ) )
98
87
{
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 ) ;
115
89
}
116
90
}
117
91
}
118
92
}
119
93
120
- private DiagnosticRecord GetDiagnosticRecord ( Token token , string fileName , string correction )
94
+ private DiagnosticRecord GetDiagnosticRecord ( Token token , string fileName , string correction , string message )
121
95
{
122
96
var extents = new [ ]
123
97
{
@@ -132,7 +106,7 @@ private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, strin
132
106
} ;
133
107
134
108
return new DiagnosticRecord (
135
- string . Format ( CultureInfo . CurrentCulture , Strings . UseConsistentCasingKeywordError , token . Text , correction ) ,
109
+ string . Format ( CultureInfo . CurrentCulture , message , token . Text , correction ) ,
136
110
token . Extent ,
137
111
GetName ( ) ,
138
112
DiagnosticSeverity . Information ,
@@ -141,56 +115,6 @@ private DiagnosticRecord GetDiagnosticRecord(Token token, string fileName, strin
141
115
suggestedCorrections : extents ) ;
142
116
}
143
117
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
-
194
118
195
119
/// <summary>
196
120
/// GetName: Retrieves the name of this rule.
0 commit comments