Skip to content

Commit ab1d8bf

Browse files
committed
Some more refactoring
1 parent 13d9846 commit ab1d8bf

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

Rubberduck.Parsing/Symbols/AccessibilityCheck.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ public static bool IsAccessible(Declaration callingProject, Declaration callingM
1919
}
2020

2121
public static bool IsModuleAccessible(Declaration callingProject, Declaration callingModule, Declaration calleeModule)
22-
{
23-
bool enclosingModule = callingModule.Equals(calleeModule);
24-
if (enclosingModule)
22+
{
23+
if (IsEnclosingModuleOfModule(callingModule, calleeModule))
2524
{
2625
return true;
2726
}
28-
bool sameProject = callingModule.ParentScopeDeclaration.Equals(calleeModule.ParentScopeDeclaration);
29-
if (sameProject)
27+
else if (IsInTheSameProject(callingModule, calleeModule))
3028
{
3129
return IsValidAccessibility(calleeModule);
3230
}
@@ -42,6 +40,17 @@ public static bool IsModuleAccessible(Declaration callingProject, Declaration ca
4240
}
4341
}
4442

43+
private static bool IsEnclosingModuleOfModule(Declaration callingModule, Declaration calleeModule)
44+
{
45+
return callingModule.Equals(calleeModule);
46+
}
47+
48+
private static bool IsInTheSameProject(Declaration callingModule, Declaration calleeModule)
49+
{
50+
return callingModule.ParentScopeDeclaration.Equals(calleeModule.ParentScopeDeclaration);
51+
}
52+
53+
4554
public static bool IsValidAccessibility(Declaration moduleOrMember)
4655
{
4756
return moduleOrMember != null
@@ -53,7 +62,7 @@ public static bool IsValidAccessibility(Declaration moduleOrMember)
5362

5463
public static bool IsMemberAccessible(Declaration callingProject, Declaration callingModule, Declaration callingParent, Declaration calleeMember)
5564
{
56-
if (IsEnclosingModule(callingModule, calleeMember) || (CallerIsSubroutineOrProperty(callingParent) && CaleeHasSameParentAsCaller(callingParent, calleeMember)))
65+
if (IsEnclosingModuleOfInstanceMember(callingModule, calleeMember) || (CallerIsSubroutineOrProperty(callingParent) && CaleeHasSameParentAsCaller(callingParent, calleeMember)))
5766
{
5867
return true;
5968
}
@@ -72,15 +81,15 @@ public static bool IsMemberAccessible(Declaration callingProject, Declaration ca
7281
return false;
7382
}
7483

75-
private static bool IsEnclosingModule(Declaration callingModule, Declaration calleeMember)
84+
private static bool IsEnclosingModuleOfInstanceMember(Declaration callingModule, Declaration calleeMember)
7685
{
7786
if (callingModule.Equals(calleeMember.ParentScopeDeclaration))
7887
{
7988
return true;
8089
}
8190
foreach (var supertype in ClassModuleDeclaration.GetSupertypes(callingModule))
8291
{
83-
if (IsEnclosingModule(supertype, calleeMember))
92+
if (IsEnclosingModuleOfInstanceMember(supertype, calleeMember))
8493
{
8594
return true;
8695
}

Rubberduck.Parsing/Symbols/ClassModuleDeclaration.cs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public static IEnumerable<Declaration> GetSupertypes(Declaration type)
5353
{
5454
return new List<Declaration>();
5555
}
56-
return ((ClassModuleDeclaration)type).Supertypes;
56+
else
57+
{
58+
return ((ClassModuleDeclaration)type).Supertypes;
59+
}
5760
}
5861

5962

@@ -70,24 +73,38 @@ public bool IsExposed
7073
{
7174
return _isExposed.Value;
7275
}
73-
// TODO: Find out if there's info about "being exposed" in type libraries.
74-
// We take the conservative approach of treating all type library modules as exposed.
75-
if (IsBuiltIn)
76+
else if (IsBuiltIn)
7677
{
77-
_isExposed = true;
78+
_isExposed = IsExposedForBuiltInModules();
7879
return _isExposed.Value;
7980
}
80-
var attributeIsExposed = false;
81-
IEnumerable<string> value;
82-
if (Attributes.TryGetValue("VB_Exposed", out value))
81+
else
8382
{
84-
attributeIsExposed = value.Single() == "True";
83+
_isExposed = HasAttribute("VB_Exposed");
84+
return _isExposed.Value;
8585
}
86-
_isExposed = attributeIsExposed;
87-
return _isExposed.Value;
8886
}
8987
}
9088

89+
// TODO: Find out if there's info about "being exposed" in type libraries.
90+
// We take the conservative approach of treating all type library modules as exposed.
91+
private static bool IsExposedForBuiltInModules()
92+
{
93+
return true;
94+
}
95+
96+
private bool HasAttribute(string attributeName)
97+
{
98+
var hasAttribute = false;
99+
IEnumerable<string> value;
100+
if (Attributes.TryGetValue(attributeName, out value))
101+
{
102+
hasAttribute = value.Single() == "True";
103+
}
104+
return hasAttribute;
105+
}
106+
107+
91108
private bool? _isGlobal;
92109
public bool IsGlobalClassModule
93110
{
@@ -97,30 +114,25 @@ public bool IsGlobalClassModule
97114
{
98115
return _isGlobal.Value;
99116
}
117+
_isGlobal = HasAttribute("VB_GlobalNamespace") || IsGlobalFromSubtypes();
118+
return _isGlobal.Value;
119+
}
120+
}
100121

101-
var attributeIsGlobalClassModule = false;
102-
IEnumerable<string> value;
103-
if (Attributes.TryGetValue("VB_GlobalNamespace", out value))
104-
{
105-
attributeIsGlobalClassModule = value.Single() == "True";
106-
}
107-
_isGlobal = attributeIsGlobalClassModule;
108-
109-
if (!_isGlobal.Value)
122+
private bool IsGlobalFromSubtypes()
123+
{
124+
var isGlobal = false;
125+
foreach (var type in Subtypes)
110126
{
111-
foreach (var type in Subtypes)
127+
if (type is ClassModuleDeclaration && ((ClassModuleDeclaration)type).IsGlobalClassModule)
112128
{
113-
if (type is ClassModuleDeclaration && ((ClassModuleDeclaration) type).IsGlobalClassModule)
114-
{
115-
_isGlobal = true;
116-
break;
117-
}
129+
isGlobal = true;
130+
break;
118131
}
119132
}
120-
121-
return _isGlobal.Value;
133+
return isGlobal;
122134
}
123-
}
135+
124136

125137
private bool? _hasPredeclaredId;
126138
/// <summary>
@@ -135,18 +147,12 @@ public bool HasPredeclaredId
135147
{
136148
return _hasPredeclaredId.Value;
137149
}
138-
139-
var attributeHasDefaultInstanceVariable = false;
140-
IEnumerable<string> value;
141-
if (Attributes.TryGetValue("VB_PredeclaredId", out value))
142-
{
143-
attributeHasDefaultInstanceVariable = value.Single() == "True";
144-
}
145-
_hasPredeclaredId = attributeHasDefaultInstanceVariable;
150+
_hasPredeclaredId = HasAttribute("VB_PredeclaredId");
146151
return _hasPredeclaredId.Value;
147152
}
148153
}
149154

155+
150156
public bool HasDefaultInstanceVariable
151157
{
152158
get

0 commit comments

Comments
 (0)