Skip to content

Commit 0502a2b

Browse files
tKepaul-dingemans
andauthored
Fix unwanted whitespace between super class constructor and its argument list (#2630)
Co-authored-by: Paul Dingemans <[email protected]>
1 parent 1b690a6 commit 0502a2b

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRule.kt

+15
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,21 @@ public class ClassSignatureRule :
584584
}
585585
}
586586

587+
// Disallow:
588+
// class Foo : Bar<String> ("foobar")
589+
superTypes
590+
.filter { it.elementType == SUPER_TYPE_CALL_ENTRY }
591+
.forEach { superTypeCallEntry ->
592+
superTypeCallEntry
593+
.findChildByType(WHITE_SPACE)
594+
?.let { whitespace ->
595+
emit(whitespace.startOffset, "No whitespace expected", true)
596+
if (autoCorrect) {
597+
whitespace.treeParent.removeChild(whitespace)
598+
}
599+
}
600+
}
601+
587602
return whiteSpaceCorrection
588603
}
589604

ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRule.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.LPAR
99
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PRIMARY_CONSTRUCTOR
1010
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR
1111
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_KEYWORD
12+
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST
1213
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT_LIST
1314
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
1415
import com.pinterest.ktlint.rule.engine.core.api.RuleId
@@ -46,7 +47,8 @@ public class SpacingAroundParensRule : StandardRule("paren-spacing") {
4647
node.treeParent?.treeParent?.elementType != FUNCTION_TYPE ||
4748
// Super keyword needs special-casing
4849
prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD ||
49-
prevLeaf.prevLeaf()?.treeParent?.elementType == PRIMARY_CONSTRUCTOR
50+
prevLeaf.prevLeaf()?.treeParent?.elementType == PRIMARY_CONSTRUCTOR ||
51+
prevLeaf.prevLeaf()?.treeParent?.elementType == TYPE_ARGUMENT_LIST
5052
) &&
5153
(
5254
node.treeParent?.elementType == VALUE_PARAMETER_LIST ||

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ClassSignatureRuleTest.kt

+49
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,55 @@ class ClassSignatureRuleTest {
17341734
.hasNoLintViolations()
17351735
}
17361736

1737+
@Nested
1738+
inner class `Issue 2630 - White space in super type call entry` {
1739+
@Test
1740+
fun `Issue 2630 - Given a super type call entry without type argument list`() {
1741+
val code =
1742+
"""
1743+
class Foo1 : Bar("foobar")
1744+
class Foo2 : Bar ("foobar")
1745+
class Foo3 : Bar
1746+
("foobar")
1747+
""".trimIndent()
1748+
val formattedCode =
1749+
"""
1750+
class Foo1 : Bar("foobar")
1751+
class Foo2 : Bar("foobar")
1752+
class Foo3 : Bar("foobar")
1753+
""".trimIndent()
1754+
classSignatureWrappingRuleAssertThat(code)
1755+
.hasLintViolations(
1756+
LintViolation(2, 17, "No whitespace expected"),
1757+
LintViolation(3, 14, "Super type should start on a newline"),
1758+
LintViolation(3, 17, "No whitespace expected"),
1759+
).isFormattedAs(formattedCode)
1760+
}
1761+
1762+
@Test
1763+
fun `Issue 2630 - Given a super type call entry with type argument list`() {
1764+
val code =
1765+
"""
1766+
class Foo1 : Bar<String>("foobar")
1767+
class Foo2 : Bar<String> ("foobar")
1768+
class Foo3 : Bar<String>
1769+
("foobar")
1770+
""".trimIndent()
1771+
val formattedCode =
1772+
"""
1773+
class Foo1 : Bar<String>("foobar")
1774+
class Foo2 : Bar<String>("foobar")
1775+
class Foo3 : Bar<String>("foobar")
1776+
""".trimIndent()
1777+
classSignatureWrappingRuleAssertThat(code)
1778+
.hasLintViolations(
1779+
LintViolation(2, 25, "No whitespace expected"),
1780+
LintViolation(3, 14, "Super type should start on a newline"),
1781+
LintViolation(3, 25, "No whitespace expected"),
1782+
).isFormattedAs(formattedCode)
1783+
}
1784+
}
1785+
17371786
private companion object {
17381787
const val UNEXPECTED_SPACES = " "
17391788
const val NO_SPACE = ""

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRuleTest.kt

+34
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@ class SpacingAroundParensRuleTest {
2828
.isFormattedAs(formattedCode)
2929
}
3030

31+
@Test
32+
fun `Given class with superclass constructor call`() {
33+
val code =
34+
"""
35+
open class Bar(param: String)
36+
class Foo : Bar ("test")
37+
""".trimIndent()
38+
val formattedCode =
39+
"""
40+
open class Bar(param: String)
41+
class Foo : Bar("test")
42+
""".trimIndent()
43+
spacingAroundParensRuleAssertThat(code)
44+
.hasLintViolation(2, 16, "Unexpected spacing before \"(\"")
45+
.isFormattedAs(formattedCode)
46+
}
47+
48+
@Test
49+
fun `Given class with superclass constructor call with type parameter`() {
50+
val code =
51+
"""
52+
open class Bar<T>(param: T)
53+
class Foo : Bar<String> ("test")
54+
""".trimIndent()
55+
val formattedCode =
56+
"""
57+
open class Bar<T>(param: T)
58+
class Foo : Bar<String>("test")
59+
""".trimIndent()
60+
spacingAroundParensRuleAssertThat(code)
61+
.hasLintViolation(2, 24, "Unexpected spacing before \"(\"")
62+
.isFormattedAs(formattedCode)
63+
}
64+
3165
@Test
3266
fun `Given a variable declaration with unexpected spacing around the opening parenthesis of the expression`() {
3367
val code =

0 commit comments

Comments
 (0)