Skip to content

Commit d951b89

Browse files
authored
Upgrade diktat to 1.2.1 (and require 1.2.1+) (#1246)
2 parents bab36df + 9b0cf08 commit d951b89

File tree

11 files changed

+64
-46
lines changed

11 files changed

+64
-46
lines changed

Diff for: CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
15+
### Changed
16+
* Minimum required `diktat` version bumped to `1.2.0` ([#1246](https://github.com/diffplug/spotless/pull/1246))
17+
* Default bumped from `1.1.0` -> `1.2.0`
1518

1619
## [2.26.2] - 2022-06-11
1720
### Fixed

Diff for: lib/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dependencies {
5151
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-experimental:$VER_KTLINT"
5252
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-standard:$VER_KTLINT"
5353

54-
String VER_DIKTAT = "1.1.0"
54+
String VER_DIKTAT = "1.2.0"
5555
diktatCompileOnly "org.cqfn.diktat:diktat-rules:$VER_DIKTAT"
5656

5757
// used for markdown formatting

Diff for: lib/src/diktat/java/com/diffplug/spotless/glue/diktat/DiktatFormatterFunc.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import org.cqfn.diktat.ruleset.rules.DiktatRuleSetProvider;
2222

2323
import com.pinterest.ktlint.core.KtLint;
24-
import com.pinterest.ktlint.core.KtLint.Params;
2524
import com.pinterest.ktlint.core.LintError;
2625
import com.pinterest.ktlint.core.RuleSet;
26+
import com.pinterest.ktlint.core.api.EditorConfigOverride;
2727

2828
import com.diffplug.spotless.FormatterFunc;
2929

@@ -33,15 +33,13 @@
3333
public class DiktatFormatterFunc implements FormatterFunc.NeedsFile {
3434

3535
private final List<RuleSet> rulesets;
36-
private final Map<String, String> userData;
3736
private final Function2<? super LintError, ? super Boolean, Unit> formatterCallback;
3837
private final boolean isScript;
3938

4039
private final ArrayList<LintError> errors = new ArrayList<>();
4140

42-
public DiktatFormatterFunc(boolean isScript, Map<String, String> userData) {
41+
public DiktatFormatterFunc(boolean isScript) {
4342
rulesets = Collections.singletonList(new DiktatRuleSetProvider().get());
44-
this.userData = userData;
4543
this.formatterCallback = new FormatterCallback(errors);
4644
this.isScript = isScript;
4745
}
@@ -65,17 +63,18 @@ public Unit invoke(LintError lintError, Boolean corrected) {
6563
@Override
6664
public String applyWithFile(String unix, File file) throws Exception {
6765
errors.clear();
68-
userData.put("file_path", file.getAbsolutePath());
69-
String result = KtLint.INSTANCE.format(new Params(
66+
String result = KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
7067
// Unlike Ktlint, Diktat requires full path to the file.
7168
// See https://github.com/diffplug/spotless/issues/1189, https://github.com/analysis-dev/diktat/issues/1202
7269
file.getAbsolutePath(),
7370
unix,
7471
rulesets,
75-
userData,
72+
Collections.emptyMap(),
7673
formatterCallback,
7774
isScript,
7875
null,
76+
false,
77+
new EditorConfigOverride(),
7978
false));
8079

8180
if (!errors.isEmpty()) {

Diff for: lib/src/main/java/com/diffplug/spotless/kotlin/DiktatStep.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public class DiktatStep {
3030
// prevent direct instantiation
3131
private DiktatStep() {}
3232

33-
private static final String DEFAULT_VERSION = "1.1.0";
33+
private static final String MIN_SUPPORTED_VERSION = "1.2.0";
34+
35+
private static final String DEFAULT_VERSION = "1.2.0";
3436
static final String NAME = "diktat";
3537
static final String PACKAGE_DIKTAT = "org.cqfn.diktat";
3638
static final String MAVEN_COORDINATE = PACKAGE_DIKTAT + ":diktat-rules:";
@@ -44,30 +46,25 @@ public static FormatterStep create(Provisioner provisioner) {
4446
}
4547

4648
public static FormatterStep create(String versionDiktat, Provisioner provisioner) {
47-
return create(versionDiktat, provisioner, Collections.emptyMap(), null);
49+
return create(versionDiktat, provisioner, null);
4850
}
4951

5052
public static FormatterStep create(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) {
51-
return create(versionDiktat, provisioner, Collections.emptyMap(), config);
52-
}
53-
54-
public static FormatterStep create(String versionDiktat, Provisioner provisioner, Map<String, String> userData, @Nullable FileSignature config) {
55-
return create(versionDiktat, provisioner, false, userData, config);
53+
return create(versionDiktat, provisioner, false, config);
5654
}
5755

5856
public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, @Nullable FileSignature config) {
59-
return createForScript(versionDiktat, provisioner, Collections.emptyMap(), config);
57+
return create(versionDiktat, provisioner, true, config);
6058
}
6159

62-
public static FormatterStep createForScript(String versionDiktat, Provisioner provisioner, Map<String, String> userData, @Nullable FileSignature config) {
63-
return create(versionDiktat, provisioner, true, userData, config);
64-
}
65-
66-
public static FormatterStep create(String versionDiktat, Provisioner provisioner, boolean isScript, Map<String, String> userData, @Nullable FileSignature config) {
60+
public static FormatterStep create(String versionDiktat, Provisioner provisioner, boolean isScript, @Nullable FileSignature config) {
61+
if (BadSemver.version(versionDiktat) < BadSemver.version(MIN_SUPPORTED_VERSION)) {
62+
throw new IllegalStateException("Minimum required Diktat version is " + MIN_SUPPORTED_VERSION + ", you tried " + versionDiktat + " which is too old");
63+
}
6764
Objects.requireNonNull(versionDiktat, "versionDiktat");
6865
Objects.requireNonNull(provisioner, "provisioner");
6966
return FormatterStep.createLazy(NAME,
70-
() -> new DiktatStep.State(versionDiktat, provisioner, isScript, userData, config),
67+
() -> new DiktatStep.State(versionDiktat, provisioner, isScript, config),
7168
DiktatStep.State::createFormat);
7269
}
7370

@@ -79,14 +76,12 @@ static final class State implements Serializable {
7976
private final boolean isScript;
8077
private final @Nullable FileSignature config;
8178
final JarState jar;
82-
private final TreeMap<String, String> userData;
8379

84-
State(String versionDiktat, Provisioner provisioner, boolean isScript, Map<String, String> userData, @Nullable FileSignature config) throws IOException {
80+
State(String versionDiktat, Provisioner provisioner, boolean isScript, @Nullable FileSignature config) throws IOException {
8581

8682
HashSet<String> pkgSet = new HashSet<>();
8783
pkgSet.add(MAVEN_COORDINATE + versionDiktat);
8884

89-
this.userData = new TreeMap<>(userData);
9085
this.jar = JarState.from(pkgSet, provisioner);
9186
this.isScript = isScript;
9287
this.config = config;
@@ -98,8 +93,8 @@ FormatterFunc createFormat() throws Exception {
9893
}
9994

10095
Class<?> formatterFunc = jar.getClassLoader().loadClass("com.diffplug.spotless.glue.diktat.DiktatFormatterFunc");
101-
Constructor<?> constructor = formatterFunc.getConstructor(boolean.class, Map.class);
102-
return (FormatterFunc.NeedsFile) constructor.newInstance(isScript, userData);
96+
Constructor<?> constructor = formatterFunc.getConstructor(boolean.class);
97+
return (FormatterFunc.NeedsFile) constructor.newInstance(isScript);
10398
}
10499
}
105100
}

Diff for: plugin-gradle/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
8+
### Changed
9+
* Minimum required `diktat` version bumped to `1.2.0` ([#1246](https://github.com/diffplug/spotless/pull/1246))
10+
* Default bumped from `1.1.0` -> `1.2.0`
811

912
## [6.7.2] - 2022-06-11
1013
### Fixed

Diff for: plugin-maven/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Support for `MAC_CLASSIC` (`\r`) line ending ([#1243](https://github.com/diffplug/spotless/pull/1243) fixes [#1196](https://github.com/diffplug/spotless/issues/1196))
8+
### Changed
9+
* Minimum required `diktat` version bumped to `1.2.0` ([#1246](https://github.com/diffplug/spotless/pull/1246))
10+
* Default bumped from `1.1.0` -> `1.2.0`
811

912
## [2.22.8] - 2022-06-11
1013
### Fixed

Diff for: plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Diktat.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2022 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,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.maven.kotlin;
1717

18-
import java.util.Collections;
19-
2018
import org.apache.maven.plugins.annotations.Parameter;
2119

2220
import com.diffplug.spotless.FileSignature;
@@ -41,6 +39,6 @@ public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
4139
config = ThrowingEx.get(() -> FileSignature.signAsList(stepConfig.getFileLocator().locateFile(configFile)));
4240
}
4341
String diktatVersion = version != null ? version : DiktatStep.defaultVersionDiktat();
44-
return DiktatStep.create(diktatVersion, stepConfig.getProvisioner(), Collections.emptyMap(), config);
42+
return DiktatStep.create(diktatVersion, stepConfig.getProvisioner(), config);
4543
}
4644
}

Diff for: plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/DiktatTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void testDiktatWithVersion() throws Exception {
4040

4141
writePomWithKotlinSteps(
4242
"<diktat>",
43-
" <version>1.0.1</version>",
43+
" <version>1.2.0</version>",
4444
"</diktat>");
4545

4646
String path = "src/main/kotlin/Main.kt";
@@ -56,7 +56,7 @@ void testDiktatConfig() throws Exception {
5656
File conf = setFile(configPath).toResource("kotlin/diktat/diktat-analysis.yml");
5757
writePomWithKotlinSteps(
5858
"<diktat>",
59-
" <version>1.0.1</version>",
59+
" <version>1.2.0</version>",
6060
" <configFile>" + conf.getAbsolutePath() + "</configFile>",
6161
"</diktat>");
6262

Diff for: testlib/src/main/resources/kotlin/diktat/main.clean

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package org.cqfn.diktat.example.gradle.multiproject
22

33
/**
4-
* @return print.
4+
* @return something
55
*/
66
class Main {
77
/**
88
* foo a [member] to this group.
99
*
1010
*/
1111
fun foo() {
12-
println(";")
13-
println()
12+
bar(";")
13+
bar()
1414
}
1515
}

Diff for: testlib/src/main/resources/kotlin/diktat/main.dirty

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package org.cqfn.diktat.example.gradle.multiproject
22
/**
3-
* @return print.
3+
* @return something
44
*/
55
class Main {
66
/**
77
* foo a [member] to this group.
88
*
99
*/
1010
fun foo() {
11-
println(";")
12-
println();
11+
bar(";")
12+
bar();
1313
}
1414
}

Diff for: testlib/src/test/java/com/diffplug/spotless/kotlin/DiktatStepTest.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import static com.diffplug.spotless.FileSignature.signAsList;
1919

2020
import java.io.File;
21-
import java.util.Collections;
2221

22+
import org.junit.jupiter.api.Assertions;
2323
import org.junit.jupiter.api.Test;
2424

2525
import com.diffplug.spotless.FileSignature;
@@ -36,31 +36,48 @@ void behavior() throws Exception {
3636
StepHarness.forStep(step)
3737
.testResourceException("kotlin/diktat/Unsolvable.kt", assertion -> {
3838
assertion.isInstanceOf(AssertionError.class);
39-
assertion.hasMessage("There are 2 unfixed errors:" +
39+
assertion.hasMessage("There are 4 unfixed errors:" +
4040
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
4141
System.lineSeparator() + "[FILE_NAME_INCORRECT] file name is incorrect - it should end with .kt extension and be in PascalCase: testlib" +
4242
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
43-
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable");
43+
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable" +
44+
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
45+
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()" +
46+
System.lineSeparator() + "Error on line: 13, column: 9 cannot be fixed automatically" +
47+
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()");
4448
});
4549
}
4650

4751
@Test
4852
void behaviorConf() throws Exception {
49-
5053
String configPath = "src/main/kotlin/diktat-analysis.yml";
5154
File conf = setFile(configPath).toResource("kotlin/diktat/diktat-analysis.yml");
5255
FileSignature config = signAsList(conf);
5356

54-
FormatterStep step = DiktatStep.create("1.0.1", TestProvisioner.mavenCentral(), Collections.emptyMap(), config);
57+
FormatterStep step = DiktatStep.create("1.2.0", TestProvisioner.mavenCentral(), config);
5558
StepHarness.forStep(step)
5659
.testResourceException("kotlin/diktat/Unsolvable.kt", assertion -> {
5760
assertion.isInstanceOf(AssertionError.class);
58-
assertion.hasMessage("There are 2 unfixed errors:" +
61+
assertion.hasMessage("There are 4 unfixed errors:" +
5962
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
6063
System.lineSeparator() + "[FILE_NAME_INCORRECT] file name is incorrect - it should end with .kt extension and be in PascalCase: testlib" +
6164
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
62-
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable");
65+
System.lineSeparator() + "[FILE_NAME_MATCH_CLASS] file name is incorrect - it should match with the class described in it if there is the only one class declared: testlib vs Unsolvable" +
66+
System.lineSeparator() + "Error on line: 1, column: 1 cannot be fixed automatically" +
67+
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()" +
68+
System.lineSeparator() + "Error on line: 13, column: 9 cannot be fixed automatically" +
69+
System.lineSeparator() + "[DEBUG_PRINT] use a dedicated logging library: found println()");
6370
});
6471
}
6572

73+
@Test
74+
void notSupportedVersion() {
75+
final IllegalStateException notSupportedException = Assertions.assertThrows(IllegalStateException.class,
76+
() -> DiktatStep.create("1.1.0", TestProvisioner.mavenCentral()));
77+
Assertions.assertTrue(
78+
notSupportedException.getMessage().contains("Minimum required Diktat version is 1.2.0, you tried 1.1.0 which is too old"));
79+
80+
Assertions.assertDoesNotThrow(() -> DiktatStep.create("1.2.1", TestProvisioner.mavenCentral()));
81+
Assertions.assertDoesNotThrow(() -> DiktatStep.create("2.0.0", TestProvisioner.mavenCentral()));
82+
}
6683
}

0 commit comments

Comments
 (0)