Skip to content

Commit 7091b97

Browse files
committed
lintify DiktatStep
1 parent 195bc16 commit 7091b97

File tree

6 files changed

+48
-34
lines changed

6 files changed

+48
-34
lines changed

lib/src/compatDiktat1Dot2Dot5/java/com/diffplug/spotless/glue/diktat/compat/DiktatCompat1Dot2Dot5Adapter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -77,9 +77,7 @@ public String format(final File file, final String content, final boolean isScri
7777
false,
7878
new EditorConfigOverride(),
7979
false));
80-
81-
DiktatReporting.reportIfRequired(errors, LintError::getLine, LintError::getCol, LintError::getDetail);
82-
80+
DiktatReporting.reportIfRequired(errors, LintError::getLine, LintError::getRuleId, LintError::getDetail);
8381
return result;
8482
}
8583
}

lib/src/compatDiktat2Dot0Dot0/java/com/diffplug/spotless/glue/diktat/compat/DiktatCompat2Dot0Dot0Adapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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.
@@ -49,7 +49,7 @@ public DiktatCompat2Dot0Dot0Adapter(@Nullable File configFile) {
4949
public String format(File file, String content, boolean isScript) {
5050
errors.clear();
5151
String result = processor.fix(content, file.toPath(), formatterCallback);
52-
DiktatReporting.reportIfRequired(errors, DiktatError::getLine, DiktatError::getCol, DiktatError::getDetail);
52+
DiktatReporting.reportIfRequired(errors, DiktatError::getLine, DiktatError::getRuleId, DiktatError::getDetail);
5353
return result;
5454
}
5555

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-2024 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,28 +15,45 @@
1515
*/
1616
package com.diffplug.spotless.glue.diktat.compat;
1717

18+
import java.io.Serializable;
19+
import java.util.ArrayList;
1820
import java.util.List;
1921
import java.util.function.Function;
2022
import java.util.function.ToIntFunction;
2123

22-
interface DiktatReporting {
24+
public interface DiktatReporting {
25+
class Lint implements Serializable {
26+
private static final long serialVersionUID = 1L;
27+
public final int line;
28+
public final String ruleId;
29+
public final String detail;
30+
31+
Lint(int line, String ruleId, String detail) {
32+
this.line = line;
33+
this.ruleId = ruleId;
34+
this.detail = detail;
35+
}
36+
}
37+
38+
class LintException extends RuntimeException {
39+
public final List<Lint> lints;
40+
41+
LintException(List<Lint> lints) {
42+
this.lints = lints;
43+
}
44+
}
45+
2346
static <T> void reportIfRequired(
2447
List<T> errors,
2548
ToIntFunction<T> lineGetter,
26-
ToIntFunction<T> columnGetter,
49+
Function<T, String> codeGetter,
2750
Function<T, String> detailGetter) {
2851
if (!errors.isEmpty()) {
29-
StringBuilder error = new StringBuilder();
30-
error.append("There are ").append(errors.size()).append(" unfixed errors:");
52+
var lints = new ArrayList<Lint>();
3153
for (T er : errors) {
32-
error.append(System.lineSeparator())
33-
.append("Error on line: ").append(lineGetter.applyAsInt(er))
34-
.append(", column: ").append(columnGetter.applyAsInt(er))
35-
.append(" cannot be fixed automatically")
36-
.append(System.lineSeparator())
37-
.append(detailGetter.apply(er));
54+
lints.add(new Lint(lineGetter.applyAsInt(er), codeGetter.apply(er), detailGetter.apply(er)));
3855
}
39-
throw new AssertionError(error);
56+
throw new LintException(lints);
4057
}
4158
}
4259
}

lib/src/diktat/java/com/diffplug/spotless/glue/diktat/DiktatFormatterFunc.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 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.
@@ -16,11 +16,14 @@
1616
package com.diffplug.spotless.glue.diktat;
1717

1818
import java.io.File;
19+
import java.util.stream.Collectors;
1920

2021
import com.diffplug.spotless.FormatterFunc;
22+
import com.diffplug.spotless.Lint;
2123
import com.diffplug.spotless.glue.diktat.compat.DiktatCompat1Dot2Dot5Adapter;
2224
import com.diffplug.spotless.glue.diktat.compat.DiktatCompat2Dot0Dot0Adapter;
2325
import com.diffplug.spotless.glue.diktat.compat.DiktatCompatAdapter;
26+
import com.diffplug.spotless.glue.diktat.compat.DiktatReporting;
2427

2528
public class DiktatFormatterFunc implements FormatterFunc.NeedsFile {
2629
private final DiktatCompatAdapter adapter;
@@ -40,6 +43,10 @@ public DiktatFormatterFunc(
4043

4144
@Override
4245
public String applyWithFile(String unix, File file) {
43-
return adapter.format(file, unix, isScript);
46+
try {
47+
return adapter.format(file, unix, isScript);
48+
} catch (DiktatReporting.LintException e) {
49+
throw Lint.shortcut(e.lints.stream().map(lint -> Lint.atLine(lint.line, lint.ruleId, lint.detail)).collect(Collectors.toList()));
50+
}
4451
}
4552
}

lib/src/main/java/com/diffplug/spotless/kotlin/DiktatStep.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.diffplug.spotless.kotlin;
1717

1818
import java.io.File;
19-
import java.io.IOException;
2019
import java.io.Serializable;
2120
import java.lang.reflect.Constructor;
2221
import java.util.*;
@@ -95,7 +94,7 @@ static final class State implements Serializable {
9594
private final boolean isScript;
9695
private final @Nullable FileSignature config;
9796

98-
State(JarState jar, String versionDiktat, boolean isScript, @Nullable FileSignature config) throws IOException {
97+
State(JarState jar, String versionDiktat, boolean isScript, @Nullable FileSignature config) {
9998
this.jar = jar;
10099
this.versionDiktat = versionDiktat;
101100
this.isScript = isScript;

testlib/src/test/java/com/diffplug/spotless/kotlin/DiktatStepTest.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 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.
@@ -29,15 +29,11 @@
2929
import com.diffplug.spotless.TestProvisioner;
3030

3131
class DiktatStepTest extends ResourceHarness {
32-
3332
@Test
3433
void behavior() {
3534
FormatterStep step = DiktatStep.create(TestProvisioner.mavenCentral());
36-
StepHarnessWithFile.forStep(this, step).testResourceExceptionMsg("kotlin/diktat/Unsolvable.kt").isEqualTo("There are 2 unfixed errors:" +
37-
System.lineSeparator() + "Error on line: 12, column: 9 cannot be fixed automatically" +
38-
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()" +
39-
System.lineSeparator() + "Error on line: 13, column: 9 cannot be fixed automatically" +
40-
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()");
35+
StepHarnessWithFile.forStep(this, step).expectLintsOfResource("kotlin/diktat/Unsolvable.kt").toBe("L12 diktat(diktat-ruleset:debug-print) [DEBUG_PRINT] use a dedicated logging library: found println()",
36+
"L13 diktat(diktat-ruleset:debug-print) [DEBUG_PRINT] use a dedicated logging library: found println()");
4137
}
4238

4339
@Test
@@ -47,11 +43,8 @@ void behaviorConf() throws Exception {
4743
FileSignature config = signAsList(conf);
4844

4945
FormatterStep step = DiktatStep.create("1.2.1", TestProvisioner.mavenCentral(), config);
50-
StepHarnessWithFile.forStep(this, step).testResourceExceptionMsg("kotlin/diktat/Unsolvable.kt").isEqualTo("There are 2 unfixed errors:" +
51-
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
52-
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()" +
53-
System.lineSeparator() + "Error on line: 13, column: 9 cannot be fixed automatically" +
54-
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()");
46+
StepHarnessWithFile.forStep(this, step).expectLintsOfResource("kotlin/diktat/Unsolvable.kt").toBe("L1 diktat(diktat-ruleset:debug-print) [DEBUG_PRINT] use a dedicated logging library: found println()",
47+
"L13 diktat(diktat-ruleset:debug-print) [DEBUG_PRINT] use a dedicated logging library: found println()");
5548
}
5649

5750
@Test

0 commit comments

Comments
 (0)