From 31f2bce0c3fa43611b371244153f761d4301fd0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Thu, 1 Sep 2022 21:10:54 +0300 Subject: [PATCH 1/4] Bump Kotlin to 1.7.10 --- build.gradle.kts | 2 +- test-project-android/build.gradle.kts | 2 +- test-project/build.gradle.kts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0bece732..2fe16ef2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { - kotlin("jvm") version "1.7.0" + kotlin("jvm") version "1.7.10" id("com.gradle.plugin-publish") version "0.21.0" `java-gradle-plugin` `maven-publish` diff --git a/test-project-android/build.gradle.kts b/test-project-android/build.gradle.kts index 5aab11fc..2c78bc07 100644 --- a/test-project-android/build.gradle.kts +++ b/test-project-android/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - kotlin("android") version "1.7.0" + kotlin("android") version "1.7.10" id("com.android.library") id("org.jmailen.kotlinter") } diff --git a/test-project/build.gradle.kts b/test-project/build.gradle.kts index 8d5cc178..afb5c456 100644 --- a/test-project/build.gradle.kts +++ b/test-project/build.gradle.kts @@ -1,4 +1,4 @@ plugins { - kotlin("jvm") version "1.7.0" + kotlin("jvm") version "1.7.10" id("org.jmailen.kotlinter") } From f24aadbd7396f806fa2fd4a75963b3d467dcdb5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Fri, 2 Sep 2022 12:56:51 +0300 Subject: [PATCH 2/4] Bump ktlint to 0.47.0 --- build.gradle.kts | 2 +- .../kotlinter/support/EditorConfigUtils.kt | 2 +- .../gradle/kotlinter/support/RuleSets.kt | 40 +++++++++---------- .../tasks/format/FormatWorkerAction.kt | 14 +++---- .../kotlinter/tasks/lint/LintWorkerAction.kt | 14 +++---- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2fe16ef2..088268c4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,7 @@ description = projectDescription object Versions { const val androidTools = "7.2.1" const val junit = "4.13.2" - const val ktlint = "0.46.1" + const val ktlint = "0.47.0" const val mockitoKotlin = "4.0.0" } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt index f27841a3..f78e559e 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/EditorConfigUtils.kt @@ -11,7 +11,7 @@ internal fun editorConfigOverride(ktLintParams: KtLintParams): EditorConfigOverr return if (rules.isEmpty()) { EditorConfigOverride.emptyEditorConfigOverride } else { - EditorConfigOverride.from(DefaultEditorConfigProperties.disabledRulesProperty to rules.joinToString(separator = ",")) + EditorConfigOverride.from(DefaultEditorConfigProperties.ktlintDisabledRulesProperty to rules.joinToString(separator = ",")) } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/RuleSets.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/RuleSets.kt index bd97d34c..8308860e 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/RuleSets.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/RuleSets.kt @@ -1,29 +1,29 @@ package org.jmailen.gradle.kotlinter.support -import com.pinterest.ktlint.core.RuleSet -import com.pinterest.ktlint.core.RuleSetProvider +import com.pinterest.ktlint.core.RuleProvider +import com.pinterest.ktlint.core.RuleSetProviderV2 import com.pinterest.ktlint.ruleset.experimental.ExperimentalRuleSetProvider import java.util.ServiceLoader -import kotlin.comparisons.compareBy -fun resolveRuleSets( - providers: Iterable, +internal fun resolveRuleProviders( + providers: Iterable, includeExperimentalRules: Boolean = false, -): List { - return providers - .filter { includeExperimentalRules || it !is ExperimentalRuleSetProvider } - .map { it.get() } - .sortedWith( - compareBy { - when (it.id) { - "standard" -> 0 - else -> 1 - } - }, - ) -} +): Set = providers + .asSequence() + .filter { includeExperimentalRules || it !is ExperimentalRuleSetProvider } + .sortedWith( + compareBy { + when (it.id) { + "standard" -> 0 + else -> 1 + } + }, + ) + .map(RuleSetProviderV2::getRuleProviders) + .flatten() + .toSet() // statically resolve providers from plugin classpath. ServiceLoader#load alone resolves classes lazily which fails when run in parallel // https://github.com/jeremymailen/kotlinter-gradle/issues/101 -val defaultRuleSetProviders: List = - ServiceLoader.load(RuleSetProvider::class.java).toList() +val defaultRuleSetProviders: List = + ServiceLoader.load(RuleSetProviderV2::class.java).toList() 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 a888c3a4..cbde073f 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 @@ -2,7 +2,7 @@ package org.jmailen.gradle.kotlinter.tasks.format import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.LintError -import com.pinterest.ktlint.core.RuleSet +import com.pinterest.ktlint.core.RuleProvider import org.gradle.api.logging.LogLevel import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging @@ -12,7 +12,7 @@ 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.resolveRuleProviders import org.jmailen.gradle.kotlinter.tasks.FormatTask import java.io.File @@ -28,7 +28,7 @@ abstract class FormatWorkerAction : WorkAction { val fixes = mutableListOf() try { files.forEach { file -> - val ruleSets = resolveRuleSets(defaultRuleSetProviders, ktLintParams.experimentalRules) + val ruleSets = resolveRuleProviders(defaultRuleSetProviders, ktLintParams.experimentalRules) val sourceText = file.readText() val relativePath = file.toRelativeString(projectDirectory) @@ -68,18 +68,18 @@ abstract class FormatWorkerAction : WorkAction { ) } - private fun formatKt(file: File, ruleSets: List, onError: ErrorHandler) = + private fun formatKt(file: File, ruleSets: Set, onError: ErrorHandler) = format(file, ruleSets, onError, false) - private fun formatKts(file: File, ruleSets: List, onError: ErrorHandler) = + private fun formatKts(file: File, ruleSets: Set, onError: ErrorHandler) = format(file, ruleSets, onError, true) - private fun format(file: File, ruleSets: List, onError: ErrorHandler, script: Boolean): String { + private fun format(file: File, ruleProviders: Set, onError: ErrorHandler, script: Boolean): String { return KtLint.format( KtLint.ExperimentalParams( fileName = file.path, text = file.readText(), - ruleSets = ruleSets, + ruleProviders = ruleProviders, script = script, editorConfigOverride = editorConfigOverride(ktLintParams), cb = { 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 d954d77e..9cd2ce0a 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 @@ -3,7 +3,7 @@ package org.jmailen.gradle.kotlinter.tasks.lint import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.LintError import com.pinterest.ktlint.core.Reporter -import com.pinterest.ktlint.core.RuleSet +import com.pinterest.ktlint.core.RuleProvider import org.gradle.api.logging.Logger import org.gradle.api.logging.Logging import org.gradle.internal.logging.slf4j.DefaultContextAwareTaskLogger @@ -15,7 +15,7 @@ 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.resolveRuleProviders import org.jmailen.gradle.kotlinter.tasks.LintTask import java.io.File @@ -35,7 +35,7 @@ abstract class LintWorkerAction : WorkAction { try { reporters.onEach { it.beforeAll() } files.forEach { file -> - val ruleSets = resolveRuleSets(defaultRuleSetProviders, ktLintParams.experimentalRules) + val ruleSets = resolveRuleProviders(defaultRuleSetProviders, ktLintParams.experimentalRules) val relativePath = file.toRelativeString(projectDirectory) reporters.onEach { it.before(relativePath) } logger.debug("$name linting: $relativePath") @@ -68,18 +68,18 @@ abstract class LintWorkerAction : WorkAction { } } - private fun lintKt(file: File, ruleSets: List, onError: (error: LintError) -> Unit) = + private fun lintKt(file: File, ruleSets: Set, onError: (error: LintError) -> Unit) = lint(file, ruleSets, onError, false) - private fun lintKts(file: File, ruleSets: List, onError: (error: LintError) -> Unit) = + private fun lintKts(file: File, ruleSets: Set, onError: (error: LintError) -> Unit) = lint(file, ruleSets, onError, true) - private fun lint(file: File, ruleSets: List, onError: ErrorHandler, script: Boolean) = + private fun lint(file: File, ruleProviders: Set, onError: ErrorHandler, script: Boolean) = KtLint.lint( KtLint.ExperimentalParams( fileName = file.path, text = file.readText(), - ruleSets = ruleSets, + ruleProviders = ruleProviders, script = script, editorConfigOverride = editorConfigOverride(ktLintParams), cb = { error, _ -> onError(error) }, From c8482ad41c1f9ced914990a0a6a7b1a739fbe55b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Fri, 2 Sep 2022 12:57:03 +0300 Subject: [PATCH 3/4] Update tests --- .../gradle/kotlinter/support/RuleSetsTest.kt | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) 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 cc3fcd08..d6f59bd4 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/support/RuleSetsTest.kt @@ -2,9 +2,8 @@ package org.jmailen.gradle.kotlinter.support import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.RuleSet -import com.pinterest.ktlint.core.RuleSetProvider -import org.jetbrains.kotlin.com.intellij.lang.ASTNode +import com.pinterest.ktlint.core.RuleProvider +import com.pinterest.ktlint.core.RuleSetProviderV2 import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test @@ -13,29 +12,24 @@ class RuleSetsTest { @Test fun `resolveRuleSets loads from classpath providers`() { - val result = resolveRuleSets(defaultRuleSetProviders) + val standardOnly = resolveRuleProviders(defaultRuleSetProviders, includeExperimentalRules = false) + val withExperimentalRules = resolveRuleProviders(defaultRuleSetProviders, includeExperimentalRules = true) - assertEquals(listOf("standard"), result.map { it.id }) - } - - @Test - fun `resolveRuleSets loads from classpath providers including experimental rules`() { - val result = resolveRuleSets(defaultRuleSetProviders, true) - - assertEquals(listOf("standard", "experimental"), result.map { it.id }) + assertTrue(standardOnly.isNotEmpty()) + assertTrue(standardOnly.size < withExperimentalRules.size) } @Test fun `resolveRuleSets puts standard rules first`() { - val standard = TestRuleSetProvider(RuleSet("standard", TestRule("one"))) - val extra1 = TestRuleSetProvider(RuleSet("extra-one", TestRule("two"))) - val extra2 = TestRuleSetProvider(RuleSet("extra-two", TestRule("three"))) + val standard = TestRuleSetProvider("standard", setOf(TestRule("one"))) + val extra1 = TestRuleSetProvider("extra-one", setOf(TestRule("two"))) + val extra2 = TestRuleSetProvider("extra-two", setOf(TestRule("three"))) - val result = resolveRuleSets(providers = listOf(extra2, standard, extra1)) + val result = resolveRuleProviders(providers = listOf(extra2, standard, extra1)).map { it.createNewRuleInstance() } assertEquals(3, result.size) - assertEquals(standard.ruleSet, result.first()) - assertTrue(result.containsAll(listOf(extra1.ruleSet, extra2.ruleSet))) + assertEquals(standard.ruleSet.single(), result.first()) + assertTrue(result.containsAll(listOf(extra1.ruleSet.single(), extra2.ruleSet.single()))) } @Test @@ -53,21 +47,24 @@ class RuleSetsTest { } """.trimIndent(), - ruleSets = resolveRuleSets(defaultRuleSetProviders), + ruleProviders = resolveRuleProviders(defaultRuleSetProviders), cb = { _, _ -> }, ), ) } } -class TestRuleSetProvider(val ruleSet: RuleSet) : RuleSetProvider { - override fun get() = ruleSet +class TestRuleSetProvider(id: String, val ruleSet: Set) : RuleSetProviderV2( + id = id, + about = About( + maintainer = "stub-maintainer", + description = "stub-description", + license = "stub-license", + repositoryUrl = "stub-repositoryUrl", + issueTrackerUrl = "stub-issueTrackerUrl", + ), +) { + override fun getRuleProviders() = ruleSet.map { rule -> RuleProvider(provider = { rule }) }.toSet() } -class TestRule(id: String) : Rule(id) { - override fun visit( - node: ASTNode, - autoCorrect: Boolean, - emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, - ) = Unit -} +class TestRule(id: String) : Rule(id) From e22ceaf75e51be86faf75f5c98d2ebbe56a2587e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Thu, 8 Sep 2022 09:00:59 +0200 Subject: [PATCH 4/4] Bump ktlint to 0.47.1 --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 088268c4..8010542f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,7 @@ description = projectDescription object Versions { const val androidTools = "7.2.1" const val junit = "4.13.2" - const val ktlint = "0.47.0" + const val ktlint = "0.47.1" const val mockitoKotlin = "4.0.0" }