/
MoveFieldCloserToUsageInspection.cs
67 lines (58 loc) · 2.93 KB
/
MoveFieldCloserToUsageInspection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Results;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Resources.Inspections;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
namespace Rubberduck.Inspections.Concrete
{
public sealed class MoveFieldCloserToUsageInspection : InspectionBase
{
public MoveFieldCloserToUsageInspection(RubberduckParserState state)
: base(state) { }
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
return State.DeclarationFinder.UserDeclarations(DeclarationType.Variable)
.Where(declaration =>
{
if (declaration.IsWithEvents
|| !new[] {DeclarationType.ClassModule, DeclarationType.Document, DeclarationType.ProceduralModule}.Contains(declaration.ParentDeclaration.DeclarationType)
|| IsIgnoringInspectionResultFor(declaration, AnnotationName))
{
return false;
}
var asType = declaration.AsTypeDeclaration;
if (asType != null && asType.ProjectName.Equals("Rubberduck") &&
(asType.IdentifierName.Equals("PermissiveAssertClass") || asType.IdentifierName.Equals("AssertClass")))
{
return false;
}
var firstReference = declaration.References.FirstOrDefault();
if (firstReference == null ||
declaration.References.Any(r => r.ParentScoping != firstReference.ParentScoping))
{
return false;
}
var parentDeclaration = ParentDeclaration(firstReference);
return parentDeclaration != null &&
!new[]
{
DeclarationType.PropertyGet,
DeclarationType.PropertyLet,
DeclarationType.PropertySet
}.Contains(parentDeclaration.DeclarationType);
})
.Select(issue =>
new DeclarationInspectionResult(this, string.Format(InspectionResults.MoveFieldCloserToUsageInspection, issue.IdentifierName), issue));
}
private Declaration ParentDeclaration(IdentifierReference reference)
{
var declarationTypes = new[] {DeclarationType.Function, DeclarationType.Procedure, DeclarationType.Property};
return UserDeclarations.SingleOrDefault(d =>
reference.ParentScoping.Equals(d) && declarationTypes.Contains(d.DeclarationType) &&
d.QualifiedName.QualifiedModuleName.Equals(reference.QualifiedModuleName));
}
}
}