Skip to content

Commit a93a424

Browse files
author
Vincent Potucek
committed
removeWildcardImports: throw new AssertionError instead of removing
1 parent b7c829e commit a93a424

File tree

10 files changed

+80
-12
lines changed

10 files changed

+80
-12
lines changed

lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.generic;
1717

18+
import static com.diffplug.spotless.Lint.atUndefinedLine;
19+
1820
import java.io.Serializable;
1921
import java.util.Objects;
2022
import java.util.regex.Pattern;
@@ -35,6 +37,15 @@ public static FormatterStep create(String name, String regex, String replacement
3537
State::toFormatter);
3638
}
3739

40+
public static FormatterStep lint(String name, String regex, String error) {
41+
Objects.requireNonNull(name, "name");
42+
Objects.requireNonNull(regex, "regex");
43+
Objects.requireNonNull(error, "error");
44+
return FormatterStep.createLazy(name,
45+
() -> new State(Pattern.compile(regex, Pattern.UNIX_LINES | Pattern.MULTILINE), error),
46+
State::toLinter);
47+
}
48+
3849
private static final class State implements Serializable {
3950
private static final long serialVersionUID = 1L;
4051

@@ -49,5 +60,14 @@ private static final class State implements Serializable {
4960
FormatterFunc toFormatter() {
5061
return raw -> regex.matcher(raw).replaceAll(replacement);
5162
}
63+
64+
FormatterFunc toLinter() {
65+
return raw -> {
66+
if (regex.matcher(raw).find()) {
67+
throw atUndefinedLine("", replacement).shortcut();
68+
}
69+
return raw;
70+
};
71+
}
5272
}
5373
}

lib/src/main/java/com/diffplug/spotless/java/RemoveWildcardImportsStep.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020

2121
/** Removes any wildcard import statements. */
2222
public final class RemoveWildcardImportsStep {
23+
24+
/**
25+
* Matches lines like 'import foo.*;' or 'import static foo.*;'.
26+
*/
27+
private static final String REGEX = "(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?";
28+
private static final String NAME = "removeWildcardImports";
29+
private static final String ERROR = "Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.";
30+
2331
private RemoveWildcardImportsStep() {}
2432

2533
public static FormatterStep create() {
26-
// Matches lines like 'import foo.*;' or 'import static foo.*;'.
27-
return ReplaceRegexStep.create(
28-
"removeWildcardImports",
29-
"(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?",
30-
"");
34+
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);
3135
}
3236
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import com.diffplug.spotless.ResourceHarness;
4747

4848
public class MavenIntegrationHarness extends ResourceHarness {
49+
50+
protected static final String PATH = "src/main/java/test.java";
4951
/**
5052
* To run tests in the IDE, run {@code gradlew :plugin-maven:changelogPrint}, then
5153
* put the last version it prints into {@code SPOTLESS_MAVEN_VERSION_IDE}. From now

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,40 @@
1515
*/
1616
package com.diffplug.spotless.maven.java;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.BeforeEach;
1821
import org.junit.jupiter.api.Test;
1922

2023
import com.diffplug.spotless.maven.MavenIntegrationHarness;
2124

2225
class RemoveWildcardImportsStepTest extends MavenIntegrationHarness {
2326

27+
private static final String ERROR = "Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.";
28+
29+
@BeforeEach
30+
void init() throws Exception {
31+
writePomWithJavaSteps("<removeWildcardImports/>");
32+
}
33+
2434
@Test
2535
void testRemoveWildcardImports() throws Exception {
26-
writePomWithJavaSteps("<removeWildcardImports/>");
36+
setFile(PATH).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
37+
assertFile(PATH).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
38+
assertThat(mavenRunner().withArguments("spotless:apply").runHasError().stdOutUtf8()).contains(ERROR);
39+
}
2740

28-
String path = "src/main/java/test.java";
29-
setFile(path).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
41+
@Test
42+
void testRemoveStaticWildcardImports() throws Exception {
43+
setFile(PATH).toResource("java/removewildcardimports/JavaCodeStaticWildcardsUnformatted.test");
44+
assertFile(PATH).sameAsResource("java/removewildcardimports/JavaCodeStaticWildcardsFormatted.test");
45+
assertThat(mavenRunner().withArguments("spotless:apply").runHasError().stdOutUtf8()).contains(ERROR);
46+
}
47+
48+
@Test
49+
void testRemoveWildcardImportsNoError() throws Exception {
50+
setFile(PATH).toResource("java/removewildcardimports/JavaCodeNoWildcardsUnformatted.test");
51+
assertFile(PATH).sameAsResource("java/removewildcardimports/JavaCodeNoWildcardsUnformatted.test");
3052
mavenRunner().withArguments("spotless:apply").runNoError();
31-
assertFile(path).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
3253
}
3354
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import java.util.List;
2+
import mylib.Helper;
3+
4+
public class Test {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import java.util.List;
2+
import mylib.Helper;
3+
4+
public class Test {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java.util.List;
2+
import mylib.Helper;
3+
import static io.quarkus.vertx.web.Route.HttpMethod.*;
4+
import static org.springframework.web.reactive.function.BodyInserters.*;
5+
6+
public class Test {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import java.util.List;
2+
import mylib.Helper;
3+
import static io.quarkus.vertx.web.Route.HttpMethod.*;
4+
import static org.springframework.web.reactive.function.BodyInserters.*;
5+
6+
public class Test {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import java.util.*;
2+
import static java.util.Collections.*;
13
import java.util.List;
24
import mylib.Helper;
5+
import io.quarkus.maven.dependency.*;
36

47
public class Test {}

testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsUnformatted.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ import static java.util.Collections.*;
33
import java.util.List;
44
import mylib.Helper;
55
import io.quarkus.maven.dependency.*;
6-
import static io.quarkus.vertx.web.Route.HttpMethod.*;
7-
import static org.springframework.web.reactive.function.BodyInserters.*;
86

97
public class Test {}

0 commit comments

Comments
 (0)