1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using Rubberduck . Inspections . Abstract ;
4
+ using Rubberduck . Inspections . Results ;
5
+ using Rubberduck . Parsing ;
6
+ using Rubberduck . Parsing . Annotations ;
7
+ using Rubberduck . Parsing . Inspections ;
8
+ using Rubberduck . Parsing . Inspections . Abstract ;
9
+ using Rubberduck . Parsing . Symbols ;
10
+ using Rubberduck . Parsing . VBA ;
11
+ using Rubberduck . Resources . Inspections ;
12
+
13
+ namespace Rubberduck . Inspections . Concrete
14
+ {
15
+ [ CannotAnnotate ]
16
+ public sealed class AttributeValueOutOfSyncInspection : InspectionBase
17
+ {
18
+ public AttributeValueOutOfSyncInspection ( RubberduckParserState state )
19
+ : base ( state )
20
+ {
21
+ }
22
+
23
+ protected override IEnumerable < IInspectionResult > DoGetInspectionResults ( )
24
+ {
25
+ var declarationsWithAttributeAnnotations = State . DeclarationFinder . AllUserDeclarations
26
+ . Where ( declaration => declaration . Annotations . Any ( annotation => annotation . AnnotationType . HasFlag ( AnnotationType . Attribute ) ) ) ;
27
+ var results = new List < DeclarationInspectionResult > ( ) ;
28
+ foreach ( var declaration in declarationsWithAttributeAnnotations )
29
+ {
30
+ foreach ( var annotation in declaration . Annotations . OfType < IAttributeAnnotation > ( ) )
31
+ {
32
+ if ( HasDifferingAttributeValues ( declaration , annotation , out var attributeValues ) )
33
+ {
34
+ var description = string . Format ( InspectionResults . AttributeValueOutOfSyncInspection ,
35
+ annotation . Attribute ,
36
+ string . Join ( ", " , attributeValues ) ,
37
+ annotation . AnnotationType ) ;
38
+
39
+ var result = new DeclarationInspectionResult ( this , description , declaration ,
40
+ new QualifiedContext ( declaration . QualifiedModuleName , annotation . Context ) ) ;
41
+ result . Properties . Annotation = annotation ;
42
+ result . Properties . AttributeValues = attributeValues ;
43
+
44
+ results . Add ( result ) ;
45
+ }
46
+ }
47
+ }
48
+
49
+ return results ;
50
+ }
51
+
52
+ private static bool HasDifferingAttributeValues ( Declaration declaration , IAttributeAnnotation annotation , out IReadOnlyList < string > attributeValues )
53
+ {
54
+ var attributeNodes = declaration . DeclarationType . HasFlag ( DeclarationType . Module )
55
+ ? declaration . Attributes . AttributeNodesFor ( annotation )
56
+ : declaration . Attributes . AttributeNodesFor ( annotation , declaration . IdentifierName ) ;
57
+
58
+ foreach ( var attributeNode in attributeNodes )
59
+ {
60
+ var values = attributeNode . Values ;
61
+ if ( ! annotation . AttributeValues . SequenceEqual ( values ) )
62
+ {
63
+ attributeValues = values ;
64
+ return true ;
65
+ }
66
+ }
67
+ attributeValues = new List < string > ( ) ;
68
+ return false ;
69
+ }
70
+ }
71
+ }
0 commit comments