Ktlint: Unable to disable "filename" rule

Created on 21 Jun 2018  路  11Comments  路  Source: pinterest/ktlint

I have a couple of files on which I'd like to temporarily disable the recently-added filename rule. An example of such a rule violation in my project follows:

<snip>/GameSpecificationSpec.kt:1:1: object GameSpecificationBehavesAsDataClassSpec should be declared in a file named GameSpecificationBehavesAsDataClassSpec.kt (filename)

However, I can't seem to get ktlint to honor my ktlint-disable directive for this rule. Since ktlint reports the violation at row 1, column 1, I tried placing both of the following directives on the first line of the file in question:

// ktlint-disable filename

and

// ktlint-disable

Unfortunately, neither had any effect with respect to the filename rule, and the violation was still reported. Am I missing something obvious about how to disable this rule?

I'm wondering if, because FilenameRule applies to the root node of the AST, it's simply running before any suppression directives are encountered in the remainder of the AST? In which case, is there some other syntax one can use to disable such a "root" rule?

All 11 comments

Confirmed and thank you for the ticket. I'll have a fix ready within 24h.

0.24.0 released :balloon:.
// ktlint-disable filename at the top of the file should work now.

Confirmed. Thanks, @shyiko!

0.24.0 released .
// ktlint-disable filename at the top of the file should work now.

Are there any other options? Since I have in each class there is an annotation that should be at the top, but if I write below it but above the package name, the command doesn't work.

@mnewlive You can add .editorconfig with following content:

[*.{kt,kts}]
disabled_rules=filename

@mnewlive You can add .editorconfig with following content:

[*.{kt,kts}]
disabled_rules=filename

using this method I can disable the rule for all classes,
but how can I disable the rule for only one class for example?

// ktlint-disable filename at the top of the file should work now.

@mnewlive just to confirm - you want to disable this rule for a specific folder?

// ktlint-disable filename at the top of the file should work now.

@mnewlive just to confirm - you want to disable this rule for a specific folder?

for a specific class/classes

then you could modify .editorconfig to following content:

[src/main/kotlin/**/*Interface.kt]
disabled-rules=filename

For more info how to use .editorconfig you could check https://editorconfig.org/

then you could modify .editorconfig to following content:

[src/main/kotlin/**/*Interface.kt]
disabled-rules=filename

For more info how to use .editorconfig you could check https://editorconfig.org/

I tried to write smth like this:

# http://editorconfig.org
root = true

[src/main/java/com/mandarine/targetList/interfaces/OnBackPressListenerEEE.kt]
disabled-rules = filename

But also get error for my class:

/home/vadimm/AndroidStudioProjects/target-list/app/src/main/java/com/mandarine/targetList/interfaces/OnBackPressListenerEEE.kt:1:1: interface OnBackPressListener should be declared in a file named OnBackPressListener.kt (cannot be auto-corrected)

@mnewlive you should remove spaces in assignments, see https://github.com/pinterest/ktlint/issues/555

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jacksgong picture Jacksgong  路  3Comments

filipedelimabrito picture filipedelimabrito  路  3Comments

vanniktech picture vanniktech  路  5Comments

tonilopezmr picture tonilopezmr  路  4Comments

pkubowicz picture pkubowicz  路  4Comments