Skip to content

Commit 08217c7

Browse files
committed
Fix RemoveParameterRefactoring for properties in interfaces
Previously, we did not remove the parameters for the implementations of the setters and letters.
1 parent e58bb04 commit 08217c7

File tree

2 files changed

+100
-40
lines changed

2 files changed

+100
-40
lines changed

Rubberduck.Refactorings/RemoveParameters/RemoveParametersRefactoring.cs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,17 @@ private void RemoveParameters(RemoveParametersModel model, IRewriteSession rewri
190190
if (setter != null)
191191
{
192192
var setterModel = ModelForNewTarget(model, setter);
193-
AdjustReferences(setterModel, rewriteSession);
194-
AdjustSignatures(setterModel, rewriteSession);
193+
RemoveParameters(setterModel, rewriteSession);
195194
}
196195

197196
var letter = GetLetterOrSetter(model.TargetDeclaration, DeclarationType.PropertyLet);
198197
if (letter != null)
199198
{
200199
var letterModel = ModelForNewTarget(model, letter);
201-
AdjustReferences(letterModel, rewriteSession);
202-
AdjustSignatures(letterModel, rewriteSession);
200+
RemoveParameters(letterModel, rewriteSession);
203201
}
204202
}
205203

206-
AdjustSignatures(model, rewriteSession);
207-
208204
var eventImplementations = _declarationFinderProvider.DeclarationFinder
209205
.FindEventHandlers(model.TargetDeclaration);
210206

@@ -216,19 +212,17 @@ private void RemoveParameters(RemoveParametersModel model, IRewriteSession rewri
216212
}
217213

218214
var interfaceImplementations = _declarationFinderProvider.DeclarationFinder
219-
.FindAllInterfaceImplementingMembers()
220-
.Where(item => item.ProjectId == model.TargetDeclaration.ProjectId
221-
&& item.IdentifierName == $"{model.TargetDeclaration.ComponentName}_{model.TargetDeclaration.IdentifierName}");
215+
.FindInterfaceImplementationMembers(model.TargetDeclaration);
222216

223-
foreach (var interfaceImplentation in interfaceImplementations)
217+
foreach (var interfaceImplementation in interfaceImplementations)
224218
{
225-
var interfaceImplementationModel = ModelForNewTarget(model, interfaceImplentation);
219+
var interfaceImplementationModel = ModelForNewTarget(model, interfaceImplementation);
226220
AdjustReferences(interfaceImplementationModel, rewriteSession);
227221
AdjustSignatures(interfaceImplementationModel, rewriteSession);
228222
}
229223
}
230224

231-
private void AdjustReferences(RemoveParametersModel model, IRewriteSession rewriteSession)
225+
private static void AdjustReferences(RemoveParametersModel model, IRewriteSession rewriteSession)
232226
{
233227
var parametersToRemove = model.RemoveParameters
234228
.Select(parameter => parameter.Declaration)
@@ -241,7 +235,7 @@ private void AdjustReferences(RemoveParametersModel model, IRewriteSession rewri
241235
}
242236
}
243237

244-
private Dictionary<QualifiedModuleName, Dictionary<Selection, List<ArgumentReference>>> ArgumentReferencesByLocation(ICollection<ParameterDeclaration> parameters)
238+
private static Dictionary<QualifiedModuleName, Dictionary<Selection, List<ArgumentReference>>> ArgumentReferencesByLocation(ICollection<ParameterDeclaration> parameters)
245239
{
246240
return parameters
247241
.SelectMany(parameterDeclaration => parameterDeclaration.ArgumentReferences)
@@ -253,7 +247,7 @@ private Dictionary<QualifiedModuleName, Dictionary<Selection, List<ArgumentRefer
253247
.ToDictionary(group => group.Key, group => group.ToList()));
254248
}
255249

256-
private void AdjustReferences(
250+
private static void AdjustReferences(
257251
QualifiedModuleName module,
258252
Dictionary<Selection, List<ArgumentReference>> argumentReferences,
259253
IRewriteSession rewriteSession)
@@ -294,7 +288,7 @@ private static void ReplaceDictionaryAccess(ArgumentReference dictionaryAccessAr
294288
rewriter.Replace(new Interval(startTokenIndex, stopTokenIndex), replacementString);
295289
}
296290

297-
private void AdjustReferences(IReadOnlyCollection<ArgumentReference> argumentReferences, IModuleRewriter rewriter)
291+
private static void AdjustReferences(IReadOnlyCollection<ArgumentReference> argumentReferences, IModuleRewriter rewriter)
298292
{
299293
if (!argumentReferences.Any())
300294
{
@@ -314,7 +308,7 @@ private void AdjustReferences(IReadOnlyCollection<ArgumentReference> argumentRef
314308
}
315309
}
316310

317-
private IEnumerable<(int startIndex, int stopIndex)> IndexRanges(IEnumerable<int> indices)
311+
private static IEnumerable<(int startIndex, int stopIndex)> IndexRanges(IEnumerable<int> indices)
318312
{
319313
var sortedIndices = indices.OrderBy(num => num).ToList();
320314
var ranges = new List<(int startIndex, int stopIndex)>();
@@ -346,7 +340,7 @@ private void AdjustReferences(IReadOnlyCollection<ArgumentReference> argumentRef
346340
return ranges;
347341
}
348342

349-
private IEnumerable<(int startIndex, int stopIndex)> WithTrailingMissingArguments(
343+
private static IEnumerable<(int startIndex, int stopIndex)> WithTrailingMissingArguments(
350344
IEnumerable<(int startIndex, int stopIndex)> argumentRanges,
351345
VBAParser.ArgumentListContext argumentList)
352346
{
@@ -384,7 +378,7 @@ private void AdjustReferences(IReadOnlyCollection<ArgumentReference> argumentRef
384378
return newRanges;
385379
}
386380

387-
private void RemoveArgumentRange(
381+
private static void RemoveArgumentRange(
388382
int startArgumentIndex,
389383
int stopArgumentIndex,
390384
VBAParser.ArgumentListContext argumentList,
@@ -394,7 +388,7 @@ private void RemoveArgumentRange(
394388
rewriter.RemoveRange(startTokenIndex, stopTokenIndex);
395389
}
396390

397-
private (int startTokenIndex, int stopTokenIndex) TokenIndexRange(
391+
private static (int startTokenIndex, int stopTokenIndex) TokenIndexRange(
398392
int startIndex,
399393
int stopIndex,
400394
IReadOnlyList<ParserRuleContext> contexts)
@@ -416,26 +410,7 @@ private void RemoveArgumentRange(
416410
return (startTokenIndex, stopTokenIndex);
417411
}
418412

419-
private RemoveParametersModel ModelForNewTarget(RemoveParametersModel oldModel, Declaration newTarget)
420-
{
421-
var newModel = new RemoveParametersModel(newTarget);
422-
var toRemoveIndices = oldModel.RemoveParameters.Select(param => oldModel.Parameters.IndexOf(param));
423-
var newToRemoveParams = toRemoveIndices
424-
.Select(index => newModel.Parameters[index])
425-
.ToList();
426-
newModel.RemoveParameters = newToRemoveParams;
427-
return newModel;
428-
}
429-
430-
private Declaration GetLetterOrSetter(Declaration declaration, DeclarationType declarationType)
431-
{
432-
return _declarationFinderProvider.DeclarationFinder
433-
.UserDeclarations(declarationType)
434-
.FirstOrDefault(item => item.QualifiedModuleName.Equals(declaration.QualifiedModuleName)
435-
&& item.IdentifierName == declaration.IdentifierName);
436-
}
437-
438-
private void AdjustSignatures(RemoveParametersModel model, IRewriteSession rewriteSession)
413+
private static void AdjustSignatures(RemoveParametersModel model, IRewriteSession rewriteSession)
439414
{
440415
var rewriter = rewriteSession.CheckOutModuleRewriter(model.TargetDeclaration.QualifiedModuleName);
441416

@@ -451,7 +426,7 @@ private void AdjustSignatures(RemoveParametersModel model, IRewriteSession rewri
451426
}
452427
}
453428

454-
private void RemoveParameterRange(
429+
private static void RemoveParameterRange(
455430
int startArgumentIndex,
456431
int stopArgumentIndex,
457432
VBAParser.ArgListContext argList,
@@ -461,6 +436,25 @@ private void RemoveParameterRange(
461436
rewriter.RemoveRange(startTokenIndex, stopTokenIndex);
462437
}
463438

439+
private Declaration GetLetterOrSetter(Declaration declaration, DeclarationType declarationType)
440+
{
441+
return _declarationFinderProvider.DeclarationFinder
442+
.UserDeclarations(declarationType)
443+
.FirstOrDefault(item => item.QualifiedModuleName.Equals(declaration.QualifiedModuleName)
444+
&& item.IdentifierName == declaration.IdentifierName);
445+
}
446+
447+
private static RemoveParametersModel ModelForNewTarget(RemoveParametersModel oldModel, Declaration newTarget)
448+
{
449+
var newModel = new RemoveParametersModel(newTarget);
450+
var toRemoveIndices = oldModel.RemoveParameters.Select(param => oldModel.Parameters.IndexOf(param));
451+
var newToRemoveParams = toRemoveIndices
452+
.Select(index => newModel.Parameters[index])
453+
.ToList();
454+
newModel.RemoveParameters = newToRemoveParams;
455+
return newModel;
456+
}
457+
464458
public static readonly DeclarationType[] ValidDeclarationTypes =
465459
{
466460
DeclarationType.Event,

RubberduckTests/Refactoring/RemoveParametersTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,72 @@ Private Sub IClass1_DoSomething(ByVal i As Integer)
13421342
Assert.AreEqual(expectedCode3, actualCode["Class2"]);
13431343
}
13441344

1345+
[Test]
1346+
[Category("Refactorings")]
1347+
[Category("Remove Parameters")]
1348+
public void RemoveParametersRefactoring_InterfaceGetterParam_ImplementationLetAndSetParamRemoved()
1349+
{
1350+
//Input
1351+
const string inputCode1 =
1352+
@"Public Property Get Foo(ByVal a As Integer, ByVal b As String) As Variant
1353+
End Property
1354+
1355+
Public Property Let Foo(ByVal a As Integer, ByVal b As String, RHS As Variant)
1356+
End Property
1357+
1358+
Public Property Set Foo(ByVal a As Integer, ByVal b As String, RHS As Variant)
1359+
End Property";
1360+
const string inputCode2 =
1361+
@"Implements IClass1
1362+
1363+
Private Property Get IClass1_Foo(ByVal a As Integer, ByVal b As String) As Variant
1364+
End Property
1365+
1366+
Private Property Let IClass1_Foo(ByVal a As Integer, ByVal b As String, RHS As Variant)
1367+
End Property
1368+
1369+
Private Property Set IClass1_Foo(ByVal a As Integer, ByVal b As String, RHS As Variant)
1370+
End Property";
1371+
1372+
var selection = new Selection(1, 23, 1, 27);
1373+
1374+
//Expectation
1375+
const string expectedCode1 =
1376+
@"Public Property Get Foo(ByVal a As Integer) As Variant
1377+
End Property
1378+
1379+
Public Property Let Foo(ByVal a As Integer, RHS As Variant)
1380+
End Property
1381+
1382+
Public Property Set Foo(ByVal a As Integer, RHS As Variant)
1383+
End Property";
1384+
const string expectedCode2 =
1385+
@"Implements IClass1
1386+
1387+
Private Property Get IClass1_Foo(ByVal a As Integer) As Variant
1388+
End Property
1389+
1390+
Private Property Let IClass1_Foo(ByVal a As Integer, RHS As Variant)
1391+
End Property
1392+
1393+
Private Property Set IClass1_Foo(ByVal a As Integer, RHS As Variant)
1394+
End Property";
1395+
1396+
var paramIndices = new[] { 1 }.ToList();
1397+
var presenterAction = StandardPresenterAction(paramIndices);
1398+
var actualCode = RefactoredCode(
1399+
"IClass1",
1400+
selection,
1401+
presenterAction,
1402+
null,
1403+
false,
1404+
("IClass1", inputCode1, ComponentType.ClassModule),
1405+
("Class1", inputCode2, ComponentType.ClassModule));
1406+
1407+
Assert.AreEqual(expectedCode1, actualCode["IClass1"]);
1408+
Assert.AreEqual(expectedCode2, actualCode["Class1"]);
1409+
}
1410+
13451411
[Test]
13461412
[Category("Refactorings")]
13471413
[Category("Remove Parameters")]

0 commit comments

Comments
 (0)