@@ -5,6 +5,8 @@ namespace StyleCop.Analyzers.MaintainabilityRules
55{
66 using System ;
77 using System . Collections . Immutable ;
8+ using System . IO ;
9+ using System . Linq ;
810 using Microsoft . CodeAnalysis ;
911 using Microsoft . CodeAnalysis . CSharp ;
1012 using Microsoft . CodeAnalysis . CSharp . Syntax ;
@@ -60,37 +62,35 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
6062 private static void HandleSyntaxTree ( SyntaxTreeAnalysisContext context )
6163 {
6264 var syntaxRoot = context . Tree . GetRoot ( context . CancellationToken ) ;
63-
6465 var descentNodes = syntaxRoot . DescendantNodes ( descendIntoChildren : node => node != null && ! node . IsKind ( SyntaxKind . ClassDeclaration ) ) ;
66+ var classNodes = from descentNode in descentNodes
67+ where descentNode . IsKind ( SyntaxKind . ClassDeclaration )
68+ select descentNode as ClassDeclarationSyntax ;
6569
66- string foundClassName = null ;
67- bool isPartialClass = false ;
70+ var preferredClassNode = classNodes . FirstOrDefault ( n => n . Identifier . Text == Path . GetFileNameWithoutExtension ( context . Tree . FilePath ) ) ?? classNodes . FirstOrDefault ( ) ;
6871
69- foreach ( var node in descentNodes )
72+ if ( preferredClassNode != null )
7073 {
71- if ( node . IsKind ( SyntaxKind . ClassDeclaration ) )
74+ string foundClassName = null ;
75+ bool isPartialClass = false ;
76+
77+ foundClassName = preferredClassNode . Identifier . Text ;
78+ isPartialClass = preferredClassNode . Modifiers . Any ( SyntaxKind . PartialKeyword ) ;
79+
80+ foreach ( var classNode in classNodes )
7281 {
73- ClassDeclarationSyntax classDeclaration = node as ClassDeclarationSyntax ;
74- if ( foundClassName != null )
82+ if ( classNode == preferredClassNode || ( isPartialClass && foundClassName == classNode . Identifier . Text ) )
7583 {
76- if ( isPartialClass && foundClassName == classDeclaration . Identifier . Text )
77- {
78- continue ;
79- }
80-
81- var location = NamedTypeHelpers . GetNameOrIdentifierLocation ( node ) ;
82- if ( location != null )
83- {
84- context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , location ) ) ;
85- }
84+ continue ;
8685 }
87- else
86+
87+ var location = NamedTypeHelpers . GetNameOrIdentifierLocation ( classNode ) ;
88+ if ( location != null )
8889 {
89- foundClassName = classDeclaration . Identifier . Text ;
90- isPartialClass = classDeclaration . Modifiers . Any ( SyntaxKind . PartialKeyword ) ;
90+ context . ReportDiagnostic ( Diagnostic . Create ( Descriptor , location ) ) ;
9191 }
9292 }
9393 }
9494 }
9595 }
96- }
96+ }
0 commit comments