From 57a5df7da14bf3f61804662f310e3fe7a934c167 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Mon, 15 Aug 2022 18:52:49 +0200 Subject: [PATCH] Prevent false positive in parameter list for which the last value parameter is a destructuring declaration followed by a trailing comma (#1582) Closes #1578 --- CHANGELOG.md | 1 + .../ktlint/ruleset/standard/WrappingRule.kt | 9 +++++++++ .../ktlint/ruleset/standard/WrappingRuleTest.kt | 17 +++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c12e3d9fb..b6bfd1cd25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -155,6 +155,7 @@ Several methods for which it is unlikely that they are used by API consumers hav * Split rule `trailing-comma` into `trailing-comma-on-call-site` and `trailing-comma-on-declaration-site` ([#1555](https://github.com/pinterest/ktlint/pull/1555)) * Fix indent of when entry with a dot qualified expression instead of simple value when trailing comma is required ([#1519](https://github.com/pinterest/ktlint/pull/1519)) * Fix whitespace between trailing comma and arrow in when entry when trailing comma is required ([#1519](https://github.com/pinterest/ktlint/pull/1519)) +* Prevent false positive in parameter list for which the last value parameter is a destructuring declaration followed by a trailing comma `wrapping` ([#1578](https://github.com/pinterest/ktlint/issues/1578)) ### Changed diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt index b155573f0d..92eb2fe7b8 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt @@ -12,6 +12,7 @@ import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.CLOSING_QUOTE import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.CONDITION +import com.pinterest.ktlint.core.ast.ElementType.DESTRUCTURING_DECLARATION import com.pinterest.ktlint.core.ast.ElementType.DOT import com.pinterest.ktlint.core.ast.ElementType.FUN import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL @@ -60,6 +61,7 @@ import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.psi.KtStringTemplateExpression import org.jetbrains.kotlin.psi.KtSuperTypeList +import org.jetbrains.kotlin.psi.psiUtil.siblings private val logger = KotlinLogging.logger {}.initKtLintKLogger() @@ -277,8 +279,11 @@ public class WrappingRule : } // insert \n after multi-line value val nextSibling = c.nextSibling { it.elementType != WHITE_SPACE } + val hasDestructuringDeclarationAsLastValueParameter = + c.isLastValueParameter() && c.firstChildNode.elementType == DESTRUCTURING_DECLARATION if ( nextSibling?.elementType == COMMA && + !hasDestructuringDeclarationAsLastValueParameter && !nextSibling.treeNext.isWhiteSpaceWithNewline() && // value( // ), // a comment @@ -291,6 +296,10 @@ public class WrappingRule : } } + private fun ASTNode.isLastValueParameter() = + elementType == VALUE_PARAMETER && + siblings().none { it.elementType == VALUE_PARAMETER } + private fun rearrangeClosingQuote( node: ASTNode, autoCorrect: Boolean, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt index f15f5af461..20f21fc934 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt @@ -1573,6 +1573,23 @@ internal class WrappingRuleTest { wrappingRuleAssertThat(code).hasNoLintViolations() } + @Test + fun `Issue 1578 - Given a destructuring declaration followed by a trailing comma then do not require it to be followed by a newline as no other value parameter follows`() { + val code = + """ + // fun foo(block: (Pair) -> Unit) {} + val bar = + foo { + ( + a, + b, + ), + -> + } + """.trimIndent() + wrappingRuleAssertThat(code).hasNoLintViolations() + } + private companion object { const val MULTILINE_STRING_QUOTE = "${'"'}${'"'}${'"'}" const val TAB = "${'\t'}"