Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug adding import to a list of imports containing duplicate imports. #4197

Merged
merged 4 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,21 @@ public List<JRightPadded<J.Import>> addImport(List<JRightPadded<J.Import>> origi
return singletonList(paddedToAdd);
}

// Do not add the import if it is already present.
JavaType addedType = paddedToAdd.getElement().getQualid().getType();
for (JRightPadded<J.Import> originalImport : originalImports) {
if (TypeUtils.isOfType(addedType, originalImport.getElement().getQualid().getType())) {
return originalImports;
}
}

// don't star fold just yet, because we are only going to star fold adjacent imports along with
// the import to add at most. we don't even want to star fold other non-adjacent imports in the same
// block that should be star folded according to the layout style (minimally invasive change).
List<JRightPadded<J.Import>> ideallyOrdered =
new ImportLayoutStyle(Integer.MAX_VALUE, Integer.MAX_VALUE, layout, packagesToFold)
.orderImports(ListUtils.concat(originalImports, paddedToAdd), new HashSet<>());

if (ideallyOrdered.size() == originalImports.size()) {
Set<String> originalPaths = new HashSet<>();
for (JRightPadded<J.Import> originalImport : originalImports) {
originalPaths.add(originalImport.getElement().getTypeName());
}
int sharedImports = 0;
for (JRightPadded<J.Import> importJRightPadded : ideallyOrdered) {
if (originalPaths.contains(importJRightPadded.getElement().getTypeName())) {
sharedImports++;
}
}
if (sharedImports == originalImports.size()) {
// must be a duplicate of an existing import
return originalImports;
}
}

JRightPadded<J.Import> before = null;
JRightPadded<J.Import> after = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.junit.jupiter.api.Test;
import org.openrewrite.Issue;
import org.openrewrite.config.DeclarativeNamedStyles;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JLeftPadded;
import org.openrewrite.java.tree.JRightPadded;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeTree;
import org.openrewrite.marker.Markers;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -80,4 +88,41 @@ void deserializeInDeclarativeNamedStyles() throws IOException {

mapper.readValue(mapper.writeValueAsBytes(style), DeclarativeNamedStyles.class);
}

@Test
@Issue("https://github.com/openrewrite/rewrite/issues/4196")
void addImportInPresenceOfDuplicateOtherImport() {
ImportLayoutStyle style = new ImportLayoutStyle(
Integer.MAX_VALUE, Integer.MAX_VALUE, Collections.emptyList(), Collections.emptyList());
JRightPadded<J.Import> import1 = new JRightPadded<>(
new J.Import(
randomId(),
Space.EMPTY,
Markers.EMPTY,
new JLeftPadded<>(Space.SINGLE_SPACE, true, Markers.EMPTY),
TypeTree.build("pkg.Clazz.MEMBER_1").withPrefix(Space.SINGLE_SPACE),
null),
Space.EMPTY,
Markers.EMPTY);
JRightPadded<J.Import> import2 = new JRightPadded<>(
new J.Import(
randomId(),
Space.EMPTY,
Markers.EMPTY,
new JLeftPadded<>(Space.SINGLE_SPACE, true, Markers.EMPTY),
TypeTree.build("pkg.Clazz.MEMBER_1").withPrefix(Space.SINGLE_SPACE),
null),
Space.EMPTY,
Markers.EMPTY);
J.Import importToAdd = new J.Import(
randomId(),
Space.EMPTY,
Markers.EMPTY,
new JLeftPadded<>(Space.SINGLE_SPACE, true, Markers.EMPTY),
TypeTree.build("pkg.Clazz.MEMBER_2").withPrefix(Space.SINGLE_SPACE),
null);
assertThat(style.addImport(List.of(import1, import2), importToAdd, null, Collections.emptyList()))
.containsExactlyInAnyOrder(
import1, import1, new JRightPadded<>(importToAdd, Space.EMPTY, Markers.EMPTY));
}
}
Loading