Skip to content

Commit fb5aa2d

Browse files
committed
Refactor some codes
1 parent dfd422f commit fb5aa2d

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/middleware/ReviewDogGenerator.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
public final class ReviewDogGenerator {
2828

29+
private static final String SOURCE = "spotless";
30+
2931
private ReviewDogGenerator() {
3032
// Prevent instantiation
3133
}
@@ -34,8 +36,8 @@ private ReviewDogGenerator() {
3436
* Generates a ReviewDog compatible JSON line (rdjsonl) for a diff between
3537
* the actual content and the formatted content of a file.
3638
*
37-
* @param path The file path
38-
* @param actualContent The content as it currently exists in the file
39+
* @param path The file path
40+
* @param actualContent The content as it currently exists in the file
3941
* @param formattedContent The content after formatting is applied
4042
* @return A string in rdjsonl format representing the diff
4143
*/
@@ -56,14 +58,12 @@ public static String rdjsonlDiff(String path, String actualContent, String forma
5658
* Generates ReviewDog compatible JSON lines (rdjsonl) for lint issues
5759
* identified by formatting steps.
5860
*
59-
* @param path The file path
60-
* @param formattedContent The content after formatting is applied
61-
* @param steps The list of formatter steps applied
62-
* @param lintsPerStep The list of lints produced by each step
61+
* @param path The file path
62+
* @param steps The list of formatter steps applied
63+
* @param lintsPerStep The list of lints produced by each step
6364
* @return A string in rdjsonl format representing the lints
6465
*/
65-
public static String rdjsonlLints(String path, String formattedContent,
66-
List<FormatterStep> steps, List<List<Lint>> lintsPerStep) {
66+
public static String rdjsonlLints(String path, List<FormatterStep> steps, List<List<Lint>> lintsPerStep) {
6767
if (lintsPerStep == null || lintsPerStep.isEmpty()) {
6868
return "";
6969
}
@@ -111,13 +111,23 @@ private static String createUnifiedDiff(String path, String actualContent, Strin
111111
/**
112112
* Formats a single lint issue as a JSON line.
113113
*/
114-
private static String formatLintAsJson(String path, Lint lint, String stepName) {
114+
private static String formatLintAsJson(String path, Lint lint, String ruleCode) {
115115
return String.format(
116-
"{\"message\":{\"path\":\"%s\",\"line\":%d,\"column\":1,\"message\":\"%s: %s\"}}",
116+
"{"
117+
+ "\"source\":\"%s\","
118+
+ "\"code\":\"%s\","
119+
+ "\"level\":\"warning\","
120+
+ "\"message\":\"%s\","
121+
+ "\"path\":\"%s\","
122+
+ "\"line\":%d,"
123+
+ "\"column\":%d"
124+
+ "}",
125+
escapeJson(SOURCE),
126+
escapeJson(ruleCode),
127+
escapeJson(lint.getDetail()),
117128
escapeJson(path),
118129
lint.getLineStart(),
119-
escapeJson(lint.getShortCode()),
120-
escapeJson(lint.getDetail()));
130+
1);
121131
}
122132

123133
/**

lib-extra/src/test/java/com/diffplug/spotless/extra/middleware/ReviewDogGeneratorTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void lintsEmpty() {
6666
List<FormatterStep> steps = new ArrayList<>();
6767
List<List<Lint>> lintsPerStep = new ArrayList<>();
6868

69-
String result = ReviewDogGenerator.rdjsonlLints("test.txt", "content", steps, lintsPerStep);
69+
String result = ReviewDogGenerator.rdjsonlLints("test.txt", steps, lintsPerStep);
7070
assertEquals("", result);
7171
}
7272

@@ -81,12 +81,13 @@ public void lintsSingleIssue() {
8181
Lint lint = Lint.atLine(1, "TEST001", "Test lint message");
8282
List<List<Lint>> lintsPerStep = Collections.singletonList(Collections.singletonList(lint));
8383

84-
String result = ReviewDogGenerator.rdjsonlLints("src/main.java", "content", steps, lintsPerStep);
84+
String result = ReviewDogGenerator.rdjsonlLints("src/main.java", steps, lintsPerStep);
8585

8686
assertNotNull(result);
8787
assertTrue(result.contains("\"path\":\"src/main.java\""));
8888
assertTrue(result.contains("\"line\":1"));
89-
assertTrue(result.contains("TEST001: Test lint message"));
89+
assertTrue(result.contains("\"message\":\"Test lint message\""));
90+
assertTrue(result.contains("\"code\":\"testStep\""));
9091
}
9192

9293
@Test
@@ -120,13 +121,13 @@ public void close() {}
120121
Collections.singletonList(lint1),
121122
Collections.singletonList(lint2));
122123

123-
String result = ReviewDogGenerator.rdjsonlLints("src/main.java", "content", steps, lintsPerStep);
124+
String result = ReviewDogGenerator.rdjsonlLints("src/main.java", steps, lintsPerStep);
124125

125126
assertNotNull(result);
126-
assertTrue(result.contains("RULE1"));
127-
assertTrue(result.contains("RULE2"));
128-
assertTrue(result.contains("First issue"));
129-
assertTrue(result.contains("Second issue"));
127+
assertTrue(result.contains("\"code\":\"step1\""));
128+
assertTrue(result.contains("\"code\":\"step2\""));
129+
assertTrue(result.contains("\"message\":\"First issue\""));
130+
assertTrue(result.contains("\"message\":\"Second issue\""));
130131

131132
String[] lines = result.split("\n");
132133
assertEquals(2, lines.length);

0 commit comments

Comments
 (0)