implement UnnecessaryLayoutWrapper rule#23
Conversation
| import org.jetbrains.kotlin.psi.psiUtil.getChildOfType | ||
| import org.jetbrains.kotlin.psi.psiUtil.getChildrenOfType | ||
|
|
||
| class UnnecessaryLayoutWrapper(config: Config = Config.empty) : Rule(config) { |
There was a problem hiding this comment.
Please add examples of compliant and non compliant code (we'll use them later to generate docs).
See example here
| @@ -0,0 +1,75 @@ | |||
| package ru.kode.detekt.rule.compose | |||
|
|
||
| private val layoutNames = setOf("Box", "Column", "Row") | ||
|
|
||
| private val unnecessaryExpressions = mutableListOf<KtCallExpression>() |
There was a problem hiding this comment.
There's no need to accumulate expressions and report them in postVisit, you can report them right when you find them, i.e. instead of having
unnecessaryExpressions.add(expression)just call
report(...)
Detekt will visit all call expressions anyway, so no need to complicate with preVisit, postVisit and state here...
| super.visitCallExpression(expression) | ||
| } | ||
|
|
||
| private fun KtBlockExpression.hasSingleLayout(): Boolean { |
| ComposableFunctionName: | ||
| active: true | ||
| UnnecessaryLayoutWrapper: | ||
| active: true |
There was a problem hiding this comment.
This also needs to be added to the sample block in the README.md, some people will be copying rules from here (config.yml doesn't work for older detekt versions)
There was a problem hiding this comment.
Secondly, this rule must be added to the rule set provider.
| val code = """ | ||
| @Composable | ||
| fun Test() { | ||
| Box { |
There was a problem hiding this comment.
you should test all supported layout types here. data-driven testing feature can be used here:
https://kotest.io/docs/framework/datatesting/data-driven-testing.html
The rule reports if the use of Box, Column or Row is not required. For example, in code below, we can easily remove the parent column.