From 276fd00f5da726d3afd56b8e43681763b9d9b416 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 20:34:01 +0300 Subject: [PATCH] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index b037219e73..7dcb15b5dd 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -384,24 +384,6 @@ class LineLength(configRules: List) : DiktatRule( } } - /** - * Runs through the sorted list [rightBinList], finds its last element, the type of which is included in the set [typesList] and adds it in the list [returnList] - */ - @Suppress("TYPE_ALIAS", "UnsafeCallOnNullableType") - private fun addInSmartListBinExpression( - returnList: MutableList?>, - rightBinList: List>, - typesList: List, - configuration: LineLengthConfiguration - ) { - val expression = rightBinList.firstOrNull { (it, offset) -> - val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE) - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference!!.firstChildNode.elementType in typesList - } - returnList.add(expression) - } - /** * Finds the first binary expression closer to the separator */ @@ -444,7 +426,7 @@ class LineLength(configRules: List) : DiktatRule( it to offset } .sortedBy { it.second } - .lastOrNull { (it, offset) -> + .lastOrNull { (_, offset) -> offset <= configuration.lineLength + 1 } } @@ -547,8 +529,8 @@ class LineLength(configRules: List) : DiktatRule( private class BinaryExpression(node: ASTNode) : LongLineFixableCases(node) { override fun fix() { val nodeOperationReference = node.findChildByType(OPERATION_REFERENCE) - val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { - nodeOperationReference.treeNext + val nextNode = if (nodeOperationReference?.firstChildNode?.elementType != ELVIS) { + nodeOperationReference?.treeNext } else { if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { nodeOperationReference.treePrev @@ -585,8 +567,8 @@ class LineLength(configRules: List) : DiktatRule( val rightSplitNode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] val nodeOperationReference = rightSplitNode?.first?.getFirstChildWithType(OPERATION_REFERENCE) rightSplitNode?.let { - val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { - nodeOperationReference.treeNext + val nextNode = if (nodeOperationReference?.firstChildNode?.elementType != ELVIS) { + nodeOperationReference?.treeNext } else { if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { nodeOperationReference.treePrev @@ -594,7 +576,7 @@ class LineLength(configRules: List) : DiktatRule( nodeOperationReference } } - if (!nextNode.text.contains(("\n"))) { + if (!nextNode?.text?.contains(("\n"))!!) { rightSplitNode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode) } } @@ -614,7 +596,7 @@ class LineLength(configRules: List) : DiktatRule( val binList: MutableList = mutableListOf() searchBinaryExpression(parent, binList) val rightBinList = binList.map { - it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second + it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)?.startOffset ?: 0).second } .sortedBy { it.second } .reversed() @@ -622,8 +604,8 @@ class LineLength(configRules: List) : DiktatRule( addInSmartListBinExpression(returnList, rightBinList, logicListOperationReference, configuration) addInSmartListBinExpression(returnList, rightBinList, compressionListOperationReference, configuration) val expression = rightBinList.firstOrNull { (it, offset) -> - val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)!!.firstChildNode.elementType - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && + val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)?.firstChildNode?.elementType + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text?.length ?: 0) <= configuration.lineLength + 1 && binOperationReference !in logicListOperationReference && binOperationReference !in compressionListOperationReference && binOperationReference != EXCL } returnList.add(expression) @@ -655,8 +637,8 @@ class LineLength(configRules: List) : DiktatRule( ) { val expression = rightBinList.firstOrNull { (it, offset) -> val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE) - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference!!.firstChildNode.elementType in typesList + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text?.length ?: 0) <= configuration.lineLength + 1 && + binOperationReference?.firstChildNode?.elementType in typesList } returnList.add(expression) } @@ -667,7 +649,7 @@ class LineLength(configRules: List) : DiktatRule( */ private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) { override fun fix() { - node.appendNewlineMergingWhiteSpace(null, node.findChildByType(EQ)!!.treeNext) + node.appendNewlineMergingWhiteSpace(null, node.findChildByType(EQ)?.treeNext) } } @@ -679,8 +661,8 @@ class LineLength(configRules: List) : DiktatRule( * Splits Lambda expressions - add splits lines, thereby making the lambda expression a separate line */ override fun fix() { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)!!.treeNext, node.findChildByType(LBRACE)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)!!.treePrev, node.findChildByType(RBRACE)!!.treePrev) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)?.treeNext, node.findChildByType(LBRACE)?.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)?.treePrev, node.findChildByType(RBRACE)?.treePrev) } } @@ -743,12 +725,12 @@ class LineLength(configRules: List) : DiktatRule( var startOffset = 0 node.getFirstChildWithType(COMMA)?.let { if (positionByOffset(it.startOffset).second > lineLength) { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)?.treeNext, node.findChildByType(LPAR)?.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = this.maximumLineLength.lineLength.toInt() } } ?: node.getFirstChildWithType(RPAR)?.let { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)?.treeNext, node.findChildByType(LPAR)?.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = this.maximumLineLength.lineLength.toInt() }