Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "removed inspections... ...again?"
This reverts commit 80a090a.
- Loading branch information
1 parent
c9fd241
commit 583c1b6
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
Rubberduck.Inspections/Concrete/MissingAnnotationArgumentInspection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Antlr4.Runtime; | ||
using Rubberduck.Inspections.Abstract; | ||
using Rubberduck.Inspections.Results; | ||
using Rubberduck.Parsing; | ||
using Rubberduck.Parsing.Annotations; | ||
using Rubberduck.Parsing.Grammar; | ||
using Rubberduck.Parsing.Inspections.Abstract; | ||
using Rubberduck.Parsing.Inspections.Resources; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.VBEditor; | ||
|
||
namespace Rubberduck.Inspections.Concrete | ||
{ | ||
public sealed class MissingAnnotationArgumentInspection : ParseTreeInspectionBase | ||
{ | ||
public MissingAnnotationArgumentInspection(RubberduckParserState state) | ||
: base(state, CodeInspectionSeverity.Error) { } | ||
|
||
public override CodeInspectionType InspectionType => CodeInspectionType.CodeQualityIssues; | ||
public override ParsePass Pass => ParsePass.AttributesPass; | ||
|
||
public override IInspectionListener Listener { get; } = | ||
new InvalidAnnotationStatementListener(); | ||
|
||
protected override IEnumerable<IInspectionResult> DoGetInspectionResults() | ||
{ | ||
return (from result in Listener.Contexts | ||
let context = (VBAParser.AnnotationContext)result.Context | ||
where context.annotationName().GetText() == AnnotationType.Ignore.ToString() | ||
|| context.annotationName().GetText() == AnnotationType.Folder.ToString() | ||
where context.annotationArgList() == null | ||
select new QualifiedContextInspectionResult(this, | ||
string.Format(InspectionsUI.MissingAnnotationArgumentInspectionResultFormat, | ||
((VBAParser.AnnotationContext)result.Context).annotationName().GetText()), | ||
result)); | ||
} | ||
|
||
public class InvalidAnnotationStatementListener : VBAParserBaseListener, IInspectionListener | ||
{ | ||
private readonly List<QualifiedContext<ParserRuleContext>> _contexts = new List<QualifiedContext<ParserRuleContext>>(); | ||
public IReadOnlyList<QualifiedContext<ParserRuleContext>> Contexts => _contexts; | ||
|
||
public QualifiedModuleName CurrentModuleName { get; set; } | ||
|
||
public void ClearContexts() | ||
{ | ||
_contexts.Clear(); | ||
} | ||
|
||
public override void ExitAnnotation(VBAParser.AnnotationContext context) | ||
{ | ||
if (context.annotationName() != null) | ||
{ | ||
_contexts.Add(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context)); | ||
} | ||
} | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
Rubberduck.Inspections/Concrete/MissingAttributeInspection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Antlr4.Runtime; | ||
using Rubberduck.Inspections.Abstract; | ||
using Rubberduck.Inspections.Results; | ||
using Rubberduck.Parsing; | ||
using Rubberduck.Parsing.Annotations; | ||
using Rubberduck.Parsing.Grammar; | ||
using Rubberduck.Parsing.Inspections; | ||
using Rubberduck.Parsing.Inspections.Abstract; | ||
using Rubberduck.Parsing.Inspections.Resources; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
|
||
namespace Rubberduck.Inspections.Concrete | ||
{ | ||
[CannotAnnotate] | ||
public sealed class MissingAttributeInspection : ParseTreeInspectionBase | ||
{ | ||
public MissingAttributeInspection(RubberduckParserState state) | ||
: base(state) | ||
{ | ||
Listener = new MissingMemberAttributeListener(state); | ||
} | ||
|
||
public override ParsePass Pass => ParsePass.AttributesPass; | ||
|
||
public override CodeInspectionType InspectionType => CodeInspectionType.RubberduckOpportunities; | ||
public override IInspectionListener Listener { get; } | ||
|
||
protected override IEnumerable<IInspectionResult> DoGetInspectionResults() | ||
{ | ||
return Listener.Contexts.Select(context => | ||
{ | ||
var name = string.Format(InspectionsUI.MissingAttributeInspectionResultFormat, context.MemberName.MemberName, | ||
((VBAParser.AnnotationContext) context.Context).annotationName().GetText()); | ||
return new QualifiedContextInspectionResult(this, name, context); | ||
}); | ||
} | ||
|
||
public class MissingMemberAttributeListener : ParseTreeListeners.AttributeAnnotationListener | ||
{ | ||
public MissingMemberAttributeListener(RubberduckParserState state) : base(state) { } | ||
|
||
public override void ExitAnnotation(VBAParser.AnnotationContext context) | ||
{ | ||
var annotationType = context.AnnotationType; | ||
|
||
if (!annotationType.HasFlag(AnnotationType.Attribute)) | ||
{ | ||
return; | ||
} | ||
|
||
var isMemberAnnotation = annotationType.HasFlag(AnnotationType.MemberAnnotation); | ||
var isModuleScope = CurrentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Module); | ||
|
||
if (isModuleScope && !isMemberAnnotation) | ||
{ | ||
// module-level annotation | ||
var module = State.DeclarationFinder.UserDeclarations(DeclarationType.Module).Single(m => m.QualifiedName.QualifiedModuleName.Equals(CurrentModuleName)); | ||
if (!module.Attributes.HasAttributeFor(context.AnnotationType)) | ||
{ | ||
AddContext(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context)); | ||
} | ||
} | ||
else if (isMemberAnnotation) | ||
{ | ||
// member-level annotation is above the context for the first member in the module.. | ||
if (isModuleScope) | ||
{ | ||
CurrentScopeDeclaration = FirstMember; | ||
} | ||
|
||
var member = Members.Value.Single(m => m.Key.Equals(CurrentScopeDeclaration.QualifiedName.MemberName)); | ||
if (!member.Value.Attributes.HasAttributeFor(context.AnnotationType, member.Key)) | ||
{ | ||
AddContext(new QualifiedContext<ParserRuleContext>(CurrentModuleName, context)); | ||
} | ||
} | ||
else | ||
{ | ||
// annotation is illegal. ignore. | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters