Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test adding custom rule sets #421

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading