Skip to content

Commit 018c370

Browse files
committed
Leave imports that cannot be sorted alone instead of failing the file
ImportOrderer.reorderImports threw "Imports not contiguous (perhaps a comment separates them?)" when another import followed the imports it had scanned. Since #39 a comment between imports goes with the import after it, so the only input left that reaches this check is a stray `;` between two imports, which javac warns about up to --release 17 and rejects from 21 on. The formatter's own parser rejects it too, so such a file still fails, but formatSourceAndFixImports, which fixes the imports before it formats, reported the misleading message instead of the parse error. The orderer now returns the text unchanged there, as google/google-java-format#1528 does, and formatSourceAndFixImports reports "2:1: error: extraneous semicolon". The command line formats before it fixes the imports and reported the parse error already.
1 parent 9b11043 commit 018c370

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/ImportOrderer.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ private String reorderImports() throws FormatterException {
7676
ImportsAndIndex imports = scanImports(firstImportStart);
7777
int afterLastImport = imports.index;
7878

79-
// Make sure there are no more imports before the next class (etc) definition.
79+
// Another import before the next class (etc) definition means something other than whitespace and comments
80+
// separates the imports, such as a stray `;`, which javac rejects from Java 21 on. Leave the imports as they
81+
// are rather than fail the whole file (google/google-java-format#1528): the formatter reports what is wrong
82+
// with the file, or formats it if nothing is.
8083
Optional<Integer> maybeLaterImport = findIdentifier(afterLastImport, IMPORT_OR_CLASS_START);
8184
if (maybeLaterImport.isPresent() && tokenAt(maybeLaterImport.get()).equals("import")) {
82-
throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)");
85+
return text;
8386
}
8487

8588
StringBuilder result = new StringBuilder();

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,25 @@ void throwsFormatterException() throws Exception {
236236
.isInstanceOf(FormatterException.class);
237237
}
238238

239+
// Fixing the imports leaves imports it cannot sort alone, so the error reported is what is wrong with the file,
240+
// not "Imports not contiguous" (google/google-java-format#1528).
241+
@Test
242+
void straySemicolonBetweenImportsReportsTheParseError() {
243+
String input = """
244+
import b.B;
245+
;
246+
import a.A;
247+
248+
class T {
249+
A a;
250+
B b;
251+
}
252+
""";
253+
assertThatThrownBy(() -> FORMATTER.formatSourceAndFixImports(input))
254+
.isInstanceOf(FormatterException.class)
255+
.hasMessage("2:1: error: extraneous semicolon");
256+
}
257+
239258
@Test
240259
void blankLinesImportComment() throws FormatterException {
241260
String withBlank = """

‎open-java-format/src/test/java/com/palantir/javaformat/java/GoogleImportStyleTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,16 @@ public static List<Object[]> parameters() {
411411
"import com.foo.Third;",
412412
}
413413
},
414+
// Anything else between two imports leaves them all where they are, rather than failing the file
415+
// (google/google-java-format#1528).
416+
{
417+
{
418+
"import com.foo.Second;", //
419+
";",
420+
"import com.foo.First;",
421+
},
422+
{}
423+
},
414424
{
415425
{
416426
"import com.foo.Second; /* A block comment after an import stays with it. */", //

0 commit comments

Comments
 (0)