Skip to content

Commit

Permalink
Fix logic "BinaryExpression" in "LineLength" Rule (#1292)
Browse files Browse the repository at this point in the history
### What's done:
* Fixed logic and tests for binary expression in rule "LineLength"
* Added tests for LineLength rule

(#1292)
Co-authored-by: Andrey Kuleshov <andrewkuleshov7@gmail.com>
  • Loading branch information
Arrgentum committed May 16, 2022
1 parent 6e60358 commit 4a13e39
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 271 deletions.
Expand Up @@ -10,6 +10,7 @@ import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_PREFIX
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_INCORRECT_SYMBOLS
import org.cqfn.diktat.ruleset.constants.Warnings.PACKAGE_NAME_MISSING
import org.cqfn.diktat.ruleset.rules.DiktatRule

import org.cqfn.diktat.ruleset.utils.*

import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -7,6 +7,10 @@ import org.cqfn.diktat.util.FixTestBase
import org.junit.jupiter.api.Test

class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) {
private val rulesConfigListLongLineLength: List<RulesConfig> = listOf(
RulesConfig(LONG_LINE.name, true,
mapOf("lineLength" to "175"))
)
private val rulesConfigListDefaultLineLength: List<RulesConfig> = listOf(
RulesConfig(LONG_LINE.name, true,
mapOf("lineLength" to "120"))
Expand Down Expand Up @@ -74,4 +78,14 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength)
fun `should fix annotation`() {
fixAndCompare("LongLineAnnotationExpected.kt", "LongLineAnnotationTest.kt", rulesConfigListLineLength)
}

@Test
fun `fix condition in small function with long length`() {
fixAndCompare("LongConditionInSmallFunctionExpected.kt", "LongConditionInSmallFunctionTest.kt", rulesConfigListLongLineLength)
}

@Test
fun `fix expression in condition`() {
fixAndCompare("LongExpressionInConditionExpected.kt", "LongExpressionInConditionTest.kt", rulesConfigListLineLength)
}
}
@@ -1,35 +1,48 @@
package test.paragraph3.long_line

fun foo() {
val veryLongExpression = Methoooooooooooooooood() +
12345
val veryLongExpression =
Methoooooooooooooooood() + 12345

val veryLongExpression = Methoooooooooooooooood() ?:
null
val veryLongExpression =
Methoooooooooooooooood() ?: null

val veryLongExpression = a.Methooooood() +
b.field

val variable = someField.filter { it.elementType == KDOC }
val variable = someField.filter {
it.elementType == KDOC
}

// limit at the left side
val variable = a?.filter { it.elementType == KDOC } ?:
null
val variable = a?.filter {
it.elementType == KDOC
} ?: null

// limit at the right side
val variable = bar?.filter { it.b == c } ?:
null
val variable = bar?.filter { it.b == c }
?: null

// limit at the operation reference
val variable = field?.filter { bar == foo } ?:
null
val variable = field?.filter { bar == foo }
?: null

val variable = Methoooooooooooooooooooooooooood() ?:
"some loooooong string"
val variable = field?.filter { bar == foo }
?: null

val variable = Methooood() * 2 + 12 + field
?: 123 + Methood().linelength

val variable = Methooood() * 2 + 12 + field
?: 123 + Methood().linelength

val variable =
Methoooooooooooooooooooooooooood() ?: "some loooooong string"

val variable = Methooooood() ?: "some" +
" looong string"

var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?:
if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null
var headerKdoc = firstCodeNode.prevSibling {
it.elementType == KDOC
} ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null
}
Expand Up @@ -18,6 +18,12 @@ fun foo() {
// limit at the operation reference
val variable = field?.filter { bar == foo } ?: null

val variable = field?.filter { bar == foo }?: null

val variable = Methooood() * 2 + 12 + field ?: 123 + Methood().linelength

val variable = Methooood() * 2 + 12 + field?: 123 + Methood().linelength

val variable = Methoooooooooooooooooooooooooood() ?: "some loooooong string"

val variable = Methooooood() ?: "some looong string"
Expand Down
@@ -0,0 +1,5 @@
package test.paragraph3.long_line

private fun isContainingRequiredPartOfCode(text: String): Boolean =
text.contains("val ", true) || text.contains("var ", true) || text.contains("=", true) || (text.contains("{", true) &&
text.substringAfter("{").contains("}", true))
@@ -0,0 +1,4 @@
package test.paragraph3.long_line

private fun isContainingRequiredPartOfCode(text: String): Boolean =
text.contains("val ", true) || text.contains("var ", true) || text.contains("=", true) || (text.contains("{", true) && text.substringAfter("{").contains("}", true))
@@ -0,0 +1,18 @@
package test.paragraph3.long_line

fun foo() {
val veryLooongStringName = "ASDFGHJKL"
val veryLooooooongConstIntName1 = 12345
val veryLooooooongConstIntName2 = 54321
var carry = 1
if (veryLooooooongConstIntName1 >
veryLooooooongConstIntName2) {
carry++
} else if (veryLooooooongConstIntName2 >
123 * 12 && veryLooongStringName != "asd") {
carry+=2
} else if (1234 + 1235 + 1236 + 1237 + 1238 >
124 * 12) {
carry+=3
}
}
@@ -0,0 +1,15 @@
package test.paragraph3.long_line

fun foo() {
val veryLooongStringName = "ASDFGHJKL"
val veryLooooooongConstIntName1 = 12345
val veryLooooooongConstIntName2 = 54321
var carry = 1
if (veryLooooooongConstIntName1 > veryLooooooongConstIntName2) {
carry++
} else if (veryLooooooongConstIntName2 > 123 * 12 && veryLooongStringName != "asd") {
carry+=2
} else if (1234 + 1235 + 1236 + 1237 + 1238 > 124 * 12) {
carry+=3
}
}
Expand Up @@ -3,8 +3,13 @@
*/
class ImplicitBackingPropertyRuleTest(configRules: List<RulesConfig>) {
private fun validateAccessors(node: ASTNode, propsWithBackSymbol: List<String>) {
val accessors = node.findAllDescendantsWithSpecificType(PROPERTY_ACCESSOR).filter { it.hasChildOfType(BLOCK) } // Comment, which shouldn't be moved
accessors.filter { it.hasChildOfType(GET_KEYWORD) }.forEach { handleGetAccessors(it, node, propsWithBackSymbol) }
accessors.filter { it.hasChildOfType(SET_KEYWORD) }.forEach { handleSetAccessors(it, node, propsWithBackSymbol) }
val accessors =
node.findAllDescendantsWithSpecificType(PROPERTY_ACCESSOR).filter { it.hasChildOfType(BLOCK) } // Comment, which shouldn't be moved
accessors.filter { it.hasChildOfType(GET_KEYWORD) }.forEach {
handleGetAccessors(it, node, propsWithBackSymbol)
}
accessors.filter { it.hasChildOfType(SET_KEYWORD) }.forEach {
handleSetAccessors(it, node, propsWithBackSymbol)
}
}
}
@@ -1,6 +1,8 @@
package test.paragraph3.long_line

fun foo() {
if (( x > 4365873654863745683)||
y<238479283749238&&!isFoo()){}
if (( x > 4365873654863745683) ||
y<238479283749238 && !isFoo()){}
if (q.text == "dc" && !IsFoo() ||
Expand All @@ -9,7 +11,8 @@ fun foo() {
x > 238479283749238 && y < 238479283749238 || g == "text"){}
if (d == "very long text" &&
gh == "very long text" || x > 238479283749238 || y< 238479283749238){}
if (x == 2384792837492387498728947289472987492){}
if (x ==
2384792837492387498728947289472987492){}
if (x == 972938473924535278492792738497){}
if (x == "veery long text to split" ||
x == "es" || y == 123 || b > 12 && jh==234 || h==54){}
Expand Down
@@ -1,6 +1,7 @@
package test.paragraph3.long_line

fun foo() {
if (( x > 4365873654863745683)||y<238479283749238&&!isFoo()){}
if (( x > 4365873654863745683) || y<238479283749238 && !isFoo()){}
if (q.text == "dc" && !IsFoo() || x > 238479283749238 && y < 238479283749238 || g == "text"){}
if (q.text == "dc" && !IsFoo() || x > 238479283749238 && y < 238479283749238 || g == "text"){}
Expand Down
Expand Up @@ -5,39 +5,44 @@ fun foo() {
" should be split"

fun foo() {
val veryLoooooooooooooooooongNamesList = listOf<String>("Jack", "Nick")
veryLoooooooooooooooooongNamesList.forEach { name ->
val veryLoooooooooooooooooongNamesList =
listOf<String>("Jack", "Nick")
veryLoooooooooooooooooongNamesList.forEach {
name ->
if (name == "Nick") {
veryLoooooooooooooooooongNamesList.map { val str = "This string shouldn't be split"}
name.map { val str = "This" +
" string should be split" }
name.map { val str =
"This string should be split" }
}

}
}

val longIntExpression = 12345 + 12345 +
12345 + 12345

val longIntExpression = (12345 + 12345 +
12345 + 12345)

val longIntExpression = (12345) + (12345) +
(12345) + (12345)

val LongWithVar2 = "very long" +
" woooooordsdcsdcsdcsdc $variable"

val longStringExpression = "First part" +
"second Part"
"second Part"

val longStringExpression = "First" + "second Part"

val longStringExpression = "First very long" +
" part" + "second Part"

val longStringExpression2 = "String starts at the line len limit"
val longStringExpression2 =
"String starts at the line len limit"

val veryLooooooooooooooooooooooooooooooongVal = "text"

val longIntExpression = (12345 + 12345 +
12345 + 12345)

val longIntExpression = (12345) + (12345) +
(12345) + (12345)
val veryLooooooooooooooooooooooooooooooongVal =
"text"

val veryLongExpression = Method() + 12345 +
baaar()
Expand Down
Expand Up @@ -15,6 +15,10 @@ fun foo() {

val longIntExpression = 12345 + 12345 + 12345 + 12345

val longIntExpression = (12345 + 12345 + 12345 + 12345)

val longIntExpression = (12345) + (12345) + (12345) + (12345)

val LongWithVar2 = "very long woooooordsdcsdcsdcsdc $variable"

val longStringExpression = "First part" + "second Part"
Expand All @@ -27,10 +31,6 @@ fun foo() {

val veryLooooooooooooooooooooooooooooooongVal = "text"

val longIntExpression = (12345 + 12345 + 12345 + 12345)

val longIntExpression = (12345) + (12345) + (12345) + (12345)

val veryLongExpression = Method() + 12345 + baaar()

val veryLongExpression = Method() + baaar() + 12345
Expand Down
Expand Up @@ -15,12 +15,14 @@ val someCode = 15
class Foo() {

fun Fuu() {
logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvkdjdfvd" else "dfjvndkjnbvif" + response.message}")
logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvkdjdfvd" else "dfjvndkjnbvif" +
response.message}")
logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvsdcsdcscskdjdfvd" else "dfjvndsdcsdcsdcskjnbvif" + response.message}")
}

val q = """
<--${respodcnsee.ccode}${if (response.mesdscsage.isEmpty()) "skfnvkdjeeeeeee" else "dfjvndksdcjnbvif" + response.mecsssdcage}
<--${respodcnsee.ccode}${if (response.mesdscsage.isEmpty()) "skfnvkdjeeeeeee" else "dfjvndksdcjnbvif" +
response.mecsssdcage}
""".trimIndent()

val w = """
Expand All @@ -31,15 +33,16 @@ class Foo() {

val e = """
another line
<--${respodcnse.ccode}${if (response.mesdscsage.isEmpty()) "skfnvkdjdsdcfvd" else "dfjvndksdcjnbvif" + response.mecsssdcage}
<--${respodcnse.ccode}${if (response.mesdscsage.isEmpty()) "skfnvkdjdsdcfvd" else "dfjvndksdcjnbvif" +
response.mecsssdcage}
""".trimIndent()

fun foo() {
val q = """
re
${
if (( x > "436587365486374568343658736548637456834365873654863745683436587365486374568343658736548637456834365873654863745683") ||
y<238479283749238 && !isFoo()){}
if (( x >
"436587365486374568343658736548637456834365873654863745683436587365486374568343658736548637456834365873654863745683") || y<238479283749238 && !isFoo()){}
}
""".trimIndent()
}
Expand Down

0 comments on commit 4a13e39

Please sign in to comment.