Skip to content

Commit 583c1b6

Browse files
committed
Revert "removed inspections... ...again?"
This reverts commit 80a090a.
1 parent c9fd241 commit 583c1b6

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Antlr4.Runtime;
4+
using Rubberduck.Inspections.Abstract;
5+
using Rubberduck.Inspections.Results;
6+
using Rubberduck.Parsing;
7+
using Rubberduck.Parsing.Annotations;
8+
using Rubberduck.Parsing.Grammar;
9+
using Rubberduck.Parsing.Inspections.Abstract;
10+
using Rubberduck.Parsing.Inspections.Resources;
11+
using Rubberduck.Parsing.VBA;
12+
using Rubberduck.VBEditor;
13+
14+
namespace Rubberduck.Inspections.Concrete
15+
{
16+
public sealed class MissingAnnotationArgumentInspection : ParseTreeInspectionBase
17+
{
18+
public MissingAnnotationArgumentInspection(RubberduckParserState state)
19+
: base(state, CodeInspectionSeverity.Error) { }
20+
21+
public override CodeInspectionType InspectionType => CodeInspectionType.CodeQualityIssues;
22+
public override ParsePass Pass => ParsePass.AttributesPass;
23+
24+
public override IInspectionListener Listener { get; } =
25+
new InvalidAnnotationStatementListener();
26+
27+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
28+
{
29+
return (from result in Listener.Contexts
30+
let context = (VBAParser.AnnotationContext)result.Context
31+
where context.annotationName().GetText() == AnnotationType.Ignore.ToString()
32+
|| context.annotationName().GetText() == AnnotationType.Folder.ToString()
33+
where context.annotationArgList() == null
34+
select new QualifiedContextInspectionResult(this,
35+
string.Format(InspectionsUI.MissingAnnotationArgumentInspectionResultFormat,
36+
((VBAParser.AnnotationContext)result.Context).annotationName().GetText()),
37+
result));
38+
}
39+
40+
public class InvalidAnnotationStatementListener : VBAParserBaseListener, IInspectionListener
41+
{
42+
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>();
43+
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts;
44+
45+
public QualifiedModuleName CurrentModuleName { get; set; }
46+
47+
public void ClearContexts()
48+
{
49+
_contexts.Clear();
50+
}
51+
52+
public override void ExitAnnotation(VBAParser.AnnotationContext context)
53+
{
54+
if (context.annotationName() != null)
55+
{
56+
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
57+
}
58+
}
59+
}
60+
}
61+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Antlr4.Runtime;
4+
using Rubberduck.Inspections.Abstract;
5+
using Rubberduck.Inspections.Results;
6+
using Rubberduck.Parsing;
7+
using Rubberduck.Parsing.Annotations;
8+
using Rubberduck.Parsing.Grammar;
9+
using Rubberduck.Parsing.Inspections;
10+
using Rubberduck.Parsing.Inspections.Abstract;
11+
using Rubberduck.Parsing.Inspections.Resources;
12+
using Rubberduck.Parsing.Symbols;
13+
using Rubberduck.Parsing.VBA;
14+
15+
namespace Rubberduck.Inspections.Concrete
16+
{
17+
[CannotAnnotate]
18+
public sealed class MissingAttributeInspection : ParseTreeInspectionBase
19+
{
20+
public MissingAttributeInspection(RubberduckParserState state)
21+
: base(state)
22+
{
23+
Listener = new MissingMemberAttributeListener(state);
24+
}
25+
26+
public override ParsePass Pass => ParsePass.AttributesPass;
27+
28+
public override CodeInspectionType InspectionType => CodeInspectionType.RubberduckOpportunities;
29+
public override IInspectionListener Listener { get; }
30+
31+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
32+
{
33+
return Listener.Contexts.Select(context =>
34+
{
35+
var name = string.Format(InspectionsUI.MissingAttributeInspectionResultFormat, context.MemberName.MemberName,
36+
((VBAParser.AnnotationContext) context.Context).annotationName().GetText());
37+
return new QualifiedContextInspectionResult(this, name, context);
38+
});
39+
}
40+
41+
public class MissingMemberAttributeListener : ParseTreeListeners.AttributeAnnotationListener
42+
{
43+
public MissingMemberAttributeListener(RubberduckParserState state) : base(state) { }
44+
45+
public override void ExitAnnotation(VBAParser.AnnotationContext context)
46+
{
47+
var annotationType = context.AnnotationType;
48+
49+
if (!annotationType.HasFlag(AnnotationType.Attribute))
50+
{
51+
return;
52+
}
53+
54+
var isMemberAnnotation = annotationType.HasFlag(AnnotationType.MemberAnnotation);
55+
var isModuleScope = CurrentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Module);
56+
57+
if (isModuleScope && !isMemberAnnotation)
58+
{
59+
// module-level annotation
60+
var module = State.DeclarationFinder.UserDeclarations(DeclarationType.Module).Single(m => m.QualifiedName.QualifiedModuleName.Equals(CurrentModuleName));
61+
if (!module.Attributes.HasAttributeFor(context.AnnotationType))
62+
{
63+
AddContext(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
64+
}
65+
}
66+
else if (isMemberAnnotation)
67+
{
68+
// member-level annotation is above the context for the first member in the module..
69+
if (isModuleScope)
70+
{
71+
CurrentScopeDeclaration = FirstMember;
72+
}
73+
74+
var member = Members.Value.Single(m => m.Key.Equals(CurrentScopeDeclaration.QualifiedName.MemberName));
75+
if (!member.Value.Attributes.HasAttributeFor(context.AnnotationType, member.Key))
76+
{
77+
AddContext(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context));
78+
}
79+
}
80+
else
81+
{
82+
// annotation is illegal. ignore.
83+
}
84+
}
85+
}
86+
}
87+
}

Rubberduck.Inspections/Rubberduck.Inspections.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@
9191
<Compile Include="Concrete\ImplicitDefaultMemberAssignmentInspection.cs" />
9292
<Compile Include="Concrete\ImplicitPublicMemberInspection.cs" />
9393
<Compile Include="Concrete\ImplicitVariantReturnTypeInspection.cs" />
94+
<Compile Include="Concrete\MissingAttributeInspection.cs" />
9495
<Compile Include="Abstract\InspectionResultBase.cs" />
9596
<Compile Include="Concrete\RedundantByRefModifierInspection.cs" />
9697
<Compile Include="Concrete\EmptyElseBlockInspection.cs" />
9798
<Compile Include="Inspector.cs" />
9899
<Compile Include="Concrete\MemberNotOnInterfaceInspection.cs" />
100+
<Compile Include="Concrete\MissingAnnotationArgumentInspection.cs" />
99101
<Compile Include="Concrete\ModuleScopeDimKeywordInspection.cs" />
100102
<Compile Include="Concrete\MoveFieldCloserToUsageInspection.cs" />
101103
<Compile Include="Concrete\MultilineParameterInspection.cs" />

0 commit comments

Comments
 (0)