Skip to content

Commit b6d5a12

Browse files
committed
Add a --skip-removing-unused-imports flag to disable unused import removal
MOE_MIGRATED_REVID=142208872
1 parent 7e2e9c5 commit b6d5a12

File tree

8 files changed

+108
-11
lines changed

8 files changed

+108
-11
lines changed

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptions.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class CommandLineOptions {
3737
private final boolean fixImportsOnly;
3838
private final boolean removeJavadocOnlyImports;
3939
private final boolean sortImports;
40+
private final boolean removeUnusedImports;
4041

4142
CommandLineOptions(
4243
ImmutableList<String> files,
@@ -50,7 +51,8 @@ final class CommandLineOptions {
5051
boolean stdin,
5152
boolean fixImportsOnly,
5253
boolean removeJavadocOnlyImports,
53-
boolean sortImports) {
54+
boolean sortImports,
55+
boolean removeUnusedImports) {
5456
this.files = files;
5557
this.inPlace = inPlace;
5658
this.lines = lines;
@@ -63,6 +65,7 @@ final class CommandLineOptions {
6365
this.fixImportsOnly = fixImportsOnly;
6466
this.removeJavadocOnlyImports = removeJavadocOnlyImports;
6567
this.sortImports = sortImports;
68+
this.removeUnusedImports = removeUnusedImports;
6669
}
6770

6871
/** The files to format. */
@@ -128,6 +131,11 @@ boolean sortImports() {
128131
return sortImports;
129132
}
130133

134+
/** Remove unused imports. */
135+
boolean removeUnusedImports() {
136+
return removeUnusedImports;
137+
}
138+
131139
/** Returns true if partial formatting was selected. */
132140
boolean isSelection() {
133141
return !lines().isEmpty() || !offsets().isEmpty() || !lengths().isEmpty();
@@ -151,6 +159,7 @@ static class Builder {
151159
private Boolean fixImportsOnly = false;
152160
private Boolean removeJavadocOnlyImports = false;
153161
private Boolean sortImports = true;
162+
private Boolean removeUnusedImports = true;
154163

155164
ImmutableList.Builder<String> filesBuilder() {
156165
return files;
@@ -210,6 +219,11 @@ Builder sortImports(boolean sortImports) {
210219
return this;
211220
}
212221

222+
Builder removeUnusedImports(boolean removeUnusedImports) {
223+
this.removeUnusedImports = removeUnusedImports;
224+
return this;
225+
}
226+
213227
CommandLineOptions build() {
214228
return new CommandLineOptions(
215229
this.files.build(),
@@ -223,7 +237,8 @@ CommandLineOptions build() {
223237
this.stdin,
224238
this.fixImportsOnly,
225239
this.removeJavadocOnlyImports,
226-
this.sortImports);
240+
this.sortImports,
241+
this.removeUnusedImports);
227242
}
228243
}
229244
}

core/src/main/java/com/google/googlejavaformat/java/CommandLineOptionsParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ static CommandLineOptions parse(Iterable<String> options) {
9292
case "--skip-sorting-imports":
9393
optionsBuilder.sortImports(false);
9494
break;
95+
case "--skip-removing-unused-imports":
96+
optionsBuilder.removeUnusedImports(false);
97+
break;
9598
case "-":
9699
optionsBuilder.stdin(true);
97100
break;

core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ public String call() throws FormatterException {
4949
}
5050

5151
private String fixImports(String input) throws FormatterException {
52-
input =
53-
RemoveUnusedImports.removeUnusedImports(
54-
input,
55-
parameters.removeJavadocOnlyImports()
56-
? JavadocOnlyImports.REMOVE
57-
: JavadocOnlyImports.KEEP);
52+
if (parameters.removeUnusedImports()) {
53+
input =
54+
RemoveUnusedImports.removeUnusedImports(
55+
input,
56+
parameters.removeJavadocOnlyImports()
57+
? JavadocOnlyImports.REMOVE
58+
: JavadocOnlyImports.KEEP);
59+
}
5860
if (parameters.sortImports()) {
5961
input = ImportOrderer.reorderImports(input);
6062
}

core/src/main/java/com/google/googlejavaformat/java/UsageException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public final class UsageException extends Exception {
4242
" Fix import order and remove any unused imports, but do no other formatting.",
4343
" --skip-sorting-imports",
4444
" Do not fix the import order. Unused imports will still be removed.",
45+
" --skip-removing-unused-imports",
46+
" Do not remove unused imports. Imports will still be sorted.",
4547
" --length, -length",
4648
" Character length to format.",
4749
" --lines, -lines, --line, -line",

core/src/test/java/com/google/googlejavaformat/java/CommandLineOptionsParserTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void defaults() {
4141
assertThat(options.inPlace()).isFalse();
4242
assertThat(options.version()).isFalse();
4343
assertThat(options.sortImports()).isTrue();
44+
assertThat(options.removeUnusedImports()).isTrue();
4445
}
4546

4647
@Test
@@ -112,8 +113,15 @@ public void version() {
112113
@Test
113114
public void skipSortingImports() {
114115
assertThat(
115-
CommandLineOptionsParser.parse(Arrays.asList("--skip-sorting-imports"))
116-
.sortImports())
116+
CommandLineOptionsParser.parse(Arrays.asList("--skip-sorting-imports")).sortImports())
117+
.isFalse();
118+
}
119+
120+
@Test
121+
public void skipRemovingUnusedImports() {
122+
assertThat(
123+
CommandLineOptionsParser.parse(Arrays.asList("--skip-removing-unused-imports"))
124+
.removeUnusedImports())
117125
.isFalse();
118126
}
119127

core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ public void importOrderingAndFormatting() throws IOException, UsageException {
263263
public void formattingWithoutImportOrdering() throws IOException, UsageException {
264264
importOrdering(
265265
"--skip-sorting-imports",
266-
"com/google/googlejavaformat/java/testimports/A.formatting-only");
266+
"com/google/googlejavaformat/java/testimports/A.formatting-and-unused-import-removal");
267+
}
268+
269+
@Test
270+
public void formattingWithoutRemovingUnusedImports() throws IOException, UsageException {
271+
importOrdering(
272+
"--skip-removing-unused-imports",
273+
"com/google/googlejavaformat/java/testimports/A.formatting-and-import-sorting");
267274
}
268275

269276
private void importOrdering(String sortArg, String outputResourceName)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.google.example;
2+
3+
import static com.google.truth.Truth.assertThat;
4+
import static org.junit.Assert.fail;
5+
6+
import com.google.common.base.Preconditions;
7+
import java.util.List;
8+
import java.util.Set;
9+
import javax.annotations.Nullable;
10+
import org.junit.runner.RunWith;
11+
import org.junit.runners.JUnit4;
12+
13+
@RunWith(JUnit4.class)
14+
public class SomeTest {
15+
16+
<T> void check(@Nullable List<T> x) {
17+
Preconditions.checkNodeNull(x);
18+
}
19+
20+
void f() {
21+
List<String> xs = null;
22+
assertThat(xs).isNull();
23+
try {
24+
check(xs);
25+
fail();
26+
} catch (NullPointerException e) {
27+
}
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.google.example;
2+
3+
import com.google.common.base.Preconditions;
4+
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.JUnit4;
7+
8+
import java.util.List;
9+
10+
import javax.annotations.Nullable;
11+
12+
import static org.junit.Assert.fail;
13+
import static com.google.truth.Truth.assertThat;
14+
15+
@RunWith(JUnit4.class)
16+
public class SomeTest {
17+
18+
<T> void check(@Nullable List<T> x) {
19+
Preconditions.checkNodeNull(x);
20+
}
21+
22+
void f() {
23+
List<String> xs = null;
24+
assertThat(xs).isNull();
25+
try {
26+
check(xs);
27+
fail();
28+
} catch (NullPointerException e) {
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)