From 524ecb3df78162b85e1ec79ac4d0470ef7c0fb94 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Fri, 4 Oct 2024 15:58:03 +0200 Subject: [PATCH] Do not insert a trailing comma in a multiline when-entry containing a guard (#2825) Closes #2817 --- .../TrailingCommaOnDeclarationSiteRule.kt | 9 +++++++ .../TrailingCommaOnDeclarationSiteRuleTest.kt | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt index c2a5a96e1c..511e412698 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRule.kt @@ -35,6 +35,7 @@ import com.pinterest.ktlint.rule.engine.core.util.cast import com.pinterest.ktlint.ruleset.standard.StandardRule import org.ec4j.core.model.PropertyType import org.ec4j.core.model.PropertyType.PropertyValueParser +import org.jetbrains.kotlin.KtNodeTypes.WHEN_ENTRY_GUARD import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -257,7 +258,13 @@ public class TrailingCommaOnDeclarationSiteRule : val trailingCommaNode = prevLeaf?.findPreviousTrailingCommaNodeOrNull() val trailingCommaState = when { + hasWhenGuard() -> { + // The compiler won't allow any comma in the when-entry in case it contains a guard clause + TrailingCommaState.NOT_EXISTS + } + isMultiline(psi) -> if (trailingCommaNode != null) TrailingCommaState.EXISTS else TrailingCommaState.MISSING + else -> if (trailingCommaNode != null) TrailingCommaState.REDUNDANT else TrailingCommaState.NOT_EXISTS } when (trailingCommaState) { @@ -425,6 +432,8 @@ public class TrailingCommaOnDeclarationSiteRule : return codeLeaf?.takeIf { it.elementType == COMMA } } + private fun ASTNode.hasWhenGuard() = elementType == WHEN_ENTRY && children().any { it.elementType == WHEN_ENTRY_GUARD } + private fun containsLineBreakInLeavesRange( from: PsiElement, to: PsiElement, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRuleTest.kt index 0c520fb513..50e3152453 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/TrailingCommaOnDeclarationSiteRuleTest.kt @@ -1066,4 +1066,28 @@ class TrailingCommaOnDeclarationSiteRuleTest { .hasLintViolation(3, 13, "Missing trailing comma and newline before \"->\"") .isFormattedAs(formattedCode) } + + @Test + fun `Issue 2817 - Given when condition with guard clause`() { + val code = + """ + val x1 = + when (true) { + true if foo("a") -> true + else -> false + } + val x2 = + when (true) { + true if foo( + "a", + ) + -> true + + else -> false + } + """.trimIndent() + trailingCommaOnDeclarationSiteRuleAssertThat(code) + .withEditorConfigOverride(TRAILING_COMMA_ON_DECLARATION_SITE_PROPERTY to true) + .hasNoLintViolations() + } }