@@ -68,6 +68,13 @@ internal class SA1305FieldNamesMustNotUseHungarianNotation : DiagnosticAnalyzer
6868
6969 private static readonly Action < CompilationStartAnalysisContext > CompilationStartAction = HandleCompilationStart ;
7070 private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > VariableDeclarationAction = Analyzer . HandleVariableDeclaration ;
71+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > CatchDeclarationAction = Analyzer . HandleCatchDeclaration ;
72+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > QueryContinuationAction = Analyzer . HandleQueryContinuation ;
73+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > FromClauseAction = Analyzer . HandleFromClause ;
74+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > LetClauseAction = Analyzer . HandleLetClause ;
75+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > JoinClauseAction = Analyzer . HandleJoinClause ;
76+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > JoinIntoClauseAction = Analyzer . HandleJoinIntoClause ;
77+ private static readonly Action < SyntaxNodeAnalysisContext , StyleCopSettings > ForEachStatementAction = Analyzer . HandleForEachStatement ;
7178
7279 /// <inheritdoc/>
7380 public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics { get ; } =
@@ -82,6 +89,13 @@ public override void Initialize(AnalysisContext context)
8289 private static void HandleCompilationStart ( CompilationStartAnalysisContext context )
8390 {
8491 context . RegisterSyntaxNodeActionHonorExclusions ( VariableDeclarationAction , SyntaxKind . VariableDeclaration ) ;
92+ context . RegisterSyntaxNodeActionHonorExclusions ( CatchDeclarationAction , SyntaxKind . CatchDeclaration ) ;
93+ context . RegisterSyntaxNodeActionHonorExclusions ( QueryContinuationAction , SyntaxKind . QueryContinuation ) ;
94+ context . RegisterSyntaxNodeActionHonorExclusions ( FromClauseAction , SyntaxKind . FromClause ) ;
95+ context . RegisterSyntaxNodeActionHonorExclusions ( LetClauseAction , SyntaxKind . LetClause ) ;
96+ context . RegisterSyntaxNodeActionHonorExclusions ( JoinClauseAction , SyntaxKind . JoinClause ) ;
97+ context . RegisterSyntaxNodeActionHonorExclusions ( JoinIntoClauseAction , SyntaxKind . JoinIntoClause ) ;
98+ context . RegisterSyntaxNodeActionHonorExclusions ( ForEachStatementAction , SyntaxKind . ForEachStatement ) ;
8599 }
86100
87101 private static class Analyzer
@@ -109,37 +123,77 @@ public static void HandleVariableDeclaration(SyntaxNodeAnalysisContext context,
109123 }
110124
111125 var identifier = variableDeclarator . Identifier ;
112- if ( identifier . IsMissing )
113- {
114- continue ;
115- }
126+ CheckIdentifier ( context , identifier , settings , fieldDeclaration ) ;
127+ }
128+ }
116129
117- string name = identifier . ValueText ;
118- if ( string . IsNullOrEmpty ( name ) )
119- {
120- continue ;
121- }
130+ public static void HandleCatchDeclaration ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
131+ {
132+ CheckIdentifier ( context , ( ( CatchDeclarationSyntax ) context . Node ) . Identifier , settings ) ;
133+ }
122134
123- var match = HungarianRegex . Match ( name ) ;
124- if ( ! match . Success )
125- {
126- continue ;
127- }
135+ public static void HandleQueryContinuation ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
136+ {
137+ CheckIdentifier ( context , ( ( QueryContinuationSyntax ) context . Node ) . Identifier , settings ) ;
138+ }
128139
129- var notationValue = match . Groups [ "notation" ] . Value ;
130- if ( settings . NamingRules . AllowCommonHungarianPrefixes && CommonPrefixes . Contains ( notationValue ) )
131- {
132- continue ;
133- }
140+ public static void HandleFromClause ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
141+ {
142+ CheckIdentifier ( context , ( ( FromClauseSyntax ) context . Node ) . Identifier , settings ) ;
143+ }
134144
135- if ( settings . NamingRules . AllowedHungarianPrefixes . Contains ( notationValue ) )
136- {
137- continue ;
138- }
145+ public static void HandleLetClause ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
146+ {
147+ CheckIdentifier ( context , ( ( LetClauseSyntax ) context . Node ) . Identifier , settings ) ;
148+ }
149+
150+ public static void HandleJoinClause ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
151+ {
152+ CheckIdentifier ( context , ( ( JoinClauseSyntax ) context . Node ) . Identifier , settings ) ;
153+ }
154+
155+ public static void HandleJoinIntoClause ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
156+ {
157+ CheckIdentifier ( context , ( ( JoinIntoClauseSyntax ) context . Node ) . Identifier , settings ) ;
158+ }
159+
160+ public static void HandleForEachStatement ( SyntaxNodeAnalysisContext context , StyleCopSettings settings )
161+ {
162+ CheckIdentifier ( context , ( ( ForEachStatementSyntax ) context . Node ) . Identifier , settings ) ;
163+ }
139164
140- // Variable names must begin with lower-case letter
141- context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , identifier . GetLocation ( ) , fieldDeclaration ? "field" : "variable" , name ) ) ;
165+ private static void CheckIdentifier ( SyntaxNodeAnalysisContext context , SyntaxToken identifier , StyleCopSettings settings , bool fieldDeclaration = false )
166+ {
167+ if ( identifier . IsMissing )
168+ {
169+ return ;
170+ }
171+
172+ string name = identifier . ValueText ;
173+ if ( string . IsNullOrEmpty ( name ) )
174+ {
175+ return ;
142176 }
177+
178+ var match = HungarianRegex . Match ( name ) ;
179+ if ( ! match . Success )
180+ {
181+ return ;
182+ }
183+
184+ var notationValue = match . Groups [ "notation" ] . Value ;
185+ if ( settings . NamingRules . AllowCommonHungarianPrefixes && CommonPrefixes . Contains ( notationValue ) )
186+ {
187+ return ;
188+ }
189+
190+ if ( settings . NamingRules . AllowedHungarianPrefixes . Contains ( notationValue ) )
191+ {
192+ return ;
193+ }
194+
195+ // Variable names must begin with lower-case letter
196+ context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , identifier . GetLocation ( ) , fieldDeclaration ? "field" : "variable" , name ) ) ;
143197 }
144198 }
145199 }
0 commit comments