From 055892d0d8089bbc9cf842e8e193ad240ed89329 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 4 Jun 2024 12:03:28 +0200 Subject: [PATCH] Do not remove the space before a function type inside a type projection Fixes regression bug introduced by #2630 --- .../standard/rules/SpacingAroundParensRule.kt | 12 +++++++++++- .../standard/rules/SpacingAroundParensRuleTest.kt | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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 171007e505..2188b92315 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 @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK_COMMENT +import com.pinterest.ktlint.rule.engine.core.api.ElementType.CONSTRUCTOR_CALLEE import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_TYPE import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER @@ -10,6 +11,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.SUPER_TYPE_CALL_ENTRY 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 @@ -20,6 +22,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline import com.pinterest.ktlint.rule.engine.core.api.nextLeaf import com.pinterest.ktlint.rule.engine.core.api.prevLeaf +import com.pinterest.ktlint.rule.engine.core.api.prevSibling import com.pinterest.ktlint.ruleset.standard.StandardRule import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -49,7 +52,14 @@ public class SpacingAroundParensRule : StandardRule("paren-spacing") { // Super keyword needs special-casing prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD || prevLeaf.prevLeaf()?.treeParent?.elementType == PRIMARY_CONSTRUCTOR || - prevLeaf.prevLeaf()?.treeParent?.elementType == TYPE_ARGUMENT_LIST + ( + // Disallow: + // class Foo : Bar ("test") + // class Foo : Bar ("test") + node.treeParent.treeParent.elementType == SUPER_TYPE_CALL_ENTRY && + prevLeaf.prevSibling()?.elementType == CONSTRUCTOR_CALLEE && + 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 1812e0e506..104ca5b8f0 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 @@ -62,6 +62,15 @@ class SpacingAroundParensRuleTest { .isFormattedAs(formattedCode) } + @Test + fun `Given a function type inside a type projection then do not remove space before the opening parenthesis`() { + val code = + """ + val foo: Map Foo> = emptyMap() + """.trimIndent() + spacingAroundParensRuleAssertThat(code).hasNoLintViolations() + } + @Test fun `Given a variable declaration with unexpected spacing around the opening parenthesis of the expression`() { val code =