Skip to content

Commit 48360cd

Browse files
authored
Bugfix/long line in string template (#1297)
### 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 ### Issue (#1243)
1 parent aa093cc commit 48360cd

19 files changed

+616
-299
lines changed

Diff for: diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt

+443-250
Large diffs are not rendered by default.

Diff for: diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,14 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength)
8888
fun `fix expression in condition`() {
8989
fixAndCompare("LongExpressionInConditionExpected.kt", "LongExpressionInConditionTest.kt", rulesConfigListLineLength)
9090
}
91+
92+
@Test
93+
fun `fix long Dot Qualified Expression`() {
94+
fixAndCompare("LongDotQualifiedExpressionExpected.kt", "LongDotQualifiedExpressionTest.kt", rulesConfigListLineLength)
95+
}
96+
97+
@Test
98+
fun `fix long value arguments list`() {
99+
fixAndCompare("LongValueArgumentsListExpected.kt", "LongValueArgumentsListTest.kt", rulesConfigListLineLength)
100+
}
91101
}

Diff for: diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthWarnTest.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class LineLengthWarnTest : LintTestBase(::LineLength) {
141141
| }
142142
|}
143143
""".trimMargin(),
144-
LintError(14, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 143", false),
145-
LintError(18, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 142", false)
144+
LintError(14, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 143", true),
145+
LintError(18, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 142", true)
146146
)
147147
}
148148

@@ -223,7 +223,8 @@ class LineLengthWarnTest : LintTestBase(::LineLength) {
223223
|}
224224
""".trimMargin(),
225225
LintError(8, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 130", false),
226-
LintError(9, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 123", false)
226+
LintError(9, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 123", true)
227+
227228
)
228229
}
229230

@@ -235,7 +236,7 @@ class LineLengthWarnTest : LintTestBase(::LineLength) {
235236
|@Query(value = "ASDAASDASDASDASDASDASDASDAASDASDASDASDASDASDASDAASDASDASDASDASDASD")
236237
|fun foo() = println("ASDAASDASDASDASDASDASDASDAASDASDASDASDASDASDASDAASDASDASDASDASDASD")
237238
""".trimMargin(),
238-
LintError(1, 1, ruleId, "${LONG_LINE.warnText()} max line length 40, but was 84", false),
239+
LintError(1, 1, ruleId, "${LONG_LINE.warnText()} max line length 40, but was 84", true),
239240
LintError(2, 1, ruleId, "${LONG_LINE.warnText()} max line length 40, but was 89", true),
240241
rulesConfigList = shortLineLength
241242
)

Diff for: diktat-rules/src/test/kotlin/org/cqfn/diktat/util/SuppressingTest.kt

-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
package org.cqfn.diktat.util
22

33
import org.cqfn.diktat.common.config.rules.RulesConfig
4-
import org.cqfn.diktat.ruleset.constants.Warnings.BACKTICKS_PROHIBITED
5-
import org.cqfn.diktat.ruleset.constants.Warnings.CLASS_NAME_INCORRECT
6-
import org.cqfn.diktat.ruleset.constants.Warnings.CONFUSING_IDENTIFIER_NAMING
7-
import org.cqfn.diktat.ruleset.constants.Warnings.CONSTANT_UPPERCASE
8-
import org.cqfn.diktat.ruleset.constants.Warnings.ENUM_VALUE
9-
import org.cqfn.diktat.ruleset.constants.Warnings.EXCEPTION_SUFFIX
10-
import org.cqfn.diktat.ruleset.constants.Warnings.FUNCTION_BOOLEAN_PREFIX
11-
import org.cqfn.diktat.ruleset.constants.Warnings.GENERIC_NAME
124
import org.cqfn.diktat.ruleset.constants.Warnings.IDENTIFIER_LENGTH
13-
import org.cqfn.diktat.ruleset.constants.Warnings.OBJECT_NAME_INCORRECT
14-
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_HAS_PREFIX
15-
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT
16-
import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT_FORMAT
175
import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID
186
import org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming
197

208
import com.pinterest.ktlint.core.LintError
21-
import generated.WarningNames
22-
import org.junit.jupiter.api.Tag
23-
import org.junit.jupiter.api.Tags
249
import org.junit.jupiter.api.Test
2510

2611
class SuppressingTest : LintTestBase(::IdentifierNaming) {

Diff for: diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ fun foo() {
3939
val variable =
4040
Methoooooooooooooooooooooooooood() ?: "some loooooong string"
4141

42-
val variable = Methooooood() ?: "some" +
43-
" looong string"
42+
val variable = Methooooood()
43+
?: "some looong string"
4444

4545
var headerKdoc = firstCodeNode.prevSibling {
4646
it.elementType == KDOC
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.paragraph3.long_line
2+
3+
val G =
4+
ThisIsVeryyyyLooooonNameDooootQualifiedExpressioWithoutDot.lalalala.lalalal
5+
6+
val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot
7+
.Qualified.Expression
8+
9+
val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot
10+
?.Qualified?.Expression
11+
12+
val C = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!!
13+
.Dot!!.Qualified!!.Expression
14+
15+
val D = This.Is.Veeeeryyyyyyy.Loooooong.Dot
16+
.Qualified.Expression
17+
18+
val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot
19+
?.Qualified?.Expression
20+
21+
val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!!
22+
.Dot!!.Qualified!!.Expression
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test.paragraph3.long_line
2+
3+
val G = ThisIsVeryyyyLooooonNameDooootQualifiedExpressioWithoutDot.lalalala.lalalal
4+
5+
val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression
6+
7+
val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot?.Qualified?.Expression
8+
9+
val C = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!!.Dot!!.Qualified!!.Expression
10+
11+
val D = This.Is.Veeeeryyyyyyy.Loooooong.Dot
12+
.Qualified.Expression
13+
14+
val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot
15+
?.Qualified?.Expression
16+
17+
val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!!
18+
.Dot!!.Qualified!!.Expression

Diff for: diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ package org.cqfn.diktat.resources.paragraph3.longline
33
class veryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong {
44
// looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg
55
//looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg
6-
val s = "d" +
7-
" s d d d d ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
6+
val s =
7+
"d s d d d d ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
88
}

Diff for: diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ fun foo() {
4545
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
4646
ElementType.IF -> handleIfElse(node)
4747
ElementType.EOL_COMMENT, ElementType.BLOCK_COMMENT -> handleEolAndBlockComments(node, configuration)
48-
ElementType.KDOC -> handleKdocComments(node, configuration)
48+
ElementType.KDOC ->
49+
handleKdocComments(node, configuration)
4950
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
5051
else -> {
5152
// this is a generated else block
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package test.paragraph3.long_line
22

3-
@Query(value = "select * from test inner join" +
4-
" test_execution on test.id = test_execution.test_id and test_execution.st", nativeQuery = true)
3+
@Query(
4+
value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.st",
5+
nativeQuery = true
6+
)
57
fun retrieveBatches(limit: Int, offset: Int, executionId: Long): Some
68

7-
@Query(value = "select * from test inner join" +
8-
" test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", nativeQuery = true)
9+
@Query(
10+
value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2",
11+
nativeQuery = true
12+
)
913
fun some(limit: Int, offset: Int, executionId: Long): List<Test>
1014

11-
@Query(value = "select * from test inner joi", nativeQuery = true)
15+
@Query(value = "select * from test inner joi",
16+
nativeQuery = true)
1217
fun test(limit: Int, offset: Int, executionId: Long): List<Test>
1318

14-
@Query(value = "select * from test inner joibbb", nativeQuery = true)
19+
@Query(value = "select * from test inner joibbb",
20+
nativeQuery = true)
1521
fun cornerCase(limit: Int, offset: Int, executionId: Long): List<Test>
1622

17-
@Query(value = "select * from test inner join" +
18-
" test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", nativeQuery = true)
23+
@Query(
24+
value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2",
25+
nativeQuery = true
26+
)
1927
fun some(limit: Int, offset: Int, executionId: Long) =
2028
println("testtesttesttesttesttesttesttesttesttesttesttest")
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package test.paragraph3.long_line
22

3-
fun foo() =
4-
println("fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd")
3+
fun foo() = println(
4+
"fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd"
5+
)
56

6-
fun foo () { println("fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd") }
7+
fun foo () { println(
8+
"fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd"
9+
) }

Diff for: diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueExpected.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ fun foo() {
77
fun foo() {
88
val veryLoooooooooooooooooongNamesList =
99
listOf<String>("Jack", "Nick")
10-
veryLoooooooooooooooooongNamesList.forEach {
11-
name ->
10+
veryLoooooooooooooooooongNamesList
11+
.forEach { name ->
1212
if (name == "Nick") {
13-
veryLoooooooooooooooooongNamesList.map { val str = "This string shouldn't be split"}
13+
veryLoooooooooooooooooongNamesList
14+
.map { val str = "This string shouldn't be split"}
1415
name.map { val str =
1516
"This string should be split" }
1617
}
@@ -32,10 +33,11 @@ fun foo() {
3233
val longStringExpression = "First part" +
3334
"second Part"
3435

35-
val longStringExpression = "First" + "second Part"
36+
val longStringExpression = "First" +
37+
"second Part"
3638

37-
val longStringExpression = "First very long" +
38-
" part" + "second Part"
39+
val longStringExpression =
40+
"First very long part" + "second Part"
3941

4042
val longStringExpression2 =
4143
"String starts at the line len limit"
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package test.paragraph3.long_line
22

3-
val LongWithVar2 = "${s + "a"} is a string"
3+
val LongWithVar2 =
4+
"${s + "a"} is a string"

Diff for: diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt

+15-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ object Observables {
33
val someCode = 15
44
// Some
55
// looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line
6-
@Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in" +
7-
" future RxKotlin release.",
8-
replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, combineFunction)", "io.reactivex.Observable"),
9-
level = DeprecationLevel.WARNING)
6+
@Deprecated(
7+
"New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.",
8+
replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, combineFunction)",
9+
"io.reactivex.Observable"),
10+
level = DeprecationLevel.WARNING
11+
)
1012
@CheckReturnValue
1113
@SchedulerSupport(SchedulerSupport.NONE)
1214
inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, R : Any> combineLatest() {}
@@ -15,9 +17,12 @@ val someCode = 15
1517
class Foo() {
1618

1719
fun Fuu() {
18-
logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvkdjdfvd" else "dfjvndkjnbvif" +
19-
response.message}")
20-
logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvsdcsdcscskdjdfvd" else "dfjvndsdcsdcsdcskjnbvif" + response.message}")
20+
logger.log(
21+
"<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvkdjdfvd" else "dfjvndkjnbvif" + response.message}"
22+
)
23+
logger.log(
24+
"<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvsdcsdcscskdjdfvd" else "dfjvndsdcsdcsdcskjnbvif" + response.message}"
25+
)
2126
}
2227

2328
val q = """
@@ -46,4 +51,7 @@ class Foo() {
4651
}
4752
""".trimIndent()
4853
}
54+
55+
val stringName = "This is long string template with binary expression. test should be up in level binary" +
56+
" expression and cannot split in operation reference and should be split this long string template" + "this string should be after operated reference"
4957
}

Diff for: diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateTest.kt

+2
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ class Foo() {
3838
}
3939
""".trimIndent()
4040
}
41+
42+
val stringName = "This is long string template with binary expression. test should be up in level binary expression and cannot split in operation reference and should be split this long string template" + "this string should be after operated reference"
4143
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test.paragraph3.long_line
2+
3+
val firstArgument = 1
4+
val secondArgument = 2
5+
val thirdArgument = 3
6+
val fourthArgument = 4
7+
val fifthArguments = 5
8+
val sixthArguments = 6
9+
val seventhArguments = 7
10+
val eighthArguments = 8
11+
12+
// Many arguments in function
13+
val result1 = ManyParamInFunction(firstArgument,
14+
secondArgument, thirdArgument, fourthArgument,
15+
fifthArguments, sixthArguments, seventhArguments,
16+
eighthArguments)
17+
18+
//
19+
val result2 = veryLongNameFun(firstArgument,
20+
secondArgument)
21+
22+
// first argument cannot to be able to stay in
23+
// the first line
24+
val result3 = veryLongNameInFirstParam(
25+
firstArgument, secondArgument, thirdArgument
26+
)
27+
28+
// first argument cannot to be able to stay in
29+
// the first line
30+
val result4 = veryLongNameInFirstParam(
31+
firstArgument
32+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.paragraph3.long_line
2+
3+
val firstArgument = 1
4+
val secondArgument = 2
5+
val thirdArgument = 3
6+
val fourthArgument = 4
7+
val fifthArguments = 5
8+
val sixthArguments = 6
9+
val seventhArguments = 7
10+
val eighthArguments = 8
11+
12+
// Many arguments in function
13+
val result1 = ManyParamInFunction(firstArgument, secondArgument, thirdArgument, fourthArgument, fifthArguments, sixthArguments, seventhArguments, eighthArguments)
14+
15+
//
16+
val result2 = veryLongNameFun(firstArgument, secondArgument)
17+
18+
// first argument cannot to be able to stay in the first line
19+
val result3 = veryLongNameInFirstParam(firstArgument, secondArgument, thirdArgument)
20+
21+
// first argument cannot to be able to stay in the first line
22+
val result4 = veryLongNameInFirstParam(firstArgument)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.cqfn.diktat
2+
3+
val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.cqfn.diktat
2+
3+
/**
4+
* @return
5+
*/
6+
val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression

0 commit comments

Comments
 (0)