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
Paul Dingemans
02/20/2023, 12:10 PM
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
Sha Sha Chu
02/20/2023, 8:11 PM
would setting the editorconfig also work? ie enabling the rule only on the specified paths
p
Paul Dingemans
02/20/2023, 8:16 PM
Ah yes, I see what you mean. You could add a glob like:
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
Ma Sapereaude
02/20/2023, 11:54 PM
Were there any options for
stopTraversalOfAST
in older Ktlint versions, for example, in
0.37.2
v.?
p
Paul Dingemans
02/21/2023, 11:28 AM
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.