Skip to content

Commit 629deec

Browse files
authored
Merge pull request #5151 from BZngr/4969_ModuleRenames
Rename conflict detection respects Project scope
2 parents f5e88d1 + f39f248 commit 629deec

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

Rubberduck.Parsing/VBA/DeclarationCaching/DeclarationFinder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,8 @@ public IEnumerable<Declaration> FindNewDeclarationNameConflicts(string newName,
13021302
return Enumerable.Empty<Declaration>();
13031303
}
13041304

1305-
var identifierMatches = MatchName(newName).ToList();
1305+
var identifierMatches = MatchName(newName).Where(match => match.ProjectId == renameTarget.ProjectId);
1306+
13061307
if (!identifierMatches.Any())
13071308
{
13081309
return Enumerable.Empty<Declaration>();
@@ -1327,7 +1328,7 @@ public IEnumerable<Declaration> FindNewDeclarationNameConflicts(string newName,
13271328
|| idm.DeclarationType.HasFlag(DeclarationType.Variable)
13281329
&& idm.ParentDeclaration.DeclarationType.HasFlag(DeclarationType.Module)
13291330
&& renameTarget.References.Any(renameTargetRef => renameTargetRef.QualifiedModuleName == idm.ParentDeclaration.QualifiedModuleName))
1330-
.ToList();
1331+
.ToList();
13311332

13321333
if (referenceConflicts.Any())
13331334
{
@@ -1342,7 +1343,8 @@ public IEnumerable<Declaration> FindNewDeclarationNameConflicts(string newName,
13421343
renameTargetModule,
13431344
renameTarget.ParentDeclaration,
13441345
idm)
1345-
&& IsConflictingMember(renameTarget, renameTargetModule, idm));
1346+
&& IsConflictingMember(renameTarget, renameTargetModule, idm))
1347+
.ToList();
13461348

13471349
return declarationConflicts;
13481350
}

RubberduckTests/Symbols/DeclarationFinderTests.cs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Rubberduck.Common;
1313
using Rubberduck.Parsing;
1414
using Rubberduck.Parsing.Grammar;
15+
using System;
1516

1617
namespace RubberduckTests.Symbols
1718
{
@@ -445,6 +446,45 @@ End Sub
445446
Assert.AreEqual(isConflict, conflicts.Where(cf => cf.IdentifierName.Equals(nameToCheck)).Any(), ConflictMessage(isConflict, nameToCheck, conflicts));
446447
}
447448

449+
450+
//https://github.com/rubberduck-vba/Rubberduck/issues/4969
451+
private const string projectOneModuleName = "projectOneModule";
452+
private const string projectTwoModuleName = "projectTwoModule";
453+
[TestCase(projectOneModuleName, 0)] //Duplicate module name found in a separate project
454+
[TestCase(projectTwoModuleName, 1)] //Duplicate module name found in the same project
455+
[Category("Resolver")]
456+
public void DeclarationFinder_NameConflictDetectionRespectsProjectScope(string proposedTestModuleName, int expectedCount)
457+
{
458+
459+
string renameTargetModuleName = "TargetModule";
460+
461+
string moduleContent = $"Private Sub Foo(){Environment.NewLine}End Sub";
462+
463+
var projectOneContent = new TestComponentSpecification[]
464+
{
465+
new TestComponentSpecification(projectOneModuleName, moduleContent, ComponentType.StandardModule)
466+
};
467+
468+
var projectTwoContent = new TestComponentSpecification[]
469+
{
470+
new TestComponentSpecification(renameTargetModuleName, moduleContent, ComponentType.StandardModule),
471+
new TestComponentSpecification(projectTwoModuleName, moduleContent, ComponentType.StandardModule)
472+
};
473+
474+
var vbe = BuildProjects(new (string, IEnumerable<TestComponentSpecification>)[]
475+
{("ProjectOne", projectOneContent),("ProjectTwo", projectTwoContent)});
476+
477+
using(var parser = MockParser.CreateAndParse(vbe))
478+
{
479+
var target = parser.DeclarationFinder.UserDeclarations(DeclarationType.ProceduralModule)
480+
.FirstOrDefault(item => item.IdentifierName.Equals(renameTargetModuleName));
481+
482+
var results = parser.DeclarationFinder.FindNewDeclarationNameConflicts(proposedTestModuleName, target);
483+
484+
Assert.AreEqual(expectedCount, results.Count());
485+
}
486+
}
487+
448488
private static string ConflictMessage(bool isConflict, string name, IEnumerable<Declaration> conflicts)
449489
{
450490
return isConflict ? $"Identifier '{name}' is a conflict but was not identified" : $"Identifier '{name}' was incorrectly found as a conflict";
@@ -498,14 +538,33 @@ private void AddTestComponent(AccessibilityTestsDataObject tdo, string moduleIde
498538
}
499539

500540
private IVBE BuildProject(string projectName, List<TestComponentSpecification> testComponents)
541+
{
542+
var projectDefs = new (string, IEnumerable<TestComponentSpecification>)[] { (projectName, testComponents) };
543+
return BuildProjects(projectDefs);
544+
}
545+
546+
private IVBE BuildProjects(IEnumerable<(string ProjectName, IEnumerable<TestComponentSpecification> TestComponents)> projectDefinitions)
501547
{
502548
var builder = new MockVbeBuilder();
549+
foreach (var projectDef in projectDefinitions)
550+
{
551+
builder = AddProject(builder, projectDef.ProjectName, projectDef.TestComponents);
552+
}
553+
return builder.Build().Object;
554+
}
555+
556+
private MockVbeBuilder AddProject(MockVbeBuilder builder, string projectName, IEnumerable<TestComponentSpecification> testComponents)
557+
{
503558
var enclosingProjectBuilder = builder.ProjectBuilder(projectName, ProjectProtection.Unprotected);
504559

505-
testComponents.ForEach(c => enclosingProjectBuilder.AddComponent(c.Name, c.ModuleType, c.Content));
560+
foreach (var testComponent in testComponents)
561+
{
562+
enclosingProjectBuilder.AddComponent(testComponent.Name, testComponent.ModuleType, testComponent.Content);
563+
}
564+
506565
var enclosingProject = enclosingProjectBuilder.Build();
507566
builder.AddProject(enclosingProject);
508-
return builder.Build().Object;
567+
return builder;
509568
}
510569

511570
private IVBComponent RetrieveComponent(AccessibilityTestsDataObject tdo, string componentName)

0 commit comments

Comments
 (0)