diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRule.kt index 33cf472e18..b86eac62c9 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRule.kt @@ -9,6 +9,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.LPAR import com.pinterest.ktlint.rule.engine.core.api.ElementType.PRIMARY_CONSTRUCTOR import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_KEYWORD +import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT_LIST import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST import com.pinterest.ktlint.rule.engine.core.api.RuleId @@ -46,7 +47,8 @@ public class SpacingAroundParensRule : StandardRule("paren-spacing") { node.treeParent?.treeParent?.elementType != FUNCTION_TYPE || // Super keyword needs special-casing prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD || - prevLeaf.prevLeaf()?.treeParent?.elementType == PRIMARY_CONSTRUCTOR + prevLeaf.prevLeaf()?.treeParent?.elementType == PRIMARY_CONSTRUCTOR || + prevLeaf.prevLeaf()?.treeParent?.elementType == TYPE_ARGUMENT_LIST ) && ( node.treeParent?.elementType == VALUE_PARAMETER_LIST || diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRuleTest.kt index ee1bc869a7..1812e0e506 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundParensRuleTest.kt @@ -28,6 +28,40 @@ class SpacingAroundParensRuleTest { .isFormattedAs(formattedCode) } + @Test + fun `Given class with superclass constructor call`() { + val code = + """ + open class Bar(param: String) + class Foo : Bar ("test") + """.trimIndent() + val formattedCode = + """ + open class Bar(param: String) + class Foo : Bar("test") + """.trimIndent() + spacingAroundParensRuleAssertThat(code) + .hasLintViolation(2, 16, "Unexpected spacing before \"(\"") + .isFormattedAs(formattedCode) + } + + @Test + fun `Given class with superclass constructor call with type parameter`() { + val code = + """ + open class Bar(param: T) + class Foo : Bar ("test") + """.trimIndent() + val formattedCode = + """ + open class Bar(param: T) + class Foo : Bar("test") + """.trimIndent() + spacingAroundParensRuleAssertThat(code) + .hasLintViolation(2, 24, "Unexpected spacing before \"(\"") + .isFormattedAs(formattedCode) + } + @Test fun `Given a variable declaration with unexpected spacing around the opening parenthesis of the expression`() { val code =