Skip to content

Commit

Permalink
Test adding custom rule sets (#421)
Browse files Browse the repository at this point in the history
* Test adding custom rule sets

Rule sets can be configured by adding them to the `ktlint` dependency configuration

* Don't fail test project build on lint

* Fix test
  • Loading branch information
jeremymailen authored Dec 12, 2024
1 parent c4ae1fd commit af7ea9f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ 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 '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
Expand Down
3 changes: 1 addition & 2 deletions test-project/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
plugins {
kotlin("jvm") version "2.1.0"
id("org.jmailen.kotlinter")
kotlin("jvm") version "2.1.0" apply false
}
7 changes: 7 additions & 0 deletions test-project/rules/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
kotlin("jvm")
}

dependencies {
compileOnly("com.pinterest.ktlint:ktlint-cli-ruleset-core:1.5.0")
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<RuleProvider> =
setOf(
RuleProvider {
NoVarRule()
},
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jmailen.ktlint.rule.CustomRuleSetProvider
13 changes: 13 additions & 0 deletions test-project/service/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
kotlin("jvm")
id("org.jmailen.kotlinter")
}

dependencies {
ktlint(project(":rules"))
}

kotlinter {
reporters = arrayOf("plain", "checkstyle")
ignoreLintFailures = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jmailen.gradle.kotlinter.sample

class CustomNoVar {
var test: String = "test"
}
3 changes: 3 additions & 0 deletions test-project/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ dependencyResolutionManagement {
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"
}

include(":service")
include(":rules")

This file was deleted.

0 comments on commit af7ea9f

Please sign in to comment.