Reputation: 337
I have been trying to write custom rules for ktlint. When I try to execute my custom rule via terminal I get no errors, standard rules of ktlint are executed but my custom rules are ignored. May be somebody has any ideas what I am missing?
My build.gradle:
plugins {
id 'java-library'
id 'org.jetbrains.kotlin.jvm' version '1.3.40'
id 'maven'
}
group 'com.xxx'
repositories {
mavenCentral()
jcenter()
}
configurations {
ktlint
}
dependencies {
compileOnly "com.pinterest.ktlint:ktlint-core:0.32.0"
testCompile "junit:junit:4.12"
testCompile "org.assertj:assertj-core:3.10.0"
testCompile "com.pinterest.ktlint:ktlint-core:0.32.0"
testCompile "com.pinterest.ktlint:ktlint-test:0.32.0"
}
task ktlint(type: JavaExec, dependsOn: classes) {
main = 'com.pinterest.ktlint.Main'
// adding compiled classes to the classpath so that ktlint would validate project's sources
// using its own ruleset (in other words to dogfood)
classpath = configurations.ktlint + sourceSets.main.output
args '--debug', 'src/**/*.kt'
}
check.dependsOn ktlint
My custom rule class (for testing purposes it is supposed to emit always an lint error):
package com.xxx.ktlint
import com.pinterest.ktlint.core.Rule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
class TempRule : Rule("no-var") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
emit(node.startOffset, "#### TEST: KTLINT ERROR", false)
}
}
My com.pinterest.ktlint.core.RuleSetProvider
:
com.xxx.ktlint.CustomRuleSetProvider
I create the jar by executing the gradle task "jar" and then I execute ktlint like this in the terminal:
ktlint -R ../path/to/ktlint-rules.jar "path/to/file/**/file-to-be-checked.kt"
I get no errors and ktlint applies to the file that should be checked all the standard rules as expected but my custom rules are just ignored. The jar-file itself does not seem to be ignored since when I put a non existing name in the command I get an error.
Any ideas?
Upvotes: 0
Views: 4144
Reputation: 337
The problem was that I had installed locally ktlint with Version 0.31.0
(before pinterest took over the code) and in my build.gradle
I was using 0.33.0
(after ptinerest took over the code). Since many packages got renamed those different version wouldn't get along with each other. After updating my local ktlint to version 0.33.0
the custom rule was applied as expected.
Upvotes: 1