Repository: jeremymailen/kotlinter-gradle
Pull request #258 (2022-06-22 20:02:08)

Bump `ktlint` to 0.46.1
\+ Replace removed `Ktlint.Params` with the new `Ktlint.ExperimentalParams`

The same as https://github.com/jeremymailen/kotlinter-gradle/pull/257, but also adjusts the plugin to the recent API changes 👀 


Patch:
diff --git a/build.gradle.kts b/build.gradle.kts
index 95d1087..3bb7571 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -6,7 +6,7 @@ plugins {
     id("com.gradle.plugin-publish") version "0.18.0"
     `java-gradle-plugin`
     `maven-publish`
-    id("org.jmailen.kotlinter") version "3.9.0"
+    id("org.jmailen.kotlinter") version "3.10.0"
     idea
 }
 
@@ -27,7 +27,7 @@ description = projectDescription
 object Versions {
     const val androidTools = "7.0.4"
     const val junit = "4.13.2"
-    const val ktlint = "0.45.2"
+    const val ktlint = "0.46.1"
     const val mockitoKotlin = "4.0.0"
 }
 
diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintEditoConfigOverrides.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintEditoConfigOverrides.kt
new file mode 100644
index 0000000..a06dc3c
--- /dev/null
+++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintEditoConfigOverrides.kt
@@ -0,0 +1,15 @@
+package org.jmailen.gradle.kotlinter.support
+
+import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.disabledRulesProperty
+import com.pinterest.ktlint.core.api.EditorConfigOverride
+import com.pinterest.ktlint.core.api.EditorConfigOverride.Companion.emptyEditorConfigOverride
+
+internal fun editorConfigOverride(ktLintParams: KtLintParams): EditorConfigOverride {
+    val rules = ktLintParams.disabledRules
+
+    return if (rules.isEmpty()) {
+        emptyEditorConfigOverride
+    } else {
+        EditorConfigOverride.from(disabledRulesProperty to rules.joinToString(separator = ","))
+    }
+}
diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintUserData.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintUserData.kt
deleted file mode 100644
index f5de944..0000000
--- a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/KtLintUserData.kt
+++ /dev/null
@@ -1,10 +0,0 @@
-package org.jmailen.gradle.kotlinter.support
-
-fun userData(ktLintParams: KtLintParams): Map<String, String> {
-    val userData = mutableMapOf<String, String>()
-
-    ktLintParams.disabledRules.takeIf { it.isNotEmpty() }?.let { rules ->
-        userData["disabled_rules"] = rules.joinToString(",")
-    }
-    return userData
-}
diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt
index 59cd4f7..f41174a 100644
--- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt
+++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt
@@ -11,8 +11,8 @@ import org.gradle.workers.WorkAction
 import org.jmailen.gradle.kotlinter.support.KotlinterError
 import org.jmailen.gradle.kotlinter.support.KtLintParams
 import org.jmailen.gradle.kotlinter.support.defaultRuleSetProviders
+import org.jmailen.gradle.kotlinter.support.editorConfigOverride
 import org.jmailen.gradle.kotlinter.support.resolveRuleSets
-import org.jmailen.gradle.kotlinter.support.userData
 import org.jmailen.gradle.kotlinter.tasks.FormatTask
 import java.io.File
 
@@ -76,12 +76,12 @@ abstract class FormatWorkerAction : WorkAction<FormatWorkerParameters> {
 
     private fun format(file: File, ruleSets: List<RuleSet>, onError: ErrorHandler, script: Boolean): String {
         return KtLint.format(
-            KtLint.Params(
+            KtLint.ExperimentalParams(
                 fileName = file.path,
                 text = file.readText(),
                 ruleSets = ruleSets,
                 script = script,
-                userData = userData(ktLintParams),
+                editorConfigOverride = editorConfigOverride(ktLintParams),
                 cb = { error, corrected ->
                     onError(error, corrected)
                 }
diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt
index 0e5c845..1b53527 100644
--- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt
+++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt
@@ -12,10 +12,10 @@ import org.jmailen.gradle.kotlinter.support.KotlinterError
 import org.jmailen.gradle.kotlinter.support.KtLintParams
 import org.jmailen.gradle.kotlinter.support.LintFailure
 import org.jmailen.gradle.kotlinter.support.defaultRuleSetProviders
+import org.jmailen.gradle.kotlinter.support.editorConfigOverride
 import org.jmailen.gradle.kotlinter.support.reporterFor
 import org.jmailen.gradle.kotlinter.support.reporterPathFor
 import org.jmailen.gradle.kotlinter.support.resolveRuleSets
-import org.jmailen.gradle.kotlinter.support.userData
 import org.jmailen.gradle.kotlinter.tasks.LintTask
 import java.io.File
 
@@ -76,12 +76,12 @@ abstract class LintWorkerAction : WorkAction<LintWorkerParameters> {
 
     private fun lint(file: File, ruleSets: List<RuleSet>, onError: ErrorHandler, script: Boolean) =
         KtLint.lint(
-            KtLint.Params(
+            KtLint.ExperimentalParams(
                 fileName = file.path,
                 text = file.readText(),
                 ruleSets = ruleSets,
                 script = script,
-                userData = userData(ktLintParams),
+                editorConfigOverride = editorConfigOverride(ktLintParams),
                 cb = { error, _ -> onError(error) }
             )
         )


Test patch:
diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt
index 66416b0..5da20d7 100644
--- a/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt
+++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt
@@ -41,9 +41,9 @@ class RuleSetsTest {
     @Test
     fun `test compatibility`() {
         KtLint.lint(
-            KtLint.Params(
-                "/tmp/src/test/KotlinClass.kt",
-                """
+            KtLint.ExperimentalParams(
+                fileName = "/tmp/src/test/KotlinClass.kt",
+                text = """
                     package test
 
                     class KotlinClass {
@@ -53,7 +53,7 @@ class RuleSetsTest {
                     }
 
                 """.trimIndent(),
-                resolveRuleSets(defaultRuleSetProviders),
+                ruleSets = resolveRuleSets(defaultRuleSetProviders),
                 cb = { _, _ -> }
             )
         )
@@ -69,5 +69,5 @@ class TestRule(id: String) : Rule(id) {
         node: ASTNode,
         autoCorrect: Boolean,
         emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
-    ) {}
+    ) = Unit
 }