Skip to content

Commit 322d5bf

Browse files
committed
Add a lint method to the core interfaces: Formatter[Step|Func]
1 parent 777fd5c commit 322d5bf

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.diffplug.spotless;
1717

1818
import java.io.File;
19+
import java.util.List;
1920
import java.util.Objects;
2021

2122
/**
@@ -32,6 +33,14 @@ default String apply(String unix, File file) throws Exception {
3233
return apply(unix);
3334
}
3435

36+
/**
37+
* Calculates a list of lints against the given content.
38+
* By default, that's just an throwables thrown by the lint.
39+
*/
40+
default List<Lint> lint(String content, File file) throws Exception {
41+
return List.of();
42+
}
43+
3544
/**
3645
* {@code Function<String, String>} and {@code BiFunction<String, File, String>} whose implementation
3746
* requires a resource which should be released when the function is no longer needed.
@@ -74,6 +83,14 @@ public String apply(String unix) throws Exception {
7483
@FunctionalInterface
7584
interface ResourceFunc<T extends AutoCloseable> {
7685
String apply(T resource, String unix) throws Exception;
86+
87+
/**
88+
* Calculates a list of lints against the given content.
89+
* By default, that's just an throwables thrown by the lint.
90+
*/
91+
default List<Lint> lint(T resource, String unix) throws Exception {
92+
return List.of();
93+
}
7794
}
7895

7996
/** Creates a {@link FormatterFunc.Closeable} which uses the given resource to execute the format function. */
@@ -101,6 +118,10 @@ public String apply(String unix) throws Exception {
101118
@FunctionalInterface
102119
interface ResourceFuncNeedsFile<T extends AutoCloseable> {
103120
String apply(T resource, String unix, File file) throws Exception;
121+
122+
default List<Lint> lint(T resource, String content, File file) throws Exception {
123+
return List.of();
124+
}
104125
}
105126

106127
/** Creates a {@link FormatterFunc.Closeable} which uses the given resource to execute the file-dependent format function. */
@@ -123,6 +144,11 @@ public String apply(String unix, File file) throws Exception {
123144
public String apply(String unix) throws Exception {
124145
return apply(unix, Formatter.NO_FILE_SENTINEL);
125146
}
147+
148+
@Override
149+
public List<Lint> lint(String content, File file) throws Exception {
150+
return function.lint(resource, content, file);
151+
}
126152
};
127153
}
128154
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.File;
1919
import java.io.Serializable;
20+
import java.util.List;
2021
import java.util.Objects;
2122

2223
import javax.annotation.Nullable;
@@ -46,6 +47,22 @@ public interface FormatterStep extends Serializable, AutoCloseable {
4647
@Nullable
4748
String format(String rawUnix, File file) throws Exception;
4849

50+
/**
51+
* Returns a list of lints against the given file content
52+
*
53+
* @param content
54+
* the content to check
55+
* @param file
56+
* the file which {@code content} was obtained from; never null. Pass an empty file using
57+
* {@code new File("")} if and only if no file is actually associated with {@code content}
58+
* @return a list of lints
59+
* @throws Exception if the formatter step experiences a problem
60+
*/
61+
@Nullable
62+
default List<Lint> lint(String content, File file) throws Exception {
63+
return List.of();
64+
}
65+
4966
/**
5067
* Returns a new {@code FormatterStep} which, observing the value of {@code formatIfMatches},
5168
* will only apply, or not, its changes to files which pass the given filter.

0 commit comments

Comments
 (0)