Skip to content

Commit 236e91d

Browse files
committed
Gather appropriate declarations for test, fix broken name matching. Closes #4411
1 parent df121ba commit 236e91d

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/SheetAccessedUsingStringInspection.cs

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,54 @@ namespace Rubberduck.Inspections.Concrete
1717
[RequiredLibrary("Excel")]
1818
public class SheetAccessedUsingStringInspection : InspectionBase
1919
{
20-
public SheetAccessedUsingStringInspection(RubberduckParserState state) : base(state)
21-
{
22-
}
20+
public SheetAccessedUsingStringInspection(RubberduckParserState state) : base(state) { }
2321

24-
private static readonly string[] Targets =
22+
private static readonly string[] InterestingMembers =
2523
{
2624
"Worksheets", "Sheets"
2725
};
2826

27+
private static readonly string[] InterestingClasses =
28+
{
29+
"_Global", "_Application", "Global", "Application", "Workbook"
30+
};
31+
2932
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
3033
{
3134
var excel = State.DeclarationFinder.Projects.SingleOrDefault(item => !item.IsUserDefined && item.IdentifierName == "Excel");
3235
if (excel == null)
3336
{
34-
return Enumerable.Empty<IInspectionResult>();
35-
37+
return Enumerable.Empty<IInspectionResult>();
3638
}
3739

38-
var modules = new[]
39-
{
40-
State.DeclarationFinder.FindClassModule("_Global", excel, true),
41-
State.DeclarationFinder.FindClassModule("_Application", excel, true),
42-
State.DeclarationFinder.FindClassModule("Global", excel, true),
43-
State.DeclarationFinder.FindClassModule("Application", excel, true),
44-
State.DeclarationFinder.FindClassModule("Workbook", excel, true),
45-
};
46-
47-
var references = Targets
48-
.SelectMany(target => modules.SelectMany(module => State.DeclarationFinder.FindMemberMatches(module, target)))
49-
.Where(declaration => declaration.References.Any())
50-
.SelectMany(declaration => declaration.References
51-
.Where(reference =>
52-
!IsIgnoringInspectionResultFor(reference, AnnotationName) && IsAccessedWithStringLiteralParameter(reference))
53-
.Select(reference => new IdentifierReferenceInspectionResult(this,
54-
InspectionResults.SheetAccessedUsingStringInspection, State, reference)));
40+
var targetProperties = BuiltInDeclarations
41+
.OfType<PropertyDeclaration>()
42+
.Where(x => InterestingMembers.Contains(x.IdentifierName) && InterestingClasses.Contains(x.ParentDeclaration?.IdentifierName))
43+
.ToList();
44+
45+
var references = targetProperties.SelectMany(declaration => declaration.References
46+
.Where(reference => !IsIgnoringInspectionResultFor(reference, AnnotationName) &&
47+
IsAccessedWithStringLiteralParameter(reference))
48+
.Select(reference => new IdentifierReferenceInspectionResult(this,
49+
InspectionResults.SheetAccessedUsingStringInspection, State, reference)));
5550

5651
var issues = new List<IdentifierReferenceInspectionResult>();
5752

5853
foreach (var reference in references)
5954
{
60-
var component = GetVBComponentMatchingSheetName(reference);
61-
if (component != null)
55+
using (var component = GetVBComponentMatchingSheetName(reference))
6256
{
57+
if (component == null)
58+
{
59+
continue;
60+
}
6361
using (var properties = component.Properties)
6462
{
6563
reference.Properties.CodeName = (string)properties.Single(property => property.Name == "CodeName").Value;
6664
}
6765
issues.Add(reference);
6866
}
6967
}
70-
7168
return issues;
7269
}
7370

@@ -101,47 +98,38 @@ private IVBComponent GetVBComponentMatchingSheetName(IdentifierReferenceInspecti
10198
var sheetName = FormatSheetName(sheetArgumentContext.GetText());
10299
var project = State.Projects.First(p => p.ProjectId == reference.QualifiedName.ProjectId);
103100

104-
105-
//return project.VBComponents.FirstOrDefault(c =>
106-
// c.Type == ComponentType.Document &&
107-
// (string)c.Properties.First(property => property.Name == "Name").Value == sheetName);
108101
using (var components = project.VBComponents)
109102
{
110-
for (var i = 0; i < components.Count; i++)
103+
foreach (var component in components)
111104
{
112-
using (var component = components[i])
113105
using (var properties = component.Properties)
114106
{
115-
if (component.Type == ComponentType.Document)
107+
if (component.Type != ComponentType.Document)
116108
{
117-
for (var j = 0; j < properties.Count; j++)
109+
component.Dispose();
110+
continue;
111+
}
112+
foreach (var property in properties)
113+
{
114+
var found = property.Name.Equals("Name") && ((string)property.Value).Equals(sheetName);
115+
property.Dispose();
116+
if (found)
118117
{
119-
using (var property = properties[j])
120-
{
121-
if (property.Name == "Name" && (string)property.Value == sheetName)
122-
{
123-
return component;
124-
}
125-
}
126-
}
118+
return component;
119+
}
127120
}
128121
}
122+
component.Dispose();
129123
}
130-
131124
return null;
132125
}
133126
}
134127

135128
private static string FormatSheetName(string sheetName)
136129
{
137-
var formattedName = sheetName.First() == '"' ? sheetName.Skip(1) : sheetName;
138-
139-
if (sheetName.Last() == '"')
140-
{
141-
formattedName = formattedName.Take(formattedName.Count() - 1);
142-
}
143-
144-
return string.Concat(formattedName);
130+
return sheetName.StartsWith("\"") && sheetName.EndsWith("\"")
131+
? sheetName.Substring(1, sheetName.Length - 2)
132+
: sheetName;
145133
}
146134
}
147135
}

0 commit comments

Comments
 (0)