From 724b2978efe68be0dfca93523d9c9c6bdb867bad Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Fri, 13 Oct 2023 10:23:58 +0200 Subject: [PATCH] Allow spread operator starting on new line (fixes regression of #2300) (#2306) --- .../standard/rules/ChainWrappingRule.kt | 30 ++++++++++++------- .../standard/rules/ChainWrappingRuleTest.kt | 11 +++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRule.kt index 8c9416237d..c3dfa8fa76 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRule.kt @@ -106,12 +106,18 @@ public class ChainWrappingRule : return } val prevLeaf = node.prevLeaf() - if (node.elementType != MUL && prevLeaf?.isPartOfSpread() == true) { - // fn(*typedArray<...>()) case + if (node.isPartOfSpread()) { + // Allow: + // fn( + // *typedArray<...>() + // ) return } if (prefixTokens.contains(elementType) && node.isInPrefixPosition()) { - // unary +/- + // Allow: + // fn( + // -42 + // ) return } @@ -160,14 +166,16 @@ public class ChainWrappingRule : } private fun ASTNode.isPartOfSpread() = - prevCodeLeaf()?.let { leaf -> - val type = leaf.elementType - type == LPAR || - type == COMMA || - type == LBRACE || - type == ELSE_KEYWORD || - KtTokens.OPERATIONS.contains(type) - } == true + elementType == MUL && + prevCodeLeaf() + ?.let { leaf -> + val type = leaf.elementType + type == LPAR || + type == COMMA || + type == LBRACE || + type == ELSE_KEYWORD || + KtTokens.OPERATIONS.contains(type) + } == true private fun ASTNode.isInPrefixPosition() = treeParent?.treeParent?.elementType == PREFIX_EXPRESSION diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRuleTest.kt index 52dc8bfce5..975288c092 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainWrappingRuleTest.kt @@ -329,4 +329,15 @@ class ChainWrappingRuleTest { .isFormattedAs(formattedCode) } } + + @Test + fun `Given a spread operator starting on a new line`() { + val code = + """ + val foo = foo( + *bar, + ) + """.trimIndent() + chainWrappingRuleAssertThat(code).hasNoLintViolations() + } }