Hello! Is there an appropriate way of saying to th...
# ktlint
m
Hello! Is there an appropriate way of saying to the custom Ktlint rule (only custom rule, not other Ktlint default rules) what directories should it check? Now I use the following approach (in Ktlint custom rule class): 1. check the PACKAGE_DIRECTIVE elements -> 2. if the package name is equal to the expected one (related to the directory, which should be checked) -> 3. get parent tree/ parent-child elements -> 4. walk through them, searching for the target nodes (in my case I need to check the type of class fields) -> 5. describe the logic for the rule Are there some other alternatives in order to avoid extra tree/node traversing (maybe additional configurations should be provided)?
p
There is no such functionality to restrict a rule to certain directories only. But as soon as you know that a file does not fulfil the access criteria you can stop the traversal of the AST of that specific file by calling the
stopTraversalOfAST
function.
s
would setting the editorconfig also work? ie enabling the rule only on the specified paths
p
Ah yes, I see what you mean. You could add a glob like:
Copy code
[/some/dir/*.{kt,kts}]
ktlint_custom-rule-set_custom-rule = disabled
Most difficult thing is that this Glob might overlap with the default glob
[*.{kt.kts}]
. So it will depend on your use case what is most preferable.
m
Were there any options for
stopTraversalOfAST
in older Ktlint versions, for example, in
0.37.2
v.?
p
No. Prior to introducing
stopTraversalOfAST
the
visit
method was called for each and every node in the AST. Usually that method contained a if-statement at the top to determine whether something needed to be done or not.