From 9e12cdf8ff9bc4870c0fe673a9d01f915fbd4fb5 Mon Sep 17 00:00:00 2001 From: Alexey Votintsev <79669665+Arrgentum@users.noreply.github.com> Date: Tue, 21 Jun 2022 15:22:57 +0300 Subject: [PATCH] Bugfix/fix cause long line (#1373) ### Whats added: * correct logic check White Space around Operator References in WhiteSpaceRule.kt * added smoke test ### Issue(#1339) --- .../rules/chapter3/files/WhiteSpaceRule.kt | 33 ++- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 6 + .../ManyLineTransformInLongLineExpected.kt | 11 + .../kotlin/ManyLineTransformInLongLineTest.kt | 213 ++++++++++++++++++ 4 files changed, 260 insertions(+), 3 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/WhiteSpaceRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/WhiteSpaceRule.kt index 378eee58b1..4a783d87e0 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/WhiteSpaceRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/WhiteSpaceRule.kt @@ -1,9 +1,16 @@ package org.cqfn.diktat.ruleset.rules.chapter3.files import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.common.config.rules.getRuleConfig +import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE +import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_WHITESPACE import org.cqfn.diktat.ruleset.rules.DiktatRule +import org.cqfn.diktat.ruleset.rules.chapter3.LineLength import org.cqfn.diktat.ruleset.rules.chapter6.classes.CompactInitialization +import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace +import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset +import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType import org.cqfn.diktat.ruleset.utils.hasChildOfType import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY @@ -23,8 +30,10 @@ import com.pinterest.ktlint.core.ast.ElementType.CONSTRUCTOR_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.DOT import com.pinterest.ktlint.core.ast.ElementType.DO_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.ELSE_KEYWORD +import com.pinterest.ktlint.core.ast.ElementType.ELVIS import com.pinterest.ktlint.core.ast.ElementType.EQ import com.pinterest.ktlint.core.ast.ElementType.EXCLEXCL +import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.FINALLY_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.FOR_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.FUN @@ -96,8 +105,14 @@ import org.slf4j.LoggerFactory class WhiteSpaceRule(configRules: List) : DiktatRule( NAME_ID, configRules, - listOf(WRONG_WHITESPACE) + listOf(WRONG_WHITESPACE, LONG_LINE, WRONG_INDENTATION) ) { + private val configuration by lazy { + LineLength.LineLengthConfiguration( + configRules.getRuleConfig(LONG_LINE)?.configuration ?: emptyMap() + ) + } + private lateinit var positionByOffset: (Int) -> Pair @Suppress("ComplexMethod") override fun logic(node: ASTNode) { when (node.elementType) { @@ -274,7 +289,6 @@ class WhiteSpaceRule(configRules: List) : DiktatRule( if (node.elementType == OPERATION_REFERENCE && node.treeParent.elementType.let { it == BINARY_EXPRESSION || it == POSTFIX_EXPRESSION || it == PROPERTY } || node.elementType != OPERATION_REFERENCE) { val requiredNumSpaces = if (operatorNode.elementType in operatorsWithNoWhitespace) 0 else 1 - handleToken(node, requiredNumSpaces, requiredNumSpaces) } } @@ -366,15 +380,28 @@ class WhiteSpaceRule(configRules: List) : DiktatRule( } } + @Suppress("UnsafeCallOnNullableType") + private fun ASTNode.isNeedNewLineInOperatorReferences(): Boolean { + positionByOffset = this.findParentNodeWithSpecificType(FILE)!!.calculateLineColByOffset() + val offset = positionByOffset(this.startOffset).second + return offset + this.text.length >= configuration.lineLength + } + + @Suppress("UnsafeCallOnNullableType") private fun ASTNode.fixSpaceAround(requiredSpacesBefore: Int?, requiredSpacesAfter: Int?) { if (requiredSpacesBefore == 1) { selfOrParentsTreePrev()?.let { if (it.elementType == WHITE_SPACE) it.treePrev else it }?.leaveSingleWhiteSpace() + if (this.isNeedNewLineInOperatorReferences() && this.firstChildNode.elementType == ELVIS) { + this.treePrev.let { this.treeParent.appendNewlineMergingWhiteSpace(it, it) } + } } else if (requiredSpacesBefore == 0) { selfOrParentsTreePrev()?.removeIfWhiteSpace() } - if (requiredSpacesAfter == 1) { leaveSingleWhiteSpace() + if (this.isNeedNewLineInOperatorReferences() && this.firstChildNode.elementType != ELVIS) { + this.treeNext.let { this.treeParent.appendNewlineMergingWhiteSpace(it, it) } + } } else if (requiredSpacesAfter == 0) { // for `!!` and possibly other postfix expressions treeNext can be null (treeNext ?: treeParent.treeNext).removeIfWhiteSpace() diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index 1060bfe248..d92fd08eb2 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -342,6 +342,12 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", } } + @Test + @Tag("DiktatRuleSetProvider") + fun `fix can cause long line`() { + fixAndCompareSmokeTest("ManyLineTransformInLongLineExpected.kt", "ManyLineTransformInLongLineTest.kt") + } + companion object { private const val DEFAULT_CONFIG_PATH = "../diktat-analysis.yml" private val unfixedLintErrors: MutableList = mutableListOf() diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt new file mode 100644 index 0000000000..bfde8a2237 --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineExpected.kt @@ -0,0 +1,11 @@ +package org.cqfn.diktat + +fun foo() { + (1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 + ?: 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 ?: 40 or 41 or 42 or 43 or 44 or 45 or 46 or 47 or 48 or 49 or 50 or 51 or 52 or 53 + 54 or 55 or 56 or 57 or 58 or 59 + ?: 60 or 61 or 62 or 63 or 64 or 65 or 66 - 67 or 68 or 69 or 70 or 1 + 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or 12 or 13 or 14 or 15 or 16 or 17 or 18 + 19 - + 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 || 42 or 43 or 44 or 45 or 46 or 47 && 48 or 49 || + 50 or 51 or 52 or 53 or 54 or 55 or 56 or 57 or 58 or 59 or 60 or 61 or 62 or 63 or 64 or 65 or 66 or 67 or 68 or 69 or 70 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 or 10 or 11 or + 12 or 13 or 14 or 15 or 16 or 17 or 18 or 19 or 20 or 21 or 22 or 23 or 24 or 25 or 26 or 27 or 28 or 29 or 30 or 31 or 32 or 33 or 34 or 35 or 36 or 37 or 38 or 39 or 40 or 41 or + 42 or 43 or 44 or 45 or 46 or 47 or 48 or 49 or 50 or 51 or 52 or 53 or 54 or 55 or 56 or 57 or 58 or 59 or 60 or 61 or 62 or 63 or 64 or 65 or 66 or 67 or 68 or 69 or 70) +} diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt new file mode 100644 index 0000000000..d5b4f26d1b --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/ManyLineTransformInLongLineTest.kt @@ -0,0 +1,213 @@ +package org.cqfn.diktat + +fun foo(){ + (1 + or 2 + or 3 + or 4 + or 5 + or 6 + or 7 + or 8 + or 9 + or 10 + or 11 + or 12 + or 13 + or 14 + or 15 + or 16 + or 17 + or 18 + or 19 + or 20 + or 21 + or 22 + or 23 + or 24 + or 25 + or 26 + or 27 + or 28 + or 29 + or 30 + or 31 + ?: 32 + or 33 + or 34 + or 35 + or 36 + or 37 + or 38 + or 39 ?: 40 + or 41 + or 42 + or 43 + or 44 + or 45 + or 46 + or 47 + or 48 + or 49 + or 50 + or 51 + or 52 + or 53 + + 54 + or 55 + or 56 + or 57 + or 58 + or 59 + ?: 60 + or 61 + or 62 + or 63 + or 64 + or 65 + or 66 + - 67 + or 68 + or 69 + or 70 + or 1 + + 2 + or 3 + or 4 + or 5 + or 6 + or 7 + or 8 + or 9 + or 10 + or 11 + or 12 + or 13 + or 14 + or 15 + or 16 + or 17 + or 18 + + 19 + - 20 + or 21 + or 22 + or 23 + or 24 + or 25 + or 26 + or 27 + or 28 + or 29 + or 30 + or 31 + or 32 + or 33 + or 34 + or 35 + or 36 + or 37 + or 38 + or 39 + or 40 + or 41 + || 42 + or 43 + or 44 + or 45 + or 46 + or 47 + && 48 + or 49 + || 50 + or 51 + or 52 + or 53 + or 54 + or 55 + or 56 + or 57 + or 58 + or 59 + or 60 + or 61 + or 62 + or 63 + or 64 + or 65 + or 66 + or 67 + or 68 + or 69 + or 70 + or 1 + or 2 + or 3 + or 4 + or 5 + or 6 + or 7 + or 8 + or 9 + or 10 + or 11 + or 12 + or 13 + or 14 + or 15 + or 16 + or 17 + or 18 + or 19 + or 20 + or 21 + or 22 + or 23 + or 24 + or 25 + or 26 + or 27 + or 28 + or 29 + or 30 + or 31 + or 32 + or 33 + or 34 + or 35 + or 36 + or 37 + or 38 + or 39 + or 40 + or 41 + or 42 + or 43 + or 44 + or 45 + or 46 + or 47 + or 48 + or 49 + or 50 + or 51 + or 52 + or 53 + or 54 + or 55 + or 56 + or 57 + or 58 + or 59 + or 60 + or 61 + or 62 + or 63 + or 64 + or 65 + or 66 + or 67 + or 68 + or 69 + or 70) +}