Skip to content

Commit

Permalink
Fix bug in order imports related to star folding
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jun 16, 2020
1 parent 9dfc098 commit 58e64dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,6 @@ public List<J.Import> orderedImports() {
int consecutiveSamePackages = 0;
for (int i = 0; i < imports.size(); i++) {
consecutiveSamePackages++;
if ("*".equals(imports.get(i).getQualid().getSimpleName())) {
foundStar = true;
}
if (i > 1 && !imports.get(i - 1).getPackageName().equals(imports.get(i).getPackageName())) {
int threshold = statik ? nameCountToUseStarImport : classCountToUseStarImport;
if (consecutiveSamePackages >= threshold || (consecutiveSamePackages > 1 && foundStar)) {
Expand All @@ -259,6 +256,9 @@ public List<J.Import> orderedImports() {
consecutiveSamePackages = 1;
foundStar = false;
}
if ("*".equals(imports.get(i).getQualid().getSimpleName())) {
foundStar = true;
}
}

return imports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,27 @@ interface OrderImportTest {
public class A {}
""".trimIndent())
}
}

@Test
fun twoImportsFollowedByStar(jp: JavaParser) {
val a = jp.parse("""
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.files.*;
public class A {}
""".trimIndent())

val fixed = a.refactor().visit(OrderImports.intellij().apply {
setRemoveUnused(false)
}).fix().fixed

assertRefactored(fixed, """
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.files.*;
public class A {}
""".trimIndent())
}
}

0 comments on commit 58e64dd

Please sign in to comment.