/
ImplicitContainingWorksheetReferenceInspection.cs
60 lines (57 loc) · 2.54 KB
/
ImplicitContainingWorksheetReferenceInspection.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
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 Worksheet.Range/Cells/Columns/Rows member calls inside worksheet modules, that implicitly refer to the containing sheet component.
/// </summary>
/// <hostApp name="Excel" />
/// <why>
/// Implicit references inside a worksheet document module can easily be mistaken for implicit references to the active worksheet (ActiveSheet), which is the behavior in all other module types.
/// By explicitly qualifying these member calls with 'Me', the ambiguity can be resolved. If the intent is to refer to the active worksheet, qualify with 'ActiveSheet' to prevent a bug.
/// </why>
/// <example hasResult="true">
/// <module name="Sheet1" type="Document Module">
/// <![CDATA[
/// Private Sub Example()
/// Dim foo As Range
/// Set foo = Range("A1") ' Worksheet.Range implicitly from containing worksheet
/// End Sub
/// ]]>
/// </module>
/// </example>
/// <example hasResult="false">
/// <module name="Sheet1" type="Document Module">
/// <![CDATA[
/// Private Sub Example()
/// Dim foo As Range
/// Set foo = Me.Range("A1")
/// End Sub
/// ]]>
/// </module>
/// </example>
[RequiredHost("Excel")]
internal sealed class ImplicitContainingWorksheetReferenceInspection : ImplicitSheetReferenceInspectionBase
{
public ImplicitContainingWorksheetReferenceInspection(IDeclarationFinderProvider declarationFinderProvider)
: base(declarationFinderProvider)
{}
protected override bool IsResultReference(IdentifierReference reference, DeclarationFinder finder)
{
return Declaration.GetModuleParent(reference.ParentNonScoping) is DocumentModuleDeclaration document
&& document.SupertypeNames.Contains("Worksheet")
&& reference.QualifyingReference == null; // if it's qualified, it's not an implicit reference
}
protected override string ResultDescription(IdentifierReference reference)
{
return string.Format(
InspectionResults.ImplicitContainingWorksheetReferenceInspection,
reference.Declaration.IdentifierName);
}
}
}