@@ -65,8 +65,6 @@ internal class SA1305FieldNamesMustNotUseHungarianNotation : DiagnosticAnalyzer
6565
6666 private static readonly Regex HungarianRegex = new Regex ( @"^(?<notation>[a-z]{1,2})[A-Z]" ) ;
6767
68- private static NamingSettings namingSettings ;
69-
7068 /// <inheritdoc/>
7169 public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics { get ; } =
7270 ImmutableArray . Create ( Descriptor ) ;
@@ -79,63 +77,74 @@ public override void Initialize(AnalysisContext context)
7977
8078 private static void HandleCompilationStart ( CompilationStartAnalysisContext context )
8179 {
82- namingSettings = context . Options . GetStyleCopSettings ( ) . NamingRules ;
83- context . RegisterSyntaxNodeActionHonorExclusions ( HandleVariableDeclarationSyntax , SyntaxKind . VariableDeclaration ) ;
80+ Analyzer analyzer = new Analyzer ( context . Options ) ;
81+ context . RegisterSyntaxNodeActionHonorExclusions ( analyzer . HandleVariableDeclarationSyntax , SyntaxKind . VariableDeclaration ) ;
8482 }
8583
86- private static void HandleVariableDeclarationSyntax ( SyntaxNodeAnalysisContext context )
84+ private sealed class Analyzer
8785 {
88- var syntax = ( VariableDeclarationSyntax ) context . Node ;
86+ private readonly NamingSettings namingSettings ;
8987
90- if ( syntax . Parent . IsKind ( SyntaxKind . EventFieldDeclaration ) )
88+ public Analyzer ( AnalyzerOptions options )
9189 {
92- return ;
90+ StyleCopSettings settings = options . GetStyleCopSettings ( ) ;
91+ this . namingSettings = settings . NamingRules ;
9392 }
9493
95- if ( NamedTypeHelpers . IsContainedInNativeMethodsClass ( syntax ) )
94+ public void HandleVariableDeclarationSyntax ( SyntaxNodeAnalysisContext context )
9695 {
97- return ;
98- }
96+ var syntax = ( VariableDeclarationSyntax ) context . Node ;
9997
100- var fieldDeclaration = syntax . Parent . IsKind ( SyntaxKind . FieldDeclaration ) ;
101- foreach ( var variableDeclarator in syntax . Variables )
102- {
103- if ( variableDeclarator == null )
98+ if ( syntax . Parent . IsKind ( SyntaxKind . EventFieldDeclaration ) )
10499 {
105- continue ;
100+ return ;
106101 }
107102
108- var identifier = variableDeclarator . Identifier ;
109- if ( identifier . IsMissing )
103+ if ( NamedTypeHelpers . IsContainedInNativeMethodsClass ( syntax ) )
110104 {
111- continue ;
105+ return ;
112106 }
113107
114- string name = identifier . ValueText ;
115- if ( string . IsNullOrEmpty ( name ) )
108+ var fieldDeclaration = syntax . Parent . IsKind ( SyntaxKind . FieldDeclaration ) ;
109+ foreach ( var variableDeclarator in syntax . Variables )
116110 {
117- continue ;
111+ if ( variableDeclarator == null )
112+ {
113+ continue ;
114+ }
115+
116+ var identifier = variableDeclarator . Identifier ;
117+ if ( identifier . IsMissing )
118+ {
119+ continue ;
120+ }
121+
122+ string name = identifier . ValueText ;
123+ if ( string . IsNullOrEmpty ( name ) )
124+ {
125+ continue ;
126+ }
127+
128+ var match = HungarianRegex . Match ( name ) ;
129+ if ( ! match . Success )
130+ {
131+ continue ;
132+ }
133+
134+ var notationValue = match . Groups [ "notation" ] . Value ;
135+ if ( this . namingSettings . AllowCommonHungarianPrefixes && CommonPrefixes . Contains ( notationValue ) )
136+ {
137+ continue ;
138+ }
139+
140+ if ( this . namingSettings . AllowedHungarianPrefixes . Contains ( notationValue ) )
141+ {
142+ continue ;
143+ }
144+
145+ // Variable names must begin with lower-case letter
146+ context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , identifier . GetLocation ( ) , fieldDeclaration ? "field" : "variable" , name ) ) ;
118147 }
119-
120- var match = HungarianRegex . Match ( name ) ;
121- if ( ! match . Success )
122- {
123- continue ;
124- }
125-
126- var notationValue = match . Groups [ "notation" ] . Value ;
127- if ( namingSettings . AllowCommonHungarianPrefixes && CommonPrefixes . Contains ( notationValue ) )
128- {
129- continue ;
130- }
131-
132- if ( namingSettings . AllowedHungarianPrefixes . Contains ( notationValue ) )
133- {
134- continue ;
135- }
136-
137- // Variable names must begin with lower-case letter
138- context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , identifier . GetLocation ( ) , fieldDeclaration ? "field" : "variable" , name ) ) ;
139148 }
140149 }
141150 }
0 commit comments