Skip to content

Commit aadeaef

Browse files
authored
Bump KtLint default to 0.47.1 and restore support back to 0.31.0 (#1303)
2 parents dcdba4d + b4d7019 commit aadeaef

File tree

16 files changed

+800
-105
lines changed

16 files changed

+800
-105
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* `formatAnnotations()` step to correct formatting of Java type annotations. It puts type annotations on the same line as the type that they qualify. Run it after a Java formatting step, such as `googleJavaFormat()`. ([#1275](https://github.com/diffplug/spotless/pull/1275))
1515
### Changes
1616
* Bump default `ktfmt` version to latest `0.39` -> `0.40` ([#1312](https://github.com/diffplug/spotless/pull/1312))
17+
* Bump default `ktlint` version to latest `0.46.1` -> `0.47.1` ([#1303](https://github.com/diffplug/spotless/pull/1303))
18+
* Also restored support for older versions of ktlint back to `0.31.0`
1719

1820
## [2.29.0] - 2022-08-23
1921
### Added

lib/build.gradle

+39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'java-library'
3+
id 'io.github.davidburstrom.version-compatibility'
34
}
45
ext.artifactId = project.artifactIdLib
56
version = rootProject.spotlessChangelog.versionNext
@@ -23,6 +24,22 @@ for (glue in NEEDS_GLUE) {
2324
}
2425
}
2526

27+
versionCompatibility {
28+
adapters {
29+
namespaces.register('KtLint') {
30+
versions = [
31+
'0.31.0',
32+
'0.32.0',
33+
'0.34.2',
34+
'0.45.2',
35+
'0.46.0',
36+
'0.47.0',
37+
]
38+
targetSourceSetName = 'ktlint'
39+
}
40+
}
41+
}
42+
2643
dependencies {
2744
compileOnly 'org.slf4j:slf4j-api:2.0.0'
2845
// zero runtime reqs is a hard requirements for spotless-lib
@@ -52,6 +69,28 @@ dependencies {
5269
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-experimental:$VER_KTLINT"
5370
ktlintCompileOnly "com.pinterest.ktlint:ktlint-ruleset-standard:$VER_KTLINT"
5471

72+
compatKtLint0Dot31Dot0CompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.20'
73+
compatKtLint0Dot31Dot0CompileOnly 'com.github.shyiko.ktlint:ktlint-core:0.31.0'
74+
compatKtLint0Dot31Dot0CompileOnly 'com.github.shyiko.ktlint:ktlint-ruleset-experimental:0.31.0'
75+
compatKtLint0Dot31Dot0CompileOnly 'com.github.shyiko.ktlint:ktlint-ruleset-standard:0.31.0'
76+
compatKtLint0Dot32Dot0CompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.20'
77+
compatKtLint0Dot32Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.32.0'
78+
compatKtLint0Dot32Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.32.0'
79+
compatKtLint0Dot32Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.32.0'
80+
compatKtLint0Dot34Dot2CompileOnly 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.20'
81+
compatKtLint0Dot34Dot2CompileOnly 'com.pinterest.ktlint:ktlint-core:0.34.2'
82+
compatKtLint0Dot34Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.34.2'
83+
compatKtLint0Dot34Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.34.2'
84+
compatKtLint0Dot45Dot2CompileOnly 'com.pinterest.ktlint:ktlint-core:0.45.2'
85+
compatKtLint0Dot45Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.45.2'
86+
compatKtLint0Dot45Dot2CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.45.2'
87+
compatKtLint0Dot46Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.46.0'
88+
compatKtLint0Dot46Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.46.0'
89+
compatKtLint0Dot46Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.46.0'
90+
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.47.0'
91+
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.47.0'
92+
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.47.0'
93+
5594
String VER_SCALAFMT="3.5.9"
5695
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"
5796

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktlint.compat;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import com.github.shyiko.ktlint.core.KtLint;
23+
import com.github.shyiko.ktlint.core.LintError;
24+
import com.github.shyiko.ktlint.core.RuleSet;
25+
import com.github.shyiko.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
26+
import com.github.shyiko.ktlint.ruleset.standard.StandardRuleSetProvider;
27+
28+
import kotlin.Unit;
29+
import kotlin.jvm.functions.Function2;
30+
31+
public class KtLintCompat0Dot31Dot0Adapter implements KtLintCompatAdapter {
32+
33+
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
34+
@Override
35+
public Unit invoke(LintError lint, Boolean corrected) {
36+
if (!corrected) {
37+
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
38+
}
39+
return null;
40+
}
41+
}
42+
43+
@Override
44+
public String format(final String text, final String name, final boolean isScript,
45+
final boolean useExperimental,
46+
final Map<String, String> userData,
47+
final Map<String, Object> editorConfigOverrideMap) {
48+
final FormatterCallback formatterCallback = new FormatterCallback();
49+
50+
final List<RuleSet> rulesets = new ArrayList<>();
51+
rulesets.add(new StandardRuleSetProvider().get());
52+
53+
if (useExperimental) {
54+
rulesets.add(new ExperimentalRuleSetProvider().get());
55+
}
56+
57+
return KtLint.INSTANCE.format(
58+
text,
59+
rulesets,
60+
userData,
61+
formatterCallback);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktlint.compat;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import com.pinterest.ktlint.core.KtLint;
23+
import com.pinterest.ktlint.core.LintError;
24+
import com.pinterest.ktlint.core.RuleSet;
25+
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
26+
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;
27+
28+
import kotlin.Unit;
29+
import kotlin.jvm.functions.Function2;
30+
31+
public class KtLintCompat0Dot32Dot0Adapter implements KtLintCompatAdapter {
32+
33+
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
34+
@Override
35+
public Unit invoke(LintError lint, Boolean corrected) {
36+
if (!corrected) {
37+
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
38+
}
39+
return null;
40+
}
41+
}
42+
43+
@Override
44+
public String format(final String text, final String name, final boolean isScript,
45+
final boolean useExperimental,
46+
final Map<String, String> userData,
47+
final Map<String, Object> editorConfigOverrideMap) {
48+
final FormatterCallback formatterCallback = new FormatterCallback();
49+
50+
final List<RuleSet> rulesets = new ArrayList<>();
51+
rulesets.add(new StandardRuleSetProvider().get());
52+
53+
if (useExperimental) {
54+
rulesets.add(new ExperimentalRuleSetProvider().get());
55+
}
56+
57+
return KtLint.INSTANCE.format(
58+
text,
59+
rulesets,
60+
userData,
61+
formatterCallback);
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktlint.compat;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import com.pinterest.ktlint.core.KtLint;
23+
import com.pinterest.ktlint.core.LintError;
24+
import com.pinterest.ktlint.core.RuleSet;
25+
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
26+
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;
27+
28+
import kotlin.Unit;
29+
import kotlin.jvm.functions.Function2;
30+
31+
public class KtLintCompat0Dot34Dot2Adapter implements KtLintCompatAdapter {
32+
33+
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
34+
@Override
35+
public Unit invoke(LintError lint, Boolean corrected) {
36+
if (!corrected) {
37+
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
38+
}
39+
return null;
40+
}
41+
}
42+
43+
@Override
44+
public String format(final String text, final String name, final boolean isScript,
45+
final boolean useExperimental,
46+
final Map<String, String> userData,
47+
final Map<String, Object> editorConfigOverrideMap) {
48+
final FormatterCallback formatterCallback = new FormatterCallback();
49+
50+
final List<RuleSet> rulesets = new ArrayList<>();
51+
rulesets.add(new StandardRuleSetProvider().get());
52+
53+
if (useExperimental) {
54+
rulesets.add(new ExperimentalRuleSetProvider().get());
55+
}
56+
57+
return KtLint.INSTANCE.format(new KtLint.Params(
58+
name,
59+
text,
60+
rulesets,
61+
userData,
62+
formatterCallback,
63+
isScript,
64+
null,
65+
false));
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2022 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.glue.ktlint.compat;
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
25+
26+
import com.pinterest.ktlint.core.KtLint;
27+
import com.pinterest.ktlint.core.LintError;
28+
import com.pinterest.ktlint.core.RuleSet;
29+
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties;
30+
import com.pinterest.ktlint.core.api.EditorConfigOverride;
31+
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties;
32+
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
33+
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;
34+
35+
import kotlin.Pair;
36+
import kotlin.Unit;
37+
import kotlin.jvm.functions.Function2;
38+
39+
public class KtLintCompat0Dot45Dot2Adapter implements KtLintCompatAdapter {
40+
41+
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
42+
@Override
43+
public Unit invoke(LintError lint, Boolean corrected) {
44+
if (!corrected) {
45+
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
46+
}
47+
return null;
48+
}
49+
}
50+
51+
@Override
52+
public String format(final String text, final String name, final boolean isScript,
53+
final boolean useExperimental,
54+
final Map<String, String> userData,
55+
final Map<String, Object> editorConfigOverrideMap) {
56+
final FormatterCallback formatterCallback = new FormatterCallback();
57+
58+
final List<RuleSet> rulesets = new ArrayList<>();
59+
rulesets.add(new StandardRuleSetProvider().get());
60+
61+
if (useExperimental) {
62+
rulesets.add(new ExperimentalRuleSetProvider().get());
63+
}
64+
65+
EditorConfigOverride editorConfigOverride;
66+
if (editorConfigOverrideMap.isEmpty()) {
67+
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
68+
} else {
69+
editorConfigOverride = createEditorConfigOverride(rulesets, editorConfigOverrideMap);
70+
}
71+
72+
return KtLint.INSTANCE.format(new KtLint.ExperimentalParams(
73+
name,
74+
text,
75+
rulesets,
76+
userData,
77+
formatterCallback,
78+
isScript,
79+
null,
80+
false,
81+
editorConfigOverride,
82+
false));
83+
}
84+
85+
/**
86+
* Create EditorConfigOverride from user provided parameters.
87+
* Calling this method requires KtLint 0.45.2.
88+
*/
89+
private static EditorConfigOverride createEditorConfigOverride(final List<RuleSet> rulesets, Map<String, Object> editorConfigOverrideMap) {
90+
// Get properties from rules in the rule sets
91+
Stream<UsesEditorConfigProperties.EditorConfigProperty<?>> ruleProperties = rulesets.stream()
92+
.flatMap(ruleSet -> Arrays.stream(ruleSet.getRules()))
93+
.filter(rule -> rule instanceof UsesEditorConfigProperties)
94+
.flatMap(rule -> ((UsesEditorConfigProperties) rule).getEditorConfigProperties().stream());
95+
96+
// Create a mapping of properties to their names based on rule properties and default properties
97+
Map<String, UsesEditorConfigProperties.EditorConfigProperty<?>> supportedProperties = Stream
98+
.concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getDefaultEditorConfigProperties().stream())
99+
.distinct()
100+
.collect(Collectors.toMap(property -> property.getType().getName(), property -> property));
101+
102+
// Create config properties based on provided property names and values
103+
@SuppressWarnings("unchecked")
104+
Pair<UsesEditorConfigProperties.EditorConfigProperty<?>, ?>[] properties = editorConfigOverrideMap.entrySet().stream()
105+
.map(entry -> {
106+
UsesEditorConfigProperties.EditorConfigProperty<?> property = supportedProperties.get(entry.getKey());
107+
if (property != null) {
108+
return new Pair<>(property, entry.getValue());
109+
} else {
110+
return null;
111+
}
112+
})
113+
.filter(Objects::nonNull)
114+
.toArray(Pair[]::new);
115+
116+
return EditorConfigOverride.Companion.from(properties);
117+
}
118+
}

0 commit comments

Comments
 (0)