From 5b6c84ce6db110913e5a64e91008cb501a2f1ce4 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 15 Jan 2021 14:48:35 +0900 Subject: [PATCH] Fix false positive after `else` keyword (`argument-list-wrapping`) --- CHANGELOG.md | 1 + .../experimental/ArgumentListWrappingRule.kt | 6 ++++-- .../ArgumentListWrappingRuleTest.kt | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f079a387c2..b02218b745 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Don't remove the equals sign for a default argument (`no-line-break-before-assignment`) ([#1039](https://github.com/pinterest/ktlint/issues/1039)) - Fix internal error in `no-unused-imports` ([#1040](https://github.com/pinterest/ktlint/issues/1040)) - Fix false positives when declaration has tail comments (`spacing-between-declarations-with-comments`) ([#1053](https://github.com/pinterest/ktlint/issues/1053)) +- Fix false positive after `else` keyword (`argument-list-wrapping`) ([#1047](https://github.com/pinterest/ktlint/issues/1047)) ### Changed - Update Gradle shadow plugin to `6.1.0` version diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRule.kt index aa29ff9fb5..a688e97b6f 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRule.kt @@ -4,6 +4,7 @@ import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.ELSE import com.pinterest.ktlint.core.ast.ElementType.TYPE_ARGUMENT_LIST import com.pinterest.ktlint.core.ast.children import com.pinterest.ktlint.core.ast.column @@ -113,7 +114,7 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") { // RPAR val lineIndent = node.lineIndent() val indent = ("\n" + lineIndent.substring(0, (lineIndent.length - adjustedIndent).coerceAtLeast(0))) - .let { if (node.isOnSameLineAsIfKeyword()) it + " ".repeat(indentSize) else it } + .let { if (node.isOnSameLineAsControlFlowKeyword()) it + " ".repeat(indentSize) else it } val paramIndent = indent + " ".repeat(indentSize) nextChild@ for (child in node.children()) { when (child.elementType) { @@ -230,8 +231,9 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") { return null } - private fun ASTNode.isOnSameLineAsIfKeyword(): Boolean { + private fun ASTNode.isOnSameLineAsControlFlowKeyword(): Boolean { val containerNode = psi.getStrictParentOfType() ?: return false + if (containerNode.node.elementType == ELSE) return false return when (val parent = containerNode.parent) { is KtIfExpression, is KtWhileExpression -> parent.node is KtDoWhileExpression -> parent.whileKeyword?.node diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt index e4d1078bf4..1e221e6b7b 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt @@ -420,6 +420,24 @@ class ArgumentListWrappingRuleTest { ).isEmpty() } + @Test + fun testLintAfterElse() { + assertThat( + ArgumentListWrappingRule().lint( + """ + fun foo(i: Int, j: Int) = 1 + + fun test() { + val x = if (true) 1 else foo( + 2, + 3 + ) + } + """.trimIndent() + ) + ).isEmpty() + } + @Test fun testLintInWhenCondition() { assertThat(