Skip to content

Commit

Permalink
Do not remove the space before a function type inside a type projection
Browse files Browse the repository at this point in the history
Fixes regression bug introduced by #2630
  • Loading branch information
paul-dingemans committed Jun 4, 2024
1 parent fc164be commit 055892d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<String> ("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 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, (Foo) -> Foo> = emptyMap()
""".trimIndent()
spacingAroundParensRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given a variable declaration with unexpected spacing around the opening parenthesis of the expression`() {
val code =
Expand Down

0 comments on commit 055892d

Please sign in to comment.