Skip to content

Commit 29ff445

Browse files
authored
Merge pull request #2569 from retailcoder/next
DeclarationFinder lives on
2 parents 64cae5d + 2f021db commit 29ff445

File tree

64 files changed

+509
-13847
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+509
-13847
lines changed

RetailCoder.VBE/Inspections/Abstract/InspectionBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ protected InspectionBase(RubberduckParserState state, CodeInspectionSeverity def
7676
/// </summary>
7777
protected virtual IEnumerable<Declaration> Declarations
7878
{
79-
get { return State.AllDeclarations.Where(declaration => !IsInspectionDisabled(declaration, AnnotationName)); }
79+
get { return State.AllDeclarations.Where(declaration => !IsIgnoringInspectionResultFor(declaration, AnnotationName)); }
8080
}
8181

8282
/// <summary>
8383
/// Gets all user declarations in the parser state without an @Ignore annotation for this inspection.
8484
/// </summary>
8585
protected virtual IEnumerable<Declaration> UserDeclarations
8686
{
87-
get { return State.AllUserDeclarations.Where(declaration => !IsInspectionDisabled(declaration, AnnotationName)); }
87+
get { return State.AllUserDeclarations.Where(declaration => !IsIgnoringInspectionResultFor(declaration, AnnotationName)); }
8888
}
8989

9090
protected virtual IEnumerable<Declaration> BuiltInDeclarations
9191
{
9292
get { return State.AllDeclarations.Where(declaration => declaration.IsBuiltIn); }
9393
}
9494

95-
protected bool IsInspectionDisabled(IVBComponent component, int line)
95+
protected bool IsIgnoringInspectionResultFor(IVBComponent component, int line)
9696
{
9797
var annotations = State.GetModuleAnnotations(component).ToList();
9898

@@ -114,7 +114,7 @@ protected bool IsInspectionDisabled(IVBComponent component, int line)
114114
return false;
115115
}
116116

117-
protected bool IsInspectionDisabled(Declaration declaration, string inspectionName)
117+
protected bool IsIgnoringInspectionResultFor(Declaration declaration, string inspectionName)
118118
{
119119
if (declaration.DeclarationType == DeclarationType.Parameter)
120120
{
@@ -128,7 +128,7 @@ protected bool IsInspectionDisabled(Declaration declaration, string inspectionNa
128128
&& ((IgnoreAnnotation)annotation).IsIgnored(inspectionName));
129129
}
130130

131-
protected bool IsInspectionDisabled(IdentifierReference reference, string inspectionName)
131+
protected bool IsIgnoringInspectionResultFor(IdentifierReference reference, string inspectionName)
132132
{
133133
if (reference == null)
134134
{

RetailCoder.VBE/Inspections/AssignedByValParameterInspection.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ public AssignedByValParameterInspection(RubberduckParserState state)
2323

2424
public override IEnumerable<InspectionResultBase> GetInspectionResults()
2525
{
26-
var assignedByValParameters = UserDeclarations.Where(declaration =>
27-
declaration.DeclarationType == DeclarationType.Parameter
28-
&& ((VBAParser.ArgContext)declaration.Context).BYVAL() != null
29-
&& declaration.References.Any(reference => reference.IsAssignment));
26+
var parameters = State.DeclarationFinder.UserDeclarations(DeclarationType.Parameter)
27+
.OfType<ParameterDeclaration>()
28+
.Where(item => !item.IsByRef
29+
&& !IsIgnoringInspectionResultFor(item, AnnotationName)
30+
&& item.References.Any(reference => reference.IsAssignment))
31+
.ToList();
3032

31-
var issues = assignedByValParameters
32-
.Select(param => new AssignedByValParameterInspectionResult(this, param));
33-
34-
return issues;
33+
return parameters
34+
.Select(param => new AssignedByValParameterInspectionResult(this, param))
35+
.ToList();
3536
}
3637
}
3738
}

RetailCoder.VBE/Inspections/Concrete/Inspector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public async Task<IEnumerable<IInspectionResult>> FindIssuesAsync(RubberduckPars
102102
return issuesByType.Where(kv => kv.Value.Count <= AGGREGATION_THRESHOLD)
103103
.SelectMany(kv => kv.Value)
104104
.Union(issuesByType.Where(kv => kv.Value.Count > AGGREGATION_THRESHOLD)
105-
.Select(kv => new AggregateInspectionResult(kv.Value.OrderBy(i => i.QualifiedSelection).ToList())));
105+
.Select(kv => new AggregateInspectionResult(kv.Value.OrderBy(i => i.QualifiedSelection).First(), kv.Value.Count)));
106106
//return allIssues;
107107
}
108108

RetailCoder.VBE/Inspections/ConstantNotUsedInspection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public ConstantNotUsedInspection(RubberduckParserState state)
2121

2222
public override IEnumerable<InspectionResultBase> GetInspectionResults()
2323
{
24-
var results = UserDeclarations.Where(declaration =>
25-
declaration.DeclarationType == DeclarationType.Constant && !declaration.References.Any());
24+
var results = State.DeclarationFinder
25+
.UserDeclarations(DeclarationType.Constant)
26+
.Where(declaration => !declaration.References.Any()
27+
&& !IsIgnoringInspectionResultFor(declaration, AnnotationName))
28+
.ToList();
2629

2730
return results.Select(issue =>
2831
new IdentifierNotUsedInspectionResult(this, issue, ((dynamic)issue.Context).identifier(), issue.QualifiedName.QualifiedModuleName));

RetailCoder.VBE/Inspections/DefaultProjectNameInspection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public DefaultProjectNameInspection(RubberduckParserState state)
2121

2222
public override IEnumerable<InspectionResultBase> GetInspectionResults()
2323
{
24-
var issues = UserDeclarations
25-
.Where(declaration => declaration.DeclarationType == DeclarationType.Project
26-
&& declaration.IdentifierName.StartsWith("VBAProject"))
27-
.Select(issue => new DefaultProjectNameInspectionResult(this, issue, State))
28-
.ToList();
24+
var projects = State.DeclarationFinder.UserDeclarations(DeclarationType.Project)
25+
.Where(item => item.IdentifierName.StartsWith("VBAProject"))
26+
.ToList();
2927

30-
return issues;
28+
return projects
29+
.Select(issue => new DefaultProjectNameInspectionResult(this, issue, State))
30+
.ToList();
3131
}
3232
}
3333
}

RetailCoder.VBE/Inspections/EmptyStringLiteralInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
3636
return new InspectionResultBase[] { };
3737
}
3838
return ParseTreeResults
39-
.Where(result => !IsInspectionDisabled(result.ModuleName.Component, result.Context.Start.Line))
39+
.Where(result => !IsIgnoringInspectionResultFor(result.ModuleName.Component, result.Context.Start.Line))
4040
.Select(result => new EmptyStringLiteralInspectionResult(this, result));
4141
}
4242

RetailCoder.VBE/Inspections/EncapsulatePublicFieldInspection.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ public EncapsulatePublicFieldInspection(RubberduckParserState state, IIndenter i
2626

2727
public override IEnumerable<InspectionResultBase> GetInspectionResults()
2828
{
29-
var issues = UserDeclarations
30-
.Where(declaration => declaration.DeclarationType == DeclarationType.Variable
31-
&& declaration.Accessibility == Accessibility.Public)
32-
.Select(issue => new EncapsulatePublicFieldInspectionResult(this, issue, State, _indenter))
33-
.ToList();
29+
var fields = State.DeclarationFinder.UserDeclarations(DeclarationType.Variable)
30+
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName)
31+
&& item.Accessibility == Accessibility.Public)
32+
.ToList();
3433

35-
return issues;
34+
return fields
35+
.Select(issue => new EncapsulatePublicFieldInspectionResult(this, issue, State, _indenter))
36+
.ToList();
3637
}
3738
}
3839
}

RetailCoder.VBE/Inspections/FunctionReturnValueNotUsedInspection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
2929
// default member is of a class.
3030
var interfaceMembers = UserDeclarations.FindInterfaceMembers().ToList();
3131
var interfaceImplementationMembers = UserDeclarations.FindInterfaceImplementationMembers();
32-
var functions = UserDeclarations.Where(function => function.DeclarationType == DeclarationType.Function).ToList();
32+
var functions = State.DeclarationFinder
33+
.UserDeclarations(DeclarationType.Function)
34+
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName))
35+
.ToList();
3336
var interfaceMemberIssues = GetInterfaceMemberIssues(interfaceMembers);
3437
var nonInterfaceFunctions = functions.Except(interfaceMembers.Union(interfaceImplementationMembers));
3538
var nonInterfaceIssues = GetNonInterfaceIssues(nonInterfaceFunctions);

RetailCoder.VBE/Inspections/ImplicitActiveSheetReferenceInspection.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,25 @@ public ImplicitActiveSheetReferenceInspection(RubberduckParserState state)
2525

2626
public override IEnumerable<InspectionResultBase> GetInspectionResults()
2727
{
28-
var matches = BuiltInDeclarations.Where(item =>
29-
item.ProjectName == "Excel" &&
30-
Targets.Contains(item.IdentifierName) &&
31-
(item.ParentDeclaration.ComponentName == "_Global" || item.ParentDeclaration.ComponentName == "Global") &&
32-
item.AsTypeName == "Range").ToList();
28+
var excel = State.DeclarationFinder.Projects.SingleOrDefault(item => item.IsBuiltIn && item.IdentifierName == "Excel");
29+
if (excel == null) { return Enumerable.Empty<InspectionResultBase>(); }
3330

34-
var issues = matches.Where(item => item.References.Any())
35-
.SelectMany(declaration => declaration.References.Distinct());
31+
var globalModules = new[]
32+
{
33+
State.DeclarationFinder.FindClassModule("Global", excel, true),
34+
State.DeclarationFinder.FindClassModule("_Global", excel, true)
35+
};
3636

37-
return issues
38-
.Where(issue => !issue.IsInspectionDisabled(AnnotationName))
39-
.Select(issue => new ImplicitActiveSheetReferenceInspectionResult(this, issue));
37+
var members = Targets
38+
.SelectMany(target => globalModules.SelectMany(global =>
39+
State.DeclarationFinder.FindMemberMatches(global, target))
40+
.Where(member => member.AsTypeName == "Range" && member.References.Any()));
41+
42+
return members
43+
.SelectMany(declaration => declaration.References)
44+
.Where(issue => !issue.IsIgnoringInspectionResultFor(AnnotationName))
45+
.Select(issue => new ImplicitActiveSheetReferenceInspectionResult(this, issue))
46+
.ToList();
4047
}
4148
}
4249
}

RetailCoder.VBE/Inspections/ImplicitActiveWorkbookReferenceInspection.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@ public ImplicitActiveWorkbookReferenceInspection(RubberduckParserState state)
2323
"Worksheets", "Sheets", "Names", "_Default"
2424
};
2525

26-
private static readonly string[] ParentScopes =
27-
{
28-
"_Global",
29-
"Global",
30-
"_Application",
31-
"Application",
32-
"Sheets",
33-
//"Worksheets",
34-
};
35-
3626
public override IEnumerable<InspectionResultBase> GetInspectionResults()
3727
{
38-
var issues = BuiltInDeclarations
39-
.Where(item => item.ProjectName == "Excel" && ParentScopes.Contains(item.ComponentName)
40-
&& item.References.Any(r => Targets.Contains(r.IdentifierName)))
41-
.SelectMany(declaration => declaration.References.Distinct())
28+
var excel = State.DeclarationFinder.Projects.SingleOrDefault(item => item.IsBuiltIn && item.IdentifierName == "Excel");
29+
if (excel == null) { return Enumerable.Empty<InspectionResultBase>(); }
30+
31+
var modules = new[]
32+
{
33+
State.DeclarationFinder.FindClassModule("_Global", excel, true),
34+
State.DeclarationFinder.FindClassModule("_Application", excel, true),
35+
State.DeclarationFinder.FindClassModule("Global", excel, true),
36+
State.DeclarationFinder.FindClassModule("Application", excel, true),
37+
State.DeclarationFinder.FindClassModule("Sheets", excel, true),
38+
};
39+
40+
var members = Targets
41+
.SelectMany(target => modules.SelectMany(module =>
42+
State.DeclarationFinder.FindMemberMatches(module, target)))
43+
.Where(item => item.References.Any())
44+
.SelectMany(item => item.References.Where(reference => !IsIgnoringInspectionResultFor(reference, AnnotationName)))
4245
.ToList();
4346

44-
var filtered = issues.Where(item => Targets.Contains(item.IdentifierName));
45-
return filtered.Select(issue => new ImplicitActiveWorkbookReferenceInspectionResult(this, issue));
47+
return members.Select(issue => new ImplicitActiveWorkbookReferenceInspectionResult(this, issue));
4648
}
4749
}
4850
}

0 commit comments

Comments
 (0)