Skip to content

Commit

Permalink
Revert "removed inspections... ...again?"
Browse files Browse the repository at this point in the history
This reverts commit 80a090a.
  • Loading branch information
retailcoder committed Dec 19, 2017
1 parent c9fd241 commit 583c1b6
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
@@ -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 Rubberduck.Inspections/Concrete/MissingAttributeInspection.cs
@@ -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.
}
}
}
}
}
2 changes: 2 additions & 0 deletions Rubberduck.Inspections/Rubberduck.Inspections.csproj
Expand Up @@ -91,11 +91,13 @@
<Compile Include="Concrete\ImplicitDefaultMemberAssignmentInspection.cs" />
<Compile Include="Concrete\ImplicitPublicMemberInspection.cs" />
<Compile Include="Concrete\ImplicitVariantReturnTypeInspection.cs" />
<Compile Include="Concrete\MissingAttributeInspection.cs" />
<Compile Include="Abstract\InspectionResultBase.cs" />
<Compile Include="Concrete\RedundantByRefModifierInspection.cs" />
<Compile Include="Concrete\EmptyElseBlockInspection.cs" />
<Compile Include="Inspector.cs" />
<Compile Include="Concrete\MemberNotOnInterfaceInspection.cs" />
<Compile Include="Concrete\MissingAnnotationArgumentInspection.cs" />
<Compile Include="Concrete\ModuleScopeDimKeywordInspection.cs" />
<Compile Include="Concrete\MoveFieldCloserToUsageInspection.cs" />
<Compile Include="Concrete\MultilineParameterInspection.cs" />
Expand Down

0 comments on commit 583c1b6

Please sign in to comment.