Skip to content

Commit

Permalink
Merge pull request #1059 from t-kameyama/issue_1047
Browse files Browse the repository at this point in the history
Fix false positive after `else` keyword (`argument-list-wrapping`)
  • Loading branch information
Tapchicoma authored Jan 15, 2021
2 parents 05a1dbd + 5b6c84c commit 12d7591
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +114,7 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
// <line indent> 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) {
Expand Down Expand Up @@ -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<KtContainerNode>() ?: 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 12d7591

Please sign in to comment.