9
9
using Rubberduck . Parsing . Symbols ;
10
10
using Rubberduck . Resources . Inspections ;
11
11
using Rubberduck . Parsing . VBA ;
12
+ using Rubberduck . Parsing . VBA . DeclarationCaching ;
13
+ using Rubberduck . VBEditor ;
12
14
using Rubberduck . VBEditor . SafeComWrappers ;
13
15
14
16
namespace Rubberduck . Inspections . Concrete
@@ -53,27 +55,65 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
53
55
{
54
56
var finder = DeclarationFinderProvider . DeclarationFinder ;
55
57
56
- var userDeclarations = finder . AllUserDeclarations . ToList ( ) ;
57
- var identifierReferences = finder . AllIdentifierReferences ( ) . ToList ( ) ;
58
- var annotations = _state . AllAnnotations ;
58
+ return finder . UserDeclarations ( DeclarationType . Module )
59
+ . Where ( module => module != null )
60
+ . SelectMany ( module => DoGetInspectionResults ( module . QualifiedModuleName , finder ) ) ;
61
+ }
62
+
63
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module )
64
+ {
65
+ var finder = DeclarationFinderProvider . DeclarationFinder ;
66
+ return DoGetInspectionResults ( module , finder ) ;
67
+ }
68
+
69
+ private IEnumerable < IInspectionResult > DoGetInspectionResults ( QualifiedModuleName module , DeclarationFinder finder )
70
+ {
71
+ var userDeclarations = finder . Members ( module ) . ToList ( ) ;
72
+ var identifierReferences = finder . IdentifierReferences ( module ) . ToList ( ) ;
73
+ var annotations = _state . GetAnnotations ( module ) ;
59
74
60
75
var unboundAnnotations = UnboundAnnotations ( annotations , userDeclarations , identifierReferences )
61
76
. Where ( annotation => ! annotation . Annotation . Target . HasFlag ( AnnotationTarget . General )
62
77
|| annotation . AnnotatedLine == null ) ;
63
- var attributeAnnotationsInDocuments = AttributeAnnotationsInDocuments ( userDeclarations ) ;
64
78
65
79
var attributeAnnotationsOnDeclarationsNotAllowingAttributes = AttributeAnnotationsOnDeclarationsNotAllowingAttributes ( userDeclarations ) ;
66
80
67
81
var illegalAnnotations = unboundAnnotations
68
- . Concat ( attributeAnnotationsInDocuments )
69
82
. Concat ( attributeAnnotationsOnDeclarationsNotAllowingAttributes )
70
- . ToHashSet ( ) ;
83
+ . Distinct ( ) ;
84
+
85
+ if ( module . ComponentType == ComponentType . Document )
86
+ {
87
+ var attributeAnnotationsInDocuments = AttributeAnnotationsInDocuments ( userDeclarations ) ;
88
+ illegalAnnotations = illegalAnnotations
89
+ . Concat ( attributeAnnotationsInDocuments )
90
+ . Distinct ( ) ;
91
+ }
71
92
72
- return illegalAnnotations . Select ( annotation =>
73
- new QualifiedContextInspectionResult (
74
- this ,
75
- string . Format ( InspectionResults . IllegalAnnotationInspection , annotation . Context . annotationName ( ) . GetText ( ) ) ,
76
- new QualifiedContext ( annotation . QualifiedSelection . QualifiedName , annotation . Context ) ) ) ;
93
+ return illegalAnnotations
94
+ . Select ( InspectionResult )
95
+ . ToList ( ) ;
96
+ }
97
+
98
+ private IInspectionResult InspectionResult ( IParseTreeAnnotation pta )
99
+ {
100
+ return new QualifiedContextInspectionResult (
101
+ this ,
102
+ ResultDescription ( pta ) ,
103
+ Context ( pta ) ) ;
104
+ }
105
+
106
+ private static string ResultDescription ( IParseTreeAnnotation pta )
107
+ {
108
+ var annotationText = pta . Context . annotationName ( ) . GetText ( ) ;
109
+ return string . Format (
110
+ InspectionResults . IllegalAnnotationInspection ,
111
+ annotationText ) ;
112
+ }
113
+
114
+ private static QualifiedContext Context ( IParseTreeAnnotation pta )
115
+ {
116
+ return new QualifiedContext ( pta . QualifiedSelection . QualifiedName , pta . Context ) ;
77
117
}
78
118
79
119
private static IEnumerable < IParseTreeAnnotation > UnboundAnnotations ( IEnumerable < IParseTreeAnnotation > annotations , IEnumerable < Declaration > userDeclarations , IEnumerable < IdentifierReference > identifierReferences )
@@ -91,7 +131,8 @@ private static IEnumerable<IParseTreeAnnotation> AttributeAnnotationsInDocuments
91
131
{
92
132
var declarationsInDocuments = userDeclarations
93
133
. Where ( declaration => declaration . QualifiedModuleName . ComponentType == ComponentType . Document ) ;
94
- return declarationsInDocuments . SelectMany ( doc => doc . Annotations ) . Where ( pta => pta . Annotation is IAttributeAnnotation ) ;
134
+ return declarationsInDocuments . SelectMany ( doc => doc . Annotations )
135
+ . Where ( pta => pta . Annotation is IAttributeAnnotation ) ;
95
136
}
96
137
97
138
private static IEnumerable < IParseTreeAnnotation > AttributeAnnotationsOnDeclarationsNotAllowingAttributes ( IEnumerable < Declaration > userDeclarations )
0 commit comments