Skip to content

Commit

Permalink
Fix false positive with lambda argument and call chain (indent) (#1203
Browse files Browse the repository at this point in the history
)
  • Loading branch information
t-kameyama authored Aug 8, 2021
1 parent 29e3ef5 commit 3eb3950
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Fixed
- Fix false positive with lambda argument and call chain (`indent`) ([#1202](https://github.com/pinterest/ktlint/issues/1202))

### Changed
- Support absolute paths for globs ([#1131](https://github.com/pinterest/ktlint/issues/1131))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1130,8 +1130,9 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
}

private fun ASTNode?.isAfterLambdaArgumentOnSameLine(): Boolean {
val prevComma = this?.prevLeafOnSameLine(RBRACE)?.nextCodeLeaf()?.takeIf { it.elementType == COMMA }
return prevComma?.treeParent?.elementType == VALUE_ARGUMENT_LIST
if (this == null) return false
val prevComma = prevLeafOnSameLine(RBRACE)?.nextCodeLeaf()?.takeIf { it.elementType == COMMA } ?: return false
return prevComma.parent(VALUE_ARGUMENT_LIST) == parent(VALUE_ARGUMENT_LIST)
}

private fun ASTNode.hasLineBreak(vararg ignoreElementTypes: IElementType): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,22 @@ internal class IndentationRuleTest {
).isEmpty()
}

// https://github.com/pinterest/ktlint/issues/1202
@Test
fun `lint lambda argument and call chain`() {
assertThat(
IndentationRule().lint(
"""
class Foo {
fun bar() {
val foo = bar.associateBy({ item -> item.toString() }, ::someFunction).toMap()
}
}
""".trimIndent()
)
).isEmpty()
}

// https://github.com/pinterest/ktlint/issues/1165
@Test
fun `lint multiline expression with elvis operator in assignment`() {
Expand Down

0 comments on commit 3eb3950

Please sign in to comment.