Skip to content

Commit e5b43f3

Browse files
committed
Check MemberAccess actually acesses rewritten member
This should symptomatically fix #4355 The rewriting logic for other modules just looked for MemberAccessContextExpressions as parents of references of the rewritten target without making sure that the member accessed is actually the target. That lead to overzealous rewriting.
1 parent 1b9dbdd commit e5b43f3

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Rubberduck.Refactorings/MoveCloserToUsage/MoveCloserToUsageRefactoring.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ private void UpdateCallsToOtherModule(IEnumerable<IdentifierReference> reference
192192
{
193193
foreach (var reference in references.OrderByDescending(o => o.Selection.StartLine).ThenByDescending(t => t.Selection.StartColumn))
194194
{
195+
// todo: Grab `GetAncestor` and use that
195196
var parent = reference.Context.Parent;
196197
while (!(parent is VBAParser.MemberAccessExprContext) && parent.Parent != null)
197198
{
@@ -203,6 +204,14 @@ private void UpdateCallsToOtherModule(IEnumerable<IdentifierReference> reference
203204
continue;
204205
}
205206

207+
// member access might be to something unrelated to the rewritten target.
208+
// check we're not accidentally overwriting some other member-access who just happens to be a parent context
209+
var memberAccessContext = (VBAParser.MemberAccessExprContext)parent;
210+
if (memberAccessContext.unrestrictedIdentifier().GetText() != _target.IdentifierName)
211+
{
212+
continue;
213+
}
214+
206215
var rewriter = _state.GetRewriter(reference.QualifiedModuleName);
207216
var tokenInterval = Interval.Of(parent.SourceInterval.a, reference.Context.SourceInterval.b);
208217
rewriter.Replace(tokenInterval, reference.IdentifierName);

RubberduckTests/Refactoring/MoveCloserToUsageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ Private Sub Foo()
921921
[Test]
922922
[Category("Move Closer")]
923923
[Category("Refactorings")]
924-
public void MoveCloser_RespectsWithBlockContexts()
924+
public void MoveCloser_RespectsMemberAccess_ContextOwners()
925925
{
926926
const string inputCode =
927927
@"

0 commit comments

Comments
 (0)