From 998ee8befae9b0700d71affdd3d94a6c254ded16 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 25 May 2022 16:05:19 +0300 Subject: [PATCH] ### 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 * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 30 ++++++++++++------- .../LongDotQualifiedExpressionExpected.kt | 17 ++++++++++- .../LongDotQualifiedExpressionTest.kt | 13 ++++++++ 3 files changed, 48 insertions(+), 12 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 c2f03edbbc..709996f574 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 @@ -1,5 +1,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3 +import com.pinterest.ktlint.core.ast.ElementType import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig @@ -50,6 +51,8 @@ import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.RPAR +import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS +import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.SHORT_STRING_TEMPLATE_ENTRY import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST @@ -168,7 +171,7 @@ class LineLength(configRules: List) : DiktatRule( WHEN_CONDITION_WITH_EXPRESSION -> return None() EOL_COMMENT -> return checkComment(parent, configuration) FUNCTION_LITERAL -> return Lambda(parent) - STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION -> { + STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> { stringOrDot = parent val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { @@ -302,10 +305,14 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { - val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT)?.first + val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT) + val nodeSafeAccess = searchRightSplitAfterType(wrongNode, configuration, SAFE_ACCESS) nodeDot?.let { return DotQualifiedExpression(wrongNode) - } ?: return None() + } ?: nodeSafeAccess?.let { + return DotQualifiedExpression(wrongNode) + } + return None() } private fun checkFunAndProperty(wrongNode: ASTNode) = @@ -372,8 +379,9 @@ class LineLength(configRules: List) : DiktatRule( private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { val node = wrongDotQualifiedExpression.node - val dot = node.getFirstChildWithType(DOT) - node.appendNewlineMergingWhiteSpace(dot, dot) + val dot = node.getFirstChildWithType(DOT) ?: node.getFirstChildWithType(SAFE_ACCESS) + val nodeBeforeDot = dot?.treePrev + node.appendNewlineMergingWhiteSpace(nodeBeforeDot, dot) } @Suppress("UnsafeCallOnNullableType", "MagicNumber") @@ -555,14 +563,14 @@ class LineLength(configRules: List) : DiktatRule( *@param node node in which to search *@param dotList mutable list of ASTNode to store nodes */ - private fun searchDot(node: ASTNode, dotList: MutableList) { - if (node.elementType == DOT_QUALIFIED_EXPRESSION) { + private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList) { + if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION) { node.getChildren(null) .filter { - it.elementType == DOT_QUALIFIED_EXPRESSION + it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION } .forEach { - searchDot(it, dotList) + searchDotOrSafeAccess(it, dotList) } dotList.add(node) } @@ -628,7 +636,7 @@ class LineLength(configRules: List) : DiktatRule( val list: MutableList = mutableListOf() when (type) { OPERATION_REFERENCE -> searchBinaryExpression(parent, list) - DOT -> searchDot(parent, list) + DOT, SAFE_ACCESS -> searchDotOrSafeAccess(parent, list) } return list.map { it to positionByOffset(it.getFirstChildWithType(type)?.startOffset ?: configuration.lineLength.toInt()).second @@ -636,7 +644,7 @@ class LineLength(configRules: List) : DiktatRule( .sortedBy { it.second } .reversed() .firstOrNull { (it, offset) -> - offset + (it.getFirstChildWithType(type)?.text?.length ?: 0) <= configuration.lineLength + 1 + offset + (it.getFirstChildWithType(type)?.text?.length ?:0)<= configuration.lineLength + 1 } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt index b25f02b974..f11b671c12 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt @@ -1,4 +1,19 @@ package test.paragraph3.long_line val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot -.Qualified.Expression \ No newline at end of file +.Qualified.Expression + +val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot +?.Qualified?.Expression + +val C = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! +.Dot!!.Qualified!!.Expression + +val D = This.Is.Veeeeryyyyyyy.Loooooong.Dot + .Qualified.Expression + +val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot + ?.Qualified?.Expression + +val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! + .Dot!!.Qualified!!.Expression \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt index be356364b9..b841210240 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt @@ -1,3 +1,16 @@ package test.paragraph3.long_line val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression + +val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot?.Qualified?.Expression + +val C = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!!.Dot!!.Qualified!!.Expression + +val D = This.Is.Veeeeryyyyyyy.Loooooong.Dot + .Qualified.Expression + +val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot + ?.Qualified?.Expression + +val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! + .Dot!!.Qualified!!.Expression \ No newline at end of file