Detekt: Filename rule doesn't work

Created on 10 Sep 2019  路  4Comments  路  Source: detekt/detekt

Expected Behavior

Filename rule should checks if top level class matches the filename

Observed Behavior

Detekt doesn't report any problem with filenames

Steps to Reproduce

  1. Create file named Foo.kt
    with content
class Bar {
}
  1. Setup Detekt to check Filename rule

Your Environment

  • Version of detekt used: 1.0.1
  • Version of Gradle used (if applicable): 5.4.1
  • Operating System and version: Mac OS 10.14.6
bug formatting

Most helpful comment

So looks like the Filename rule is using a custom user data attached on the ASTNode to check the path of the file.

https://github.com/pinterest/ktlint/blob/b63cd7f4ee26e32d2bf92fad5ae761382f77f0ba/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FilenameRule.kt#L43-L47

This KtLint.FILE_PATH_USER_DATA_KEY is initialised only when calling .lint() or .format() directly on the KtLint class. The way how detekt wraps over ktLint is by directly invoking every single rule.
In this case the Filename is simply ignoring all the files as the KtLint.FILE_PATH_USER_DATA_KEY is never set.

We should probably extend the Filename wrap to provide the userData configuration that the ktLint rule is expecting (or either remove this rule).

@danikula as a workaround, you can use the MatchingDeclarationName rule that is not a ktlint wrap and offers a similar behavior.

All 4 comments

Definitely a bug. I was able to reproduce it and I'm currently investigating on what could be the cause.

So looks like the Filename rule is using a custom user data attached on the ASTNode to check the path of the file.

https://github.com/pinterest/ktlint/blob/b63cd7f4ee26e32d2bf92fad5ae761382f77f0ba/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/FilenameRule.kt#L43-L47

This KtLint.FILE_PATH_USER_DATA_KEY is initialised only when calling .lint() or .format() directly on the KtLint class. The way how detekt wraps over ktLint is by directly invoking every single rule.
In this case the Filename is simply ignoring all the files as the KtLint.FILE_PATH_USER_DATA_KEY is never set.

We should probably extend the Filename wrap to provide the userData configuration that the ktLint rule is expecting (or either remove this rule).

@danikula as a workaround, you can use the MatchingDeclarationName rule that is not a ktlint wrap and offers a similar behavior.

@3flex @schalkms @arturbosch What's the plan here? Do we want to remove the Filename wrapper rule or adapt it to make it work?

The latter sounds better but we would really be prone to breaking changes from the ktLint side.

Thanks for investigating this!
We have done something similar for the editor config key:

https://github.com/arturbosch/detekt/blob/master/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRule.kt#L52

We could put:
https://github.com/arturbosch/detekt/blob/master/detekt-api/src/main/kotlin/io/gitlab/arturbosch/detekt/api/internal/PathMatchers.kt#L28
inside each KtFile.

Sounds doable if we can access KtLint.FILE_PATH_USER_DATA_KEY of find that key by string name.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BraisGabin picture BraisGabin  路  4Comments

arturbosch picture arturbosch  路  4Comments

sewerk picture sewerk  路  3Comments

jcornaz picture jcornaz  路  4Comments

arturdryomov picture arturdryomov  路  4Comments