From f7e6ed19233f4f5adf4d0ffae267bec56bc7bc91 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 28 Jun 2022 17:34:52 +0300 Subject: [PATCH 1/6] ## Whats Added: * add analytic fully-qualified * add test * correct warning message ## Issue (#1403) --- .../rules/chapter3/files/NewlinesRule.kt | 15 +++-- .../chapter3/files/NewlinesRuleWarnTest.kt | 2 +- .../LongDotQualifiedExpressionExpected.kt | 62 +++++++++++-------- .../LongDotQualifiedExpressionTest.kt | 47 +++++++++----- 4 files changed, 77 insertions(+), 49 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index 44237ad89b..96e9ca640c 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -86,12 +86,14 @@ import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.nextCodeSibling import com.pinterest.ktlint.core.ast.parent import com.pinterest.ktlint.core.ast.prevCodeSibling +import com.sun.org.apache.xpath.internal.operations.Bool import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet +import org.jetbrains.kotlin.fir.builder.Context import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtParameterList @@ -158,12 +160,14 @@ class NewlinesRule(configRules: List) : DiktatRule( isDotQuaOrSafeAccessOrPostfixExpression(it) && it.elementType != POSTFIX_EXPRESSION }.reversed() if (listDot.size > 3) { - val without = listDot.filterNot { + val without = listDot.filterIndexed { index, it -> val whiteSpaceBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev - whiteSpaceBeforeDotOrSafeAccess?.elementType == WHITE_SPACE && whiteSpaceBeforeDotOrSafeAccess.text.lines().size > 1 + val firstElem = it.firstChildNode + (firstElem.textContains('(') || firstElem.textContains('{')) && (index > 0) && ((whiteSpaceBeforeDotOrSafeAccess?.elementType != WHITE_SPACE) || + (whiteSpaceBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !whiteSpaceBeforeDotOrSafeAccess.textContains('\n'))) } - if (without.size > 1 || (without.size == 1 && without[0] != listDot[0])) { - WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access", node.startOffset, node) { + if (without.isNotEmpty()) { + WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access after first call expression", node.startOffset, node) { fixDotQualifiedExpression(listDot) } } @@ -193,7 +197,8 @@ class NewlinesRule(configRules: List) : DiktatRule( list.forEachIndexed { index, astNode -> val dotNode = astNode.getFirstChildWithType(DOT) ?: astNode.getFirstChildWithType(SAFE_ACCESS) val nodeBeforeDot = dotNode?.treePrev - if (index > 0) { + val firstElem = astNode.firstChildNode + if (index > 0 && (firstElem.textContains('(') || firstElem.textContains('{'))) { astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode) } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt index 9fff0a58bd..3085d0f6ac 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt @@ -26,7 +26,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) { mapOf("maxCallsInOneLine" to "1")) ) private val ruleId = "$DIKTAT_RULE_SET_ID:${NewlinesRule.NAME_ID}" - private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} should be split before second and other dot/safe access" + private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} should be split before second and other dot/safe access after first call expression" private val shouldBreakAfter = "${WRONG_NEWLINES.warnText()} should break a line after and not before" private val shouldBreakBefore = "${WRONG_NEWLINES.warnText()} should break a line before and not after" private val functionalStyleWarn = "${WRONG_NEWLINES.warnText()} should follow functional style at" diff --git a/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionExpected.kt index 3ddbd9f27a..49615088f2 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionExpected.kt @@ -1,21 +1,20 @@ package test.paragraph3.newlines -val elem1 = firstArgumentDot?.secondArgumentDot +val elem1 = firstArgumentDot()?.secondArgumentDot ?.thirdArgumentDot ?.fourthArgumentDot ?.fifthArgumentDot ?.sixthArgumentDot -val elem2 = firstArgumentDot?.secondArgumentDot +val elem2 = firstArgumentDot?.secondArgumentDot() ?.thirdArgumentDot ?.fourthArgumentDot ?.fifthArgumentDot ?.sixthArgumentDot -val elem3 = firstArgumentDot?.secondArgumentDot -?.thirdArgumentDot +val elem3 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot() ?.fourthArgumentDot ?.fifthArgumentDot ?.sixthArgumentDot @@ -24,70 +23,79 @@ val elem3 = firstArgumentDot?.secondArgumentDot val elem4 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot + firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot -val elem5 = firstArgumentDot!!.secondArgumentDot!! +val elem5 = firstArgumentDot()!!.secondArgumentDot()!! .thirdArgumentDot!! .fourthArgumentDot!! .fifthArgumentDot!! -.sixthArgumentDot +.sixthArgumentDot() -val elem6 = firstArgumentDot!!.secondArgumentDot!! -.thirdArgumentDot!! +val elem6 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot()!! .fourthArgumentDot!! -.fifthArgumentDot!! +.fifthArgumentDot()!! .sixthArgumentDot -val elem7 = firstArgumentDot!!.secondArgumentDot!! +val elem7 = firstArgumentDot!!.secondArgumentDot()!! .thirdArgumentDot!! -.fourthArgumentDot!! +.fourthArgumentDot()!! .fifthArgumentDot!! .sixthArgumentDot -val elem8 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot +val elem8 = firstArgumentDot()!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot -val elem9 = firstArgumentDot.secondArgumentDot -.thirdArgumentDot +val elem9 = firstArgumentDot().secondArgumentDot +.thirdArgumentDot() .fourthArgumentDot .fifthArgumentDot .sixthArgumentDot -val elem10 = firstArgumentDot.secondArgumentDot +val elem10 = firstArgumentDot.secondArgumentDot() .thirdArgumentDot .fourthArgumentDot -.fifthArgumentDot +.fifthArgumentDot() .sixthArgumentDot -val elem11 = firstArgumentDot.secondArgumentDot -.thirdArgumentDot +val elem11 = firstArgumentDot.secondArgumentDot.thirdArgumentDot() .fourthArgumentDot .fifthArgumentDot .sixthArgumentDot -val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot +val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot().thirdArgumentDot.fourthArgumentDot -val elem13 = firstArgumentDot!!.secondArgumentDot -?.thirdArgumentDot +val elem13 = firstArgumentDot!!.secondArgumentDot?.thirdArgumentDot() .fourthArgumentDot!! -.fifthArgumentDot +.fifthArgumentDot() ?.sixthArgumentDot -val elem14 = firstArgumentDot.secondArgumentDot -?.thirdArgumentDot!! +val elem14 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot()!! .fourthArgumentDot ?.fifthArgumentDot .sixthArgumentDot -val elem15 = firstArgumentDot?.secondArgumentDot!! -.thirdArgumentDot -.fourthArgumentDot +val elem15 = firstArgumentDot?.secondArgumentDot!!.thirdArgumentDot.fourthArgumentDot() .fifthArgumentDot!! .sixthArgumentDot + + +val elem16 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot + + +val elem17 = firstArgumentDot!!.secondArgumentDot.thirdArgumentDot!!.fourthArgumentDot.fifthArgumentDot!!.sixthArgumentDot + + +val elem18 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot + + +private val holder: java.util.concurrent.atomic.AtomicReference = java.util.concurrent.atomic.AtomicReference(valueToStore) + + +private val holder: kotlin.native.concurrent.AtomicReference = kotlin.native.concurrent.AtomicReference(valueToStore) diff --git a/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionTest.kt index 7ccb6cb55b..b32e0100a8 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/newlines/LongDotQualifiedExpressionTest.kt @@ -1,51 +1,66 @@ package test.paragraph3.newlines -val elem1 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot?.fifthArgumentDot?.sixthArgumentDot +val elem1 = firstArgumentDot()?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot?.fifthArgumentDot?.sixthArgumentDot -val elem2 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot +val elem2 = firstArgumentDot?.secondArgumentDot()?.thirdArgumentDot ?.fourthArgumentDot?.fifthArgumentDot?.sixthArgumentDot -val elem3 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot +val elem3 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot()?.fourthArgumentDot ?.fifthArgumentDot?.sixthArgumentDot val elem4 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot + firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot -val elem5 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot!!.fifthArgumentDot!!.sixthArgumentDot +val elem5 = firstArgumentDot()!!.secondArgumentDot()!!.thirdArgumentDot!!.fourthArgumentDot!!.fifthArgumentDot!!.sixthArgumentDot() -val elem6 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!! - .fourthArgumentDot!!.fifthArgumentDot!!.sixthArgumentDot +val elem6 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot()!! + .fourthArgumentDot!!.fifthArgumentDot()!!.sixthArgumentDot -val elem7 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot!! +val elem7 = firstArgumentDot!!.secondArgumentDot()!!.thirdArgumentDot!!.fourthArgumentDot()!! .fifthArgumentDot!!.sixthArgumentDot -val elem8 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot +val elem8 = firstArgumentDot()!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot -val elem9 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot +val elem9 = firstArgumentDot().secondArgumentDot.thirdArgumentDot().fourthArgumentDot.fifthArgumentDot.sixthArgumentDot -val elem10 = firstArgumentDot.secondArgumentDot.thirdArgumentDot - .fourthArgumentDot.fifthArgumentDot.sixthArgumentDot +val elem10 = firstArgumentDot.secondArgumentDot().thirdArgumentDot + .fourthArgumentDot.fifthArgumentDot().sixthArgumentDot -val elem11 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot +val elem11 = firstArgumentDot.secondArgumentDot.thirdArgumentDot().fourthArgumentDot .fifthArgumentDot.sixthArgumentDot -val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot +val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot().thirdArgumentDot.fourthArgumentDot -val elem13 = firstArgumentDot!!.secondArgumentDot?.thirdArgumentDot.fourthArgumentDot!!.fifthArgumentDot?.sixthArgumentDot +val elem13 = firstArgumentDot!!.secondArgumentDot?.thirdArgumentDot().fourthArgumentDot!!.fifthArgumentDot()?.sixthArgumentDot -val elem14 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot!!.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot +val elem14 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot()!!.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot -val elem15 = firstArgumentDot?.secondArgumentDot!!.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot!!.sixthArgumentDot \ No newline at end of file +val elem15 = firstArgumentDot?.secondArgumentDot!!.thirdArgumentDot.fourthArgumentDot().fifthArgumentDot!!.sixthArgumentDot + + +val elem16 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot + + +val elem17 = firstArgumentDot!!.secondArgumentDot.thirdArgumentDot!!.fourthArgumentDot.fifthArgumentDot!!.sixthArgumentDot + + +val elem18 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot + + +private val holder: java.util.concurrent.atomic.AtomicReference = java.util.concurrent.atomic.AtomicReference(valueToStore) + + +private val holder: kotlin.native.concurrent.AtomicReference = kotlin.native.concurrent.AtomicReference(valueToStore) From d015c77e4950c0d105c133128299a7483dd5fccb Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 28 Jun 2022 17:50:05 +0300 Subject: [PATCH 2/6] ## Whats Added: * add analytic fully-qualified * add test * correct warning message * run fix mode ## Issue (#1403) --- .../diktat/ruleset/rules/chapter3/files/NewlinesRule.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index 96e9ca640c..2a05e96ce3 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -86,14 +86,12 @@ import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.nextCodeSibling import com.pinterest.ktlint.core.ast.parent import com.pinterest.ktlint.core.ast.prevCodeSibling -import com.sun.org.apache.xpath.internal.operations.Bool import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet -import org.jetbrains.kotlin.fir.builder.Context import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtNamedFunction import org.jetbrains.kotlin.psi.KtParameterList @@ -164,10 +162,11 @@ class NewlinesRule(configRules: List) : DiktatRule( val whiteSpaceBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev val firstElem = it.firstChildNode (firstElem.textContains('(') || firstElem.textContains('{')) && (index > 0) && ((whiteSpaceBeforeDotOrSafeAccess?.elementType != WHITE_SPACE) || - (whiteSpaceBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !whiteSpaceBeforeDotOrSafeAccess.textContains('\n'))) + (whiteSpaceBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !whiteSpaceBeforeDotOrSafeAccess.textContains('\n'))) } if (without.isNotEmpty()) { - WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access after first call expression", node.startOffset, node) { + WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access after first call expression", + node.startOffset, node) { fixDotQualifiedExpression(listDot) } } From 591bdef3fe36d499db193c2b680d0b7a93a6d93e Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 28 Jun 2022 18:40:11 +0300 Subject: [PATCH 3/6] ## Whats Added: * add analytic fully-qualified * add test * correct warning message * run fix mode ## Issue (#1403) --- .../rules/chapter3/files/NewlinesRule.kt | 19 +++++++++---------- .../chapter3/files/NewlinesRuleWarnTest.kt | 2 +- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index 2a05e96ce3..fd38c5d714 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -159,15 +159,17 @@ class NewlinesRule(configRules: List) : DiktatRule( }.reversed() if (listDot.size > 3) { val without = listDot.filterIndexed { index, it -> - val whiteSpaceBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev + val nodeBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev val firstElem = it.firstChildNode - (firstElem.textContains('(') || firstElem.textContains('{')) && (index > 0) && ((whiteSpaceBeforeDotOrSafeAccess?.elementType != WHITE_SPACE) || - (whiteSpaceBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !whiteSpaceBeforeDotOrSafeAccess.textContains('\n'))) + val isTextContainsParenthesized = firstElem.textContains('(') || firstElem.textContains('{') + val isWhiteSpaceBeforeDotOrSafeAccessContainNewLine = nodeBeforeDotOrSafeAccess?.elementType != WHITE_SPACE || + (nodeBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !nodeBeforeDotOrSafeAccess.textContains('\n')) + isTextContainsParenthesized && (index > 0) && isWhiteSpaceBeforeDotOrSafeAccessContainNewLine } if (without.isNotEmpty()) { - WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access after first call expression", + WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "wrong split long `dot qualified expression` or `safe access expression`", node.startOffset, node) { - fixDotQualifiedExpression(listDot) + fixDotQualifiedExpression(without) } } } @@ -193,13 +195,10 @@ class NewlinesRule(configRules: List) : DiktatRule( * 2) If before first Dot or Safe Access node stay White Space node with \n - remove this node */ private fun fixDotQualifiedExpression(list: List) { - list.forEachIndexed { index, astNode -> + list.forEach { astNode -> val dotNode = astNode.getFirstChildWithType(DOT) ?: astNode.getFirstChildWithType(SAFE_ACCESS) val nodeBeforeDot = dotNode?.treePrev - val firstElem = astNode.firstChildNode - if (index > 0 && (firstElem.textContains('(') || firstElem.textContains('{'))) { - astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode) - } + astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode) } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt index 3085d0f6ac..3008ad44ee 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/files/NewlinesRuleWarnTest.kt @@ -26,7 +26,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) { mapOf("maxCallsInOneLine" to "1")) ) private val ruleId = "$DIKTAT_RULE_SET_ID:${NewlinesRule.NAME_ID}" - private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} should be split before second and other dot/safe access after first call expression" + private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} wrong split long `dot qualified expression` or `safe access expression`" private val shouldBreakAfter = "${WRONG_NEWLINES.warnText()} should break a line after and not before" private val shouldBreakBefore = "${WRONG_NEWLINES.warnText()} should break a line before and not after" private val functionalStyleWarn = "${WRONG_NEWLINES.warnText()} should follow functional style at" From 62a62a49f4a9df7b4f6061d26675c6e5d559b04e Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 28 Jun 2022 18:48:36 +0300 Subject: [PATCH 4/6] ## Whats Added: * add analytic fully-qualified * add test * correct warning message * run fix mode ## Issue (#1403) --- .../diktat/ruleset/rules/chapter3/files/NewlinesRule.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index fd38c5d714..442979a7cc 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -161,9 +161,9 @@ class NewlinesRule(configRules: List) : DiktatRule( val without = listDot.filterIndexed { index, it -> val nodeBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev val firstElem = it.firstChildNode - val isTextContainsParenthesized = firstElem.textContains('(') || firstElem.textContains('{') + val isTextContainsParenthesized = isTextContainParentheses(firstElem) val isWhiteSpaceBeforeDotOrSafeAccessContainNewLine = nodeBeforeDotOrSafeAccess?.elementType != WHITE_SPACE || - (nodeBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !nodeBeforeDotOrSafeAccess.textContains('\n')) + (nodeBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !nodeBeforeDotOrSafeAccess.textContains('\n')) isTextContainsParenthesized && (index > 0) && isWhiteSpaceBeforeDotOrSafeAccessContainNewLine } if (without.isNotEmpty()) { @@ -627,12 +627,14 @@ class NewlinesRule(configRules: List) : DiktatRule( } return if (dropLeadingProperties) { // fixme: we can't distinguish fully qualified names (like java.lang) from chain of property accesses (like list.size) for now - parentExpressionList?.dropWhile { !it.treeParent.textContains('(') && !it.treeParent.textContains('{') } + parentExpressionList?.dropWhile { !isTextContainParentheses(it.treeParent) } } else { parentExpressionList } } + private fun isTextContainParentheses(node: ASTNode): Boolean = node.textContains('(') || node.textContains('}') + private fun List.isNotValidCalls(node: ASTNode): Boolean { if (this.size == 1) { return false From 962afd2383c6c8e4b458b3395bf49ef1767cd694 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 28 Jun 2022 18:54:05 +0300 Subject: [PATCH 5/6] ## Whats Added: * add analytic fully-qualified * add test * correct warning message * run fix mode ## Issue (#1403) --- .../ruleset/rules/chapter3/files/NewlinesRule.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index 442979a7cc..dccbb31d67 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -161,10 +161,10 @@ class NewlinesRule(configRules: List) : DiktatRule( val without = listDot.filterIndexed { index, it -> val nodeBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev val firstElem = it.firstChildNode - val isTextContainsParenthesized = isTextContainParentheses(firstElem) - val isWhiteSpaceBeforeDotOrSafeAccessContainNewLine = nodeBeforeDotOrSafeAccess?.elementType != WHITE_SPACE || + val isTextContainsParenthesized = isTextContainsFunctionCall(firstElem) + val isNotWhiteSpaceBeforeDotOrSafeAccessContainNewLine = nodeBeforeDotOrSafeAccess?.elementType != WHITE_SPACE || (nodeBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !nodeBeforeDotOrSafeAccess.textContains('\n')) - isTextContainsParenthesized && (index > 0) && isWhiteSpaceBeforeDotOrSafeAccessContainNewLine + isTextContainsParenthesized && (index > 0) && isNotWhiteSpaceBeforeDotOrSafeAccessContainNewLine } if (without.isNotEmpty()) { WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "wrong split long `dot qualified expression` or `safe access expression`", @@ -627,13 +627,13 @@ class NewlinesRule(configRules: List) : DiktatRule( } return if (dropLeadingProperties) { // fixme: we can't distinguish fully qualified names (like java.lang) from chain of property accesses (like list.size) for now - parentExpressionList?.dropWhile { !isTextContainParentheses(it.treeParent) } + parentExpressionList?.dropWhile { !isTextContainsFunctionCall(it.treeParent) } } else { parentExpressionList } } - private fun isTextContainParentheses(node: ASTNode): Boolean = node.textContains('(') || node.textContains('}') + private fun isTextContainsFunctionCall(node: ASTNode): Boolean = node.textContains('(') || node.textContains('}') private fun List.isNotValidCalls(node: ASTNode): Boolean { if (this.size == 1) { From 688dc314a660dc9b2b09dbdfa2b72b3038373188 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 29 Jun 2022 11:32:11 +0300 Subject: [PATCH 6/6] ## Whats Added: * Add logic fully-qualified: not split `dot qualified expression` before first `call expression` * Add 1 fix test and 5 test examples * Correct warning message * run fix mode --- .../cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt index a4df99f96b..f85f885fd7 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/NewlinesRule.kt @@ -633,7 +633,7 @@ class NewlinesRule(configRules: List) : DiktatRule( } } - private fun isTextContainsFunctionCall(node: ASTNode): Boolean = node.textContains('(') || node.textContains('}') + private fun isTextContainsFunctionCall(node: ASTNode): Boolean = node.textContains('(') || node.textContains('{') private fun List.isNotValidCalls(node: ASTNode): Boolean { if (this.size == 1) {