Hi :android-wave: , For writing custom detekt rule...
# detekt
a
Hi 👋 , For writing custom detekt rules, Are there any docs on Kotlin PSI? I have written a couple of rules by referring to rules from the marketplace, but docs would help to understand the code better and extend my rules for more scenarios. thank you color
b
No they are not. I ask for the same when I started with detekt.
a
😢
How do you write rules without docs? Can you please share more info in this?
b
TDD. I write tests for the cases I want to cover and then make the tests pass. Also thinking about similar rules on detekt and reading their code.
a
Yes, I am trying to do TDD. I have written some test and it is currently failing. But, I do not know how to fix the issue.
UseTrailingComma.kt
UseTrailingCommaTest.kt
A custom detekt rule to ensure trailing commas are used wherver applicable. The same is already available in klint. I am trying to write one in detekt without using klint. https://github.com/detekt/detekt/blob/main/detekt-formatting/src/main/kotlin/io/gi[…]rturbosch/detekt/formatting/wrappers/TrailingCommaOnCallSite.kt
b
At the rule level detekt and ktlint are kind of similar. So the ktlint code should help.
a
I have referred this code, https://github.com/pinterest/ktlint/blob/master/ktlint-ruleset-standard/src/main/k[…]st/ktlint/ruleset/standard/rules/TrailingCommaOnCallSiteRule.kt But, I am unable to adapt the same to the detekt rule. Also, I haven't done linting, custom klint or custom detekt before. My idea is to understand how the current rules are created and then to create new rules to align with my project requirements. Is there any better approach to this? My current approach is more like trail and error as I do not understand what a particular method does since there is no KDocs or any additional info I could find. sad panda
b
Then you picked a difficult rule to start with. I understand the "pain" I felt the same at the begging. There is an intellij plugin that let you see the ast tree of any file. It helps to see what you should look for on the api.
And the trail and error is the only way I know. That and read similar rules. The problem is that detekt doesn't have rules related with formatting (as the one that you want to implement). In general those rules need to use ast. And detekt usually works on the psi level. It doesn't mean that you can't implement it. The information is there and the rule can be written. But you will find few or none examples.
a
Thanks for sharing the info. I was hoping that this would be an easy task laugh cry face palm .
There is an intellij plugin that let you see the ast tree of any file. It helps to see what you should look for on the api.
Can you please share what plugin this is?
a
thank you color
t
Suggest the LLM tools like copilot, openai, etc... I've found it really helpful in working with undocumented parts of the kotlin repo, like a k2 compiler plugin.
a
I have tried both Gemini and ChatGPT. The output is mostly made up of functions and properties that doesn't exist.
a
Write the code and then read the nodes given by the psiviewer, that way you will know which node to target. I think
KtCallExpression
,
KtNamedFunction
,
KtEnumEntry
should be your first target. Then you can check the parent class of these class to see if you need to visit parent class to make you code handle more cases