/
ImplicitContainingWorkbookReferenceInspection.cs
72 lines (67 loc) · 2.96 KB
/
ImplicitContainingWorkbookReferenceInspection.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
68
69
70
71
72
using System.Collections.Generic;
using System.Linq;
using Rubberduck.CodeAnalysis.Inspections.Abstract;
using Rubberduck.CodeAnalysis.Inspections.Attributes;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.DeclarationCaching;
using Rubberduck.Resources.Inspections;
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
{
/// <summary>
/// Locates unqualified Workbook.Worksheets/Sheets/Names member calls inside workbook document modules that implicitly refer to the containing workbook.
/// </summary>
/// <reference name="Excel" />
/// <why>
/// Implicit references inside a workbook document module can be mistakes for implicit references to the active workbook, which is the behavior in all other modules
/// By explicitly qualifying these member calls with Me, the ambiguity can be resolved.
/// </why>
/// <example hasResult="true">
/// <module name="ThisWorkbook" type="Document Module">
/// <![CDATA[
/// Private Sub Example()
/// Dim summarySheet As Worksheet
/// Set summarySheet = Worksheets("Summary") ' unqualified Worksheets is implicitly querying the containing workbook's Worksheets collection.
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="ThisWorkbook" type="Document Module">
/// <![CDATA[
/// Private Sub Example()
/// Dim summarySheet As Worksheet
/// Set summarySheet = Me.Worksheets("Summary")
/// End Sub
/// ]]>
/// </module>
/// </example>
[RequiredLibrary("Excel")]
internal sealed class ImplicitContainingWorkbookReferenceInspection : ImplicitWorkbookReferenceInspectionBase
{
public ImplicitContainingWorkbookReferenceInspection(IDeclarationFinderProvider declarationFinderProvider)
: base(declarationFinderProvider)
{ }
private static readonly List<string> _alwaysActiveWorkbookReferenceParents = new List<string>
{
"_Application", "Application"
};
protected override IEnumerable<Declaration> ObjectionableDeclarations(DeclarationFinder finder)
{
return base.ObjectionableDeclarations(finder)
.Where(declaration => !_alwaysActiveWorkbookReferenceParents.Contains(declaration.ParentDeclaration.IdentifierName));
}
protected override bool IsResultReference(IdentifierReference reference, DeclarationFinder finder)
{
return Declaration.GetModuleParent(reference.ParentNonScoping) is DocumentModuleDeclaration document
&& document.SupertypeNames.Contains("Workbook");
}
protected override string ResultDescription(IdentifierReference reference)
{
var referenceText = reference.Context.GetText();
return string.Format(
InspectionResults.ImplicitContainingWorkbookReferenceInspection,
referenceText);
}
}
}