Hello! I created custom Ktlint rule which works pe...
# ktlint
f
Hello! I created custom Ktlint rule which works perfectly, but sometimes I want to suppress it. I tried
// ktlint-disable my-custom-rule
and also
Copy code
/* ktlint-disable my-custom-rule */
...this should not fail
/* ktlint-enable my-custom-rule */
But it does not work. Please, is there any way how to suppress my custom rule? Thank you!
it works with
// ktlint-disable
but I am not sure if it is correct usage 🙂
r
file an issue? both should work actually, that's likely a bug
f
My custom rule:
Copy code
class NoExposedTransactionImportRule : Rule("no-exposed-transaction-import") {
    override fun visit(
        node: ASTNode,
        autoCorrect: Boolean,
        emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
    ) {
        if (node.elementType == KtStubElementTypes.IMPORT_DIRECTIVE) {
            val importDirective = node.psi as KtImportDirective
            val path = importDirective.importPath?.pathStr
            if (path?.equals("org.jetbrains.exposed.sql.transactions.transaction") == true) {
                emit(node.startOffset, "Importing exposed transaction: '$path'. You should use exposedTransactionWrapper instead", false)
            }
        }
    }
}
RulesetProvider:
Copy code
class CustomRuleSetProvider : RuleSetProvider {
    override fun get() = RuleSet("custom-ktlint-rules", NoExposedTransactionImportRule())
}
Usage in `build.gradle`:
Copy code
dependencies {

        ktlintRuleset project(":custom-ktlint-rules")
    }

    ktlint {
        disabledRules = ['import-ordering', 'no-wildcard-imports', 'no-semi'] // some of globally disabled rules
    }
I also tried to use
// ktlint-disable custom-ktlint-rules
but
ktlintCheck
still fails.
s
@Filip Lastic you need to prefix it with the ruleset name - so
custom-ktlint-rules:my-custom-rule
🤦 1
f
Eh thank you!! I knew that I am doing something wrong 🙂