Filename rule should checks if top level class matches the filename
Detekt doesn't report any problem with filenames
Foo.ktclass Bar {
}
Filename ruleDefinitely 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.
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:
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.
Most helpful comment
So looks like the
Filenamerule is using a custom user data attached on theASTNodeto 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_KEYis initialised only when calling.lint()or.format()directly on theKtLintclass. The way how detekt wraps over ktLint is by directly invoking every single rule.In this case the
Filenameis simply ignoring all the files as theKtLint.FILE_PATH_USER_DATA_KEYis never set.We should probably extend the
Filenamewrap to provide the userData configuration that the ktLint rule is expecting (or either remove this rule).@danikula as a workaround, you can use the
MatchingDeclarationNamerule that is not a ktlint wrap and offers a similar behavior.