Skip to content

Commit

Permalink
Allow spread operator starting on new line (fixes regression of #2300)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-dingemans committed Oct 13, 2023
1 parent 2b25d78 commit a6c7eb9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit a6c7eb9

Please sign in to comment.