Skip to content

Commit 308c7cc

Browse files
authoredJan 8, 2023
Fix ktlint 0.48 and bump default to 0.48.1 (#1456)
2 parents c50503e + 2eebed3 commit 308c7cc

File tree

11 files changed

+144
-14
lines changed

11 files changed

+144
-14
lines changed
 

‎CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
1315
### Changes
1416
* Bump the dev version of Gradle from `7.5.1` to `7.6` ([#1409](https://github.com/diffplug/spotless/pull/1409))
1517
* We also removed the no-longer-required dependency `org.codehaus.groovy:groovy-xml`
@@ -18,6 +20,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1820
* `StepHarness` now operates on `Formatter` rather than a `FormatterStep`
1921
* `StepHarnessWithFile` now takes a `ResourceHarness` in its constructor to handle the file manipulation parts
2022
* Standardized that we test exception *messages*, not types, which will ease the transition to linting later on
23+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
2124

2225
## [2.31.1] - 2023-01-02
2326
### Fixed

‎lib/build.gradle

+9-7
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ dependencies {
4545
compileOnly 'org.slf4j:slf4j-api:2.0.0'
4646
// zero runtime reqs is a hard requirements for spotless-lib
4747
// if you need a dep, put it in lib-extra
48-
testImplementation "org.junit.jupiter:junit-jupiter:$VER_JUNIT"
49-
testImplementation "org.assertj:assertj-core:$VER_ASSERTJ"
50-
testImplementation "com.diffplug.durian:durian-testlib:$VER_DURIAN"
48+
testCommonImplementation "org.junit.jupiter:junit-jupiter:$VER_JUNIT"
49+
testCommonImplementation "org.assertj:assertj-core:$VER_ASSERTJ"
50+
testCommonImplementation "com.diffplug.durian:durian-testlib:$VER_DURIAN"
5151

5252
// used for pom sorting
5353
sortPomCompileOnly 'com.github.ekryd.sortpom:sortpom-sorter:3.0.0'
@@ -91,9 +91,9 @@ dependencies {
9191
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.47.0'
9292
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.47.0'
9393
compatKtLint0Dot47Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.47.0'
94-
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-core:0.48.0'
95-
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0'
96-
compatKtLint0Dot48Dot0CompileOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0'
94+
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-core:0.48.0'
95+
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0'
96+
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0'
9797

9898
String VER_SCALAFMT="3.6.1"
9999
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"
@@ -108,7 +108,9 @@ dependencies {
108108
// we'll hold the core lib to a high standard
109109
spotbugs { reportLevel = 'low' } // low|medium|high (low = sensitive to even minor mistakes)
110110

111-
test { useJUnitPlatform() }
111+
tasks.withType(Test).configureEach {
112+
useJUnitPlatform()
113+
}
112114

113115
jar {
114116
for (glue in NEEDS_GLUE) {

‎lib/src/compatKtLint0Dot48Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot48Dot0Adapter.java

+41-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktlint.compat;
1717

18+
import java.util.ArrayList;
19+
import java.util.Collections;
1820
import java.util.LinkedHashSet;
1921
import java.util.List;
2022
import java.util.Map;
@@ -27,11 +29,17 @@
2729
import com.pinterest.ktlint.core.LintError;
2830
import com.pinterest.ktlint.core.Rule;
2931
import com.pinterest.ktlint.core.RuleProvider;
30-
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties;
3132
import com.pinterest.ktlint.core.api.EditorConfigDefaults;
3233
import com.pinterest.ktlint.core.api.EditorConfigOverride;
3334
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties;
35+
import com.pinterest.ktlint.core.api.editorconfig.CodeStyleEditorConfigPropertyKt;
36+
import com.pinterest.ktlint.core.api.editorconfig.DisabledRulesEditorConfigPropertyKt;
3437
import com.pinterest.ktlint.core.api.editorconfig.EditorConfigProperty;
38+
import com.pinterest.ktlint.core.api.editorconfig.IndentSizeEditorConfigPropertyKt;
39+
import com.pinterest.ktlint.core.api.editorconfig.IndentStyleEditorConfigPropertyKt;
40+
import com.pinterest.ktlint.core.api.editorconfig.InsertFinalNewLineEditorConfigPropertyKt;
41+
import com.pinterest.ktlint.core.api.editorconfig.MaxLineLengthEditorConfigPropertyKt;
42+
import com.pinterest.ktlint.core.api.editorconfig.RuleExecutionEditorConfigPropertyKt;
3543
import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider;
3644
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider;
3745

@@ -41,13 +49,29 @@
4149

4250
public class KtLintCompat0Dot48Dot0Adapter implements KtLintCompatAdapter {
4351

52+
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES;
53+
54+
static {
55+
List<EditorConfigProperty<?>> list = new ArrayList<>();
56+
list.add(CodeStyleEditorConfigPropertyKt.getCODE_STYLE_PROPERTY());
57+
//noinspection deprecation
58+
list.add(DisabledRulesEditorConfigPropertyKt.getDISABLED_RULES_PROPERTY());
59+
//noinspection KotlinInternalInJava,deprecation
60+
list.add(DisabledRulesEditorConfigPropertyKt.getKTLINT_DISABLED_RULES_PROPERTY());
61+
list.add(IndentStyleEditorConfigPropertyKt.getINDENT_STYLE_PROPERTY());
62+
list.add(IndentSizeEditorConfigPropertyKt.getINDENT_SIZE_PROPERTY());
63+
list.add(InsertFinalNewLineEditorConfigPropertyKt.getINSERT_FINAL_NEWLINE_PROPERTY());
64+
list.add(MaxLineLengthEditorConfigPropertyKt.getMAX_LINE_LENGTH_PROPERTY());
65+
DEFAULT_EDITOR_CONFIG_PROPERTIES = Collections.unmodifiableList(list);
66+
}
67+
4468
static class FormatterCallback implements Function2<LintError, Boolean, Unit> {
4569
@Override
4670
public Unit invoke(LintError lint, Boolean corrected) {
4771
if (!corrected) {
4872
KtLintCompatReporting.report(lint.getLine(), lint.getCol(), lint.getRuleId(), lint.getDetail());
4973
}
50-
return null;
74+
return Unit.INSTANCE;
5175
}
5276
}
5377

@@ -66,7 +90,7 @@ public String format(final String text, final String name, final boolean isScrip
6690

6791
EditorConfigOverride editorConfigOverride;
6892
if (editorConfigOverrideMap.isEmpty()) {
69-
editorConfigOverride = EditorConfigOverride.Companion.getEmptyEditorConfigOverride();
93+
editorConfigOverride = EditorConfigOverride.Companion.getEMPTY_EDITOR_CONFIG_OVERRIDE();
7094
} else {
7195
editorConfigOverride = createEditorConfigOverride(allRuleProviders.stream().map(
7296
RuleProvider::createNewRuleInstance).collect(
@@ -82,7 +106,7 @@ public String format(final String text, final String name, final boolean isScrip
82106
formatterCallback,
83107
isScript,
84108
false,
85-
EditorConfigDefaults.Companion.getEmptyEditorConfigDefaults(),
109+
EditorConfigDefaults.Companion.getEMPTY_EDITOR_CONFIG_DEFAULTS(),
86110
editorConfigOverride,
87111
false));
88112
}
@@ -98,7 +122,7 @@ private static EditorConfigOverride createEditorConfigOverride(final List<Rule>
98122

99123
// Create a mapping of properties to their names based on rule properties and default properties
100124
Map<String, EditorConfigProperty<?>> supportedProperties = Stream
101-
.concat(ruleProperties, DefaultEditorConfigProperties.INSTANCE.getEditorConfigProperties().stream())
125+
.concat(ruleProperties, DEFAULT_EDITOR_CONFIG_PROPERTIES.stream())
102126
.distinct()
103127
.collect(Collectors.toMap(EditorConfigProperty::getName, property -> property));
104128

@@ -109,6 +133,18 @@ private static EditorConfigOverride createEditorConfigOverride(final List<Rule>
109133
EditorConfigProperty<?> property = supportedProperties.get(entry.getKey());
110134
if (property != null) {
111135
return new Pair<>(property, entry.getValue());
136+
} else if (entry.getKey().startsWith("ktlint_")) {
137+
String[] parts = entry.getKey().substring(7).split("_", 2);
138+
if (parts.length == 1) {
139+
// convert ktlint_{ruleset} to {ruleset}
140+
String qualifiedRuleId = parts[0];
141+
property = RuleExecutionEditorConfigPropertyKt.createRuleSetExecutionEditorConfigProperty(qualifiedRuleId);
142+
} else {
143+
// convert ktlint_{ruleset}_{rulename} to {ruleset}:{rulename}
144+
String qualifiedRuleId = parts[0] + ":" + parts[1];
145+
property = RuleExecutionEditorConfigPropertyKt.createRuleExecutionEditorConfigProperty(qualifiedRuleId);
146+
}
147+
return new Pair<>(property, entry.getValue());
112148
} else {
113149
return null;
114150
}

‎lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class KtLintStep {
3333
// prevent direct instantiation
3434
private KtLintStep() {}
3535

36-
private static final String DEFAULT_VERSION = "0.48.0";
36+
private static final String DEFAULT_VERSION = "0.48.1";
3737
static final String NAME = "ktlint";
3838
static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
3939
static final String PACKAGE = "com.pinterest";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2023 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 static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.io.TempDir;
30+
31+
public class KtLintCompat0Dot48Dot0AdapterTest {
32+
@Test
33+
public void testDefaults(@TempDir Path path) throws IOException {
34+
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
35+
String text = loadAndWriteText(path, "empty_class_body.kt");
36+
37+
Map<String, String> userData = new HashMap<>();
38+
39+
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
40+
41+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, "empty_class_body.kt", false, false, userData, editorConfigOverrideMap);
42+
assertEquals("class empty_class_body\n", formatted);
43+
}
44+
45+
@Test
46+
public void testEditorConfigCanDisable(@TempDir Path path) throws IOException {
47+
KtLintCompat0Dot48Dot0Adapter ktLintCompat0Dot48Dot0Adapter = new KtLintCompat0Dot48Dot0Adapter();
48+
String text = loadAndWriteText(path, "fails_no_semicolons.kt");
49+
50+
Map<String, String> userData = new HashMap<>();
51+
52+
Map<String, Object> editorConfigOverrideMap = new HashMap<>();
53+
editorConfigOverrideMap.put("indent_style", "tab");
54+
editorConfigOverrideMap.put("ktlint_standard_no-semi", "disabled");
55+
56+
String formatted = ktLintCompat0Dot48Dot0Adapter.format(text, "fails_no_semicolons.kt", false, false, userData, editorConfigOverrideMap);
57+
assertEquals("class fails_no_semicolons {\n\tval i = 0;\n}\n", formatted);
58+
}
59+
60+
private static String loadAndWriteText(Path path, String name) throws IOException {
61+
try (InputStream is = KtLintCompat0Dot48Dot0AdapterTest.class.getResourceAsStream("/" + name)) {
62+
Files.copy(is, path.resolve(name));
63+
}
64+
return new String(Files.readAllBytes(path.resolve(name)), StandardCharsets.UTF_8);
65+
}
66+
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class empty_class_body {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class fails_no_semicolons {
2+
val i = 0;
3+
}

‎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
### Fixed
77
* Prevent tool configurations from being resolved outside project ([#1447](https://github.com/diffplug/spotless/pull/1447) fixes [#1215](https://github.com/diffplug/spotless/issues/1215))
8+
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
9+
### Changes
10+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
811

912
## [6.12.1] - 2023-01-02
1013
### Fixed

‎plugin-maven/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66
### Fixed
7+
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
8+
### Changes
9+
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
710
* Reduce spurious invalidations of the up-to-date index file ([#1461](https://github.com/diffplug/spotless/pull/1461))
811

912
## [2.29.0] - 2023-01-02

‎settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pluginManagement {
1616
// https://github.com/radarsh/gradle-test-logger-plugin/blob/develop/CHANGELOG.md
1717
id 'com.adarshr.test-logger' version '3.2.0'
1818
// https://github.com/davidburstrom/version-compatibility-gradle-plugin/tags
19-
id 'io.github.davidburstrom.version-compatibility' version '0.3.0'
19+
id 'io.github.davidburstrom.version-compatibility' version '0.4.0'
2020
}
2121
}
2222
plugins {

‎testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ void works0_48_0() {
139139
"Wildcard import");
140140
}
141141

142+
@Test
143+
void works0_48_1() {
144+
FormatterStep step = KtLintStep.create("0.48.1", TestProvisioner.mavenCentral());
145+
StepHarnessWithFile.forStep(this, step)
146+
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
147+
.testResourceExceptionMsg("kotlin/ktlint/unsolvable.dirty").isEqualTo("Error on line: 1, column: 1\n" +
148+
"rule: no-wildcard-imports\n" +
149+
"Wildcard import");
150+
}
151+
142152
@Test
143153
void equality() {
144154
new SerializableEqualityTester() {

0 commit comments

Comments
 (0)