Merge pull request #43 from openjavaformat/unused-import-blank-line · openjavaformat/open-java-format@f38b759 · GitHub
Skip to content

Commit f38b759

Browse files
authored
Merge pull request #43 from openjavaformat/unused-import-blank-line
Stop an unused import from leaving two blank lines behind
2 parents c6f717f + 3632838 commit f38b759

3 files changed

Lines changed: 158 additions & 3 deletions

File tree

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

Lines changed: 50 additions & 3 deletions

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,46 @@ public void importRemovalLines() throws Exception {
266266
assertThat(out.toString()).isEqualTo(joiner.join(expected));
267267
}
268268

269+
// An unused import between two blank lines must not leave both of them behind: one run of the command line gives
270+
// what a second run would, and what the entry point of the Gradle and Spotless step gives (#37, from
271+
// google/google-java-format#1436).
272+
@Test
273+
public void unusedImportRemovalLeavesOneBlankLine() throws Exception {
274+
String[] input = {
275+
"package com.example;",
276+
"",
277+
"import static io.grpc.MethodDescriptor.generateFullMethodName;",
278+
"",
279+
"/**",
280+
" * Javadoc for class.",
281+
" */",
282+
"public class TestBug {",
283+
"}",
284+
"",
285+
};
286+
String[] expected = {
287+
"package com.example;", //
288+
"",
289+
"/**",
290+
" * Javadoc for class.",
291+
" */",
292+
"public class TestBug {}",
293+
"",
294+
};
295+
StringWriter out = new StringWriter();
296+
Main main = new Main(
297+
new PrintWriter(out, true),
298+
new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.err, UTF_8)), true),
299+
new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
300+
assertThat(main.format("-")).isEqualTo(0);
301+
assertThat(out.toString()).isEqualTo(joiner.join(expected));
302+
303+
Formatter formatter = Formatter.createFormatter(JavaFormatterOptions.builder()
304+
.style(JavaFormatterOptions.Style.OJF)
305+
.build());
306+
assertThat(formatter.formatSourceAndFixImports(joiner.join(input))).isEqualTo(joiner.join(expected));
307+
}
308+
269309
// test that errors are reported on the right line when imports are removed
270310
@Test
271311
public void importRemoveErrorParseError() throws Exception {

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

Lines changed: 68 additions & 0 deletions

0 commit comments

Comments
 (0)