Skip to content

Commit

Permalink
New rule 5.2.4 for lambda length
Browse files Browse the repository at this point in the history
### What's done:
* Added rule logic
* Added new warning
* Added new test
  • Loading branch information
Cheshiriks committed Jan 18, 2021
1 parent 4eacdd3 commit ccf1b2b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
2 changes: 0 additions & 2 deletions diktat-rules/src/main/kotlin/generated/WarningNames.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ public object WarningNames {

public const val RUN_BLOCKING_INSIDE_ASYNC: String = "RUN_BLOCKING_INSIDE_ASYNC"

public const val TOO_MANY_LINES_IN_LAMBDA: String = "TOO_MANY_LINES_IN_LAMBDA"

public const val SINGLE_CONSTRUCTOR_SHOULD_BE_PRIMARY: String =
"SINGLE_CONSTRUCTOR_SHOULD_BE_PRIMARY"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.pinterest.ktlint.core.ast.ElementType
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

/**
* Rule 5.2.4 check lambda length without parameters
* Rule 5.2.5 check lambda length without parameters
*/
class LambdaLengthRule(private val configRules: List<RulesConfig>) : Rule("lambda-length") {
private val configuration by lazy {
Expand Down Expand Up @@ -40,12 +40,12 @@ class LambdaLengthRule(private val configRules: List<RulesConfig>) : Rule("lambd
val copyNode = node.clone() as ASTNode
val sizeLambda = countCodeLines(copyNode)
if (sizeLambda > configuration.maxLambdaLength) {
copyNode.findAllNodesWithCondition({it.elementType == ElementType.LAMBDA_EXPRESSION}).forEachIndexed {index, node ->
copyNode.findAllNodesWithCondition({ it.elementType == ElementType.LAMBDA_EXPRESSION }).forEachIndexed { index, node ->
if (index > 0) {
node.treeParent.removeChild(node)
}
}
val isIt: Boolean = copyNode.findAllNodesWithSpecificType(ElementType.REFERENCE_EXPRESSION).map {re -> re.text}.contains("it")
val isIt = copyNode.findAllNodesWithSpecificType(ElementType.REFERENCE_EXPRESSION).map {re -> re.text}.contains("it")
val parameters = node.findChildByType(ElementType.FUNCTION_LITERAL)?.findChildByType(ElementType.VALUE_PARAMETER_LIST)
if (parameters == null && isIt) {
Warnings.TOO_MANY_LINES_IN_LAMBDA.warn(configRules, emitWarn, isFixMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ class SmartCastRule(private val configRules: List<RulesConfig>) : Rule("smart-ca

val identifier = node.getFirstChildWithType(REFERENCE_EXPRESSION)?.text

node.getAllChildrenWithType(WHEN_ENTRY).forEach { node ->
if (node.hasChildOfType(WHEN_CONDITION_IS_PATTERN) && identifier != null) {
val type = node.getFirstChildWithType(WHEN_CONDITION_IS_PATTERN)!!
node.getAllChildrenWithType(WHEN_ENTRY).forEach { entry ->
if (entry.hasChildOfType(WHEN_CONDITION_IS_PATTERN) && identifier != null) {
val type = entry.getFirstChildWithType(WHEN_CONDITION_IS_PATTERN)!!
.getFirstChildWithType(TYPE_REFERENCE)?.text

val callExpr = node.findAllNodesWithSpecificType(BINARY_WITH_TYPE).firstOrNull()
val callExpr = entry.findAllNodesWithSpecificType(BINARY_WITH_TYPE).firstOrNull()
val blocks = listOf(IsExpressions(identifier, type ?: ""))

callExpr?.let {
Expand Down
2 changes: 1 addition & 1 deletion info/guide/guide-TOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ I [Preface](#c0)
* [5.2.1 The lambda parameter of the function should be placed at the end of the argument list](#r5.2.1)
* [5.2.2 Number of function parameters should be limited to five](#r5.2.2)
* [5.2.3 Use default values for function arguments instead of overloading them](#r5.2.3)
* [5.2.4 Long lambdas should have explicit parameters](#r5.2.4)
* [5.2.5 Long lambdas should have explicit parameters](#r5.2.4)

[6. Classes, interfaces, and extension functions](#c6)
* [6.1 Classes](#c6.1)
Expand Down
2 changes: 1 addition & 1 deletion info/guide/guide-chapter-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ GlobalScope.async {
}
}
```
#### <a name="r5.2.4"></a> 5.2.4 Long lambdas should have explicit parameters
#### <a name="r5.2.5"></a> 5.2.5 Long lambdas should have explicit parameters
The lambda without parameters shouldn't be too long.
If a lambda is too long, it can confuse the user. Lambda without parameters should consist of 10 lines (non-empty and non-comment) in total.

0 comments on commit ccf1b2b

Please sign in to comment.