Skip to content

Commit 43cd94b

Browse files
committed
Make an explicit constant for a lint at an undefined line.
1 parent 29cba64 commit 43cd94b

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,21 @@
2727
* to Spotless, then it is by definition.
2828
*/
2929
public final class Lint implements Serializable {
30-
/** Returns a runtime exception which, if thrown, will lint the entire file. */
31-
public static ShortcutException entireFile(String code, String msg) {
32-
return new ShortcutException(Lint.create(code, msg, -1));
30+
/** Returns a runtime exception which, if thrown, will create a lint at an undefined line. */
31+
public static ShortcutException atUndefinedLine(String code, String msg) {
32+
return new ShortcutException(Lint.create(code, msg, LINE_UNDEFINED));
3333
}
3434

3535
/** Returns a runtime exception which, if thrown, will lint a specific line. */
3636
public static ShortcutException atLine(int line, String code, String msg) {
3737
return new ShortcutException(Lint.create(code, msg, line));
3838
}
3939

40+
/** Returns a runtime exception which, if thrown, will lint a specific line range. */
41+
public static ShortcutException atLineRange(int lineStart, int lineEnd, String code, String msg) {
42+
return new ShortcutException(Lint.create(code, msg, lineStart, lineEnd));
43+
}
44+
4045
/** Any exception which implements this interface will have its lints extracted and reported cleanly to the user. */
4146
public interface Has {
4247
List<Lint> getLints();
@@ -103,7 +108,11 @@ public String getMsg() {
103108
@Override
104109
public String toString() {
105110
if (lineStart == lineEnd) {
106-
return lineStart + ": (" + code + ") " + msg;
111+
if (lineStart == LINE_UNDEFINED) {
112+
return "LINE_UNDEFINED: (" + code + ") " + msg;
113+
} else {
114+
return lineStart + ": (" + code + ") " + msg;
115+
}
107116
} else {
108117
return lineStart + "-" + lineEnd + ": (" + code + ") " + msg;
109118
}
@@ -163,4 +172,6 @@ private static String msgFrom(String message) {
163172
}
164173
return "";
165174
}
175+
176+
public static final int LINE_UNDEFINED = -1;
166177
}

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void writeBuild(String... toInsert) throws IOException {
4343
lines.add(" target file('README.md')");
4444
lines.add(" custom 'no swearing', {");
4545
lines.add(" if (it.toLowerCase(Locale.ROOT).contains('fubar')) {");
46-
lines.add(" throw com.diffplug.spotless.Lint.entireFile('swearing', 'No swearing!');");
46+
lines.add(" throw com.diffplug.spotless.Lint.atUndefinedLine('swearing', 'No swearing!');");
4747
lines.add(" }");
4848
lines.add(" }");
4949
lines.addAll(Arrays.asList(toInsert));
@@ -68,8 +68,8 @@ void anyExceptionShouldFail() throws Exception {
6868
runWithFailure(
6969
"> Task :spotlessMisc FAILED\n" +
7070
"Step 'no swearing' found problem in 'README.md':\n" +
71-
"-1: (swearing) No swearing!\n" +
72-
"java.lang.Throwable: -1: (swearing) No swearing!");
71+
"LINE_UNDEFINED: (swearing) No swearing!\n" +
72+
"java.lang.Throwable: LINE_UNDEFINED: (swearing) No swearing!");
7373
}
7474

7575
@Test
@@ -113,8 +113,8 @@ void failsIfNeitherStepNorFileExempted() throws Exception {
113113
setFile("README.md").toContent("This code is fubar.");
114114
runWithFailure("> Task :spotlessMisc FAILED\n" +
115115
"Step 'no swearing' found problem in 'README.md':\n" +
116-
"-1: (swearing) No swearing!\n" +
117-
"java.lang.Throwable: -1: (swearing) No swearing!");
116+
"LINE_UNDEFINED: (swearing) No swearing!\n" +
117+
"java.lang.Throwable: LINE_UNDEFINED: (swearing) No swearing!");
118118
}
119119

120120
private void runWithSuccess(String expectedToStartWith) throws Exception {

0 commit comments

Comments
 (0)