Skip to content

Commit

Permalink
Prevent false positive in parameter list for which the last value par…
Browse files Browse the repository at this point in the history
…ameter is a destructuring declaration followed by a trailing comma (pinterest#1582)

Closes pinterest#1578
  • Loading branch information
paul-dingemans committed Aug 15, 2022
1 parent 93e97a3 commit 57a5df7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

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

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
Expand Up @@ -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<Int, Int>) -> Unit) {}
val bar =
foo {
(
a,
b,
),
->
}
""".trimIndent()
wrappingRuleAssertThat(code).hasNoLintViolations()
}

private companion object {
const val MULTILINE_STRING_QUOTE = "${'"'}${'"'}${'"'}"
const val TAB = "${'\t'}"
Expand Down

0 comments on commit 57a5df7

Please sign in to comment.