From 972d917c6c59ded59248e0f57a6f1d25c248a3b3 Mon Sep 17 00:00:00 2001 From: Jeremy Mailen Date: Wed, 11 Dec 2024 22:52:56 -0800 Subject: [PATCH 1/3] Test adding custom rule sets Rule sets can be configured by adding them to the `ktlint` dependency configuration --- .github/workflows/default.yml | 5 ++-- test-project/build.gradle.kts | 3 +-- test-project/rules/build.gradle.kts | 7 ++++++ .../org/jmailen/ktlint/rule/NoVarRule.kt | 23 +++++++++++++++++++ .../jmailen/ktlint/rule/RuleSetProvider.kt | 16 +++++++++++++ ...int.cli.ruleset.core.api.RuleSetProviderV3 | 1 + test-project/service/build.gradle.kts | 8 +++++++ .../gradle/kotlinter/sample/CustomNoVar.kt | 5 ++++ .../kotlinter/sample/EmptyClassBody.kt} | 2 +- .../gradle/kotlinter/sample/OpSpacing.kt | 0 test-project/settings.gradle.kts | 3 +++ 11 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 test-project/rules/build.gradle.kts create mode 100644 test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt create mode 100644 test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt create mode 100644 test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 create mode 100644 test-project/service/build.gradle.kts create mode 100644 test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt rename test-project/{src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt => service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt} (62%) rename test-project/{ => service}/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt (100%) diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 8c18957d..81fc6065 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -72,8 +72,9 @@ jobs: - name: Assert fixes run: | - cd test-project/build/reports/ktlint - grep 'no-empty-class-body' main-format.txt | grep -q 'EmptyClassBodyClass.kt:3:27' + cd test-project/service/build/reports/ktlint + grep 'no-empty-class-body' main-format.txt | grep -q 'EmptyClassBody.kt:3:27' + grep 'kotlinter-test-rules:no-var' main-format.txt | grep -q 'NoVar.kt:4:5' grep 'op-spacing' test-format.txt | grep -q 'OpSpacing.kt:5:16' - name: Upload reports diff --git a/test-project/build.gradle.kts b/test-project/build.gradle.kts index d5fbdfd6..2682285e 100644 --- a/test-project/build.gradle.kts +++ b/test-project/build.gradle.kts @@ -1,4 +1,3 @@ plugins { - kotlin("jvm") version "2.1.0" - id("org.jmailen.kotlinter") + kotlin("jvm") version "2.1.0" apply false } diff --git a/test-project/rules/build.gradle.kts b/test-project/rules/build.gradle.kts new file mode 100644 index 00000000..4f6f62dd --- /dev/null +++ b/test-project/rules/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + kotlin("jvm") +} + +dependencies { + compileOnly("com.pinterest.ktlint:ktlint-cli-ruleset-core:1.5.0") +} diff --git a/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt new file mode 100644 index 00000000..937951e9 --- /dev/null +++ b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/NoVarRule.kt @@ -0,0 +1,23 @@ +package org.jmailen.ktlint.rule + +import com.pinterest.ktlint.rule.engine.core.api.* +import org.jetbrains.kotlin.com.intellij.lang.ASTNode + +class NoVarRule : + Rule( + ruleId = RuleId("$CUSTOM_RULE_SET_ID:no-var"), + about = About( + maintainer = "jmailen", + repositoryUrl = "https://gtihub.com/jeremymailen/kotlinter-gradle" + ) + ), + RuleAutocorrectApproveHandler { + override fun beforeVisitChildNodes( + node: ASTNode, + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision + ) { + if (node.elementType == ElementType.VAR_KEYWORD) { + emit(node.startOffset, "Use val instead of var", false) + } + } +} diff --git a/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt new file mode 100644 index 00000000..f698e7e3 --- /dev/null +++ b/test-project/rules/src/main/kotlin/org/jmailen/ktlint/rule/RuleSetProvider.kt @@ -0,0 +1,16 @@ +package org.jmailen.ktlint.rule + +import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 +import com.pinterest.ktlint.rule.engine.core.api.RuleProvider +import com.pinterest.ktlint.rule.engine.core.api.RuleSetId + +internal val CUSTOM_RULE_SET_ID = "kotlinter-test-rules" + +class CustomRuleSetProvider : RuleSetProviderV3(RuleSetId(CUSTOM_RULE_SET_ID)) { + override fun getRuleProviders(): Set = + setOf( + RuleProvider { + NoVarRule() + }, + ) +} diff --git a/test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 b/test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 new file mode 100644 index 00000000..683baa14 --- /dev/null +++ b/test-project/rules/src/main/resources/META-INF/services/com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3 @@ -0,0 +1 @@ +org.jmailen.ktlint.rule.CustomRuleSetProvider diff --git a/test-project/service/build.gradle.kts b/test-project/service/build.gradle.kts new file mode 100644 index 00000000..ca4fb84b --- /dev/null +++ b/test-project/service/build.gradle.kts @@ -0,0 +1,8 @@ +plugins { + kotlin("jvm") + id("org.jmailen.kotlinter") +} + +dependencies { + ktlint(project(":rules")) +} diff --git a/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt new file mode 100644 index 00000000..5952ecef --- /dev/null +++ b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/CustomNoVar.kt @@ -0,0 +1,5 @@ +package org.jmailen.gradle.kotlinter.sample + +class CustomNoVar { + var test: String = "test" +} diff --git a/test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt similarity index 62% rename from test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt rename to test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt index cc7fbd3b..b4b14a74 100644 --- a/test-project/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBodyClass.kt +++ b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt @@ -1,4 +1,4 @@ package org.jmailen.gradle.kotlinter.sample -class EmptyClassBodyClass { +class EmptyClassBody { } diff --git a/test-project/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt b/test-project/service/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt similarity index 100% rename from test-project/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt rename to test-project/service/src/test/kotlin/org/jmailen/gradle/kotlinter/sample/OpSpacing.kt diff --git a/test-project/settings.gradle.kts b/test-project/settings.gradle.kts index e8bbe0f0..c5f7e923 100644 --- a/test-project/settings.gradle.kts +++ b/test-project/settings.gradle.kts @@ -10,3 +10,6 @@ dependencyResolutionManagement { plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0" } + +include(":service") +include(":rules") From 81b92d7d99786f99691b919119292b3f8a3cd19f Mon Sep 17 00:00:00 2001 From: Jeremy Mailen Date: Wed, 11 Dec 2024 22:58:31 -0800 Subject: [PATCH 2/3] Don't fail test project build on lint --- test-project/service/build.gradle.kts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test-project/service/build.gradle.kts b/test-project/service/build.gradle.kts index ca4fb84b..88a9f1bf 100644 --- a/test-project/service/build.gradle.kts +++ b/test-project/service/build.gradle.kts @@ -6,3 +6,7 @@ plugins { dependencies { ktlint(project(":rules")) } + +kotlinter { + ignoreLintFailures = true +} From d83dcdbbc810b11386ce0f32e8efca5d0ccb183e Mon Sep 17 00:00:00 2001 From: Jeremy Mailen Date: Wed, 11 Dec 2024 23:18:39 -0800 Subject: [PATCH 3/3] Fix test --- .github/workflows/default.yml | 3 +-- test-project/service/build.gradle.kts | 1 + .../org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt | 4 ---- 3 files changed, 2 insertions(+), 6 deletions(-) delete mode 100644 test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt diff --git a/.github/workflows/default.yml b/.github/workflows/default.yml index 81fc6065..a7b7f97d 100644 --- a/.github/workflows/default.yml +++ b/.github/workflows/default.yml @@ -73,8 +73,7 @@ jobs: - name: Assert fixes run: | cd test-project/service/build/reports/ktlint - grep 'no-empty-class-body' main-format.txt | grep -q 'EmptyClassBody.kt:3:27' - grep 'kotlinter-test-rules:no-var' main-format.txt | grep -q 'NoVar.kt:4:5' + grep 'kotlinter-test-rules:no-var' main-lint.txt | grep -q 'CustomNoVar.kt:4:5' grep 'op-spacing' test-format.txt | grep -q 'OpSpacing.kt:5:16' - name: Upload reports diff --git a/test-project/service/build.gradle.kts b/test-project/service/build.gradle.kts index 88a9f1bf..f01fc325 100644 --- a/test-project/service/build.gradle.kts +++ b/test-project/service/build.gradle.kts @@ -8,5 +8,6 @@ dependencies { } kotlinter { + reporters = arrayOf("plain", "checkstyle") ignoreLintFailures = true } diff --git a/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt b/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt deleted file mode 100644 index b4b14a74..00000000 --- a/test-project/service/src/main/kotlin/org/jmailen/gradle/kotlinter/sample/EmptyClassBody.kt +++ /dev/null @@ -1,4 +0,0 @@ -package org.jmailen.gradle.kotlinter.sample - -class EmptyClassBody { -}