Skip to content

Commit

Permalink
Fix IndexOutOfBoundsException in argument-list-wrapping-rule format…
Browse files Browse the repository at this point in the history
…ting file with many corrections (pinterest#1083)
  • Loading branch information
t-kameyama authored and romtsn committed Aug 8, 2021
1 parent 2ec43ac commit 221372e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- 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))
- Fix formatting with comments (`colon-spacing`) ([#1057](https://github.com/pinterest/ktlint/issues/1057))
- Fix IndexOutOfBoundsException in `argument-list-wrapping-rule` formatting file with many corrections ([#1081](https://github.com/pinterest/ktlint/issues/1081))

### Changed
- Update Gradle shadow plugin to `6.1.0` version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.isWhiteSpace
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.lineIndent
import com.pinterest.ktlint.core.ast.lineNumber
import com.pinterest.ktlint.core.ast.prevLeaf
import com.pinterest.ktlint.core.ast.visit
import kotlin.math.max
Expand Down Expand Up @@ -234,10 +233,18 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
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
val controlFlowKeyword = when (val parent = containerNode.parent) {
is KtIfExpression -> parent.ifKeyword.node
is KtWhileExpression -> parent.firstChild.node
is KtDoWhileExpression -> parent.whileKeyword?.node
else -> null
}?.lineNumber() == lineNumber()
} ?: return false

var prevLeaf = prevLeaf() ?: return false
while (prevLeaf != controlFlowKeyword) {
if (prevLeaf.isWhiteSpaceWithNewline()) return false
prevLeaf = prevLeaf.prevLeaf() ?: return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,99 @@ class ArgumentListWrappingRuleTest {
)
).isEmpty()
}

// https://github.com/pinterest/ktlint/issues/1081
@Test
fun testManyCorrections() {
assertThat(
ArgumentListWrappingRule().format(
"""
package com.foo
class MyClass() {
private fun doSomething() {
if (d == 0 || e == 0f) {
c.ee(hh, d, d, yy)
} else {
foo.blah()
val dr = t - u
val xx = -gg(dr) * rr(2f * d, dr.hh)
foo.bar(
dd,
g - d
)
foo.baz(
a.b, a.c - d
)
foo.biz(
a.b, a.c - d,
a.b, a.c,
a.b + d, a.c
)
foo.baz(
a.x - d, a.c
)
foo.biz(
a.x - d, a.c,
a.x, a.c,
a.x, a.c - d
)
foo.baz(
a.x, a.j + d
)
}
}
}
""".trimIndent()
)
).isEqualTo(
"""
package com.foo
class MyClass() {
private fun doSomething() {
if (d == 0 || e == 0f) {
c.ee(hh, d, d, yy)
} else {
foo.blah()
val dr = t - u
val xx = -gg(dr) * rr(2f * d, dr.hh)
foo.bar(
dd,
g - d
)
foo.baz(
a.b,
a.c - d
)
foo.biz(
a.b,
a.c - d,
a.b,
a.c,
a.b + d,
a.c
)
foo.baz(
a.x - d,
a.c
)
foo.biz(
a.x - d,
a.c,
a.x,
a.c,
a.x,
a.c - d
)
foo.baz(
a.x,
a.j + d
)
}
}
}
""".trimIndent()
)
}
}

0 comments on commit 221372e

Please sign in to comment.