Skip to content

Commit

Permalink
Fix false positive with delegated properties (indent) (#1190)
Browse files Browse the repository at this point in the history
Co-authored-by: Sha Sha Chu <shasha@pinterest.com>
  • Loading branch information
t-kameyama and shashachu authored Aug 5, 2021
1 parent d84b883 commit 66eef59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 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 delegated properties (`indent`) ([#1189](https://github.com/pinterest/ktlint/issues/1189))
- Fix false positive with lambda argument in super type entry (`indent`) ([#1188](https://github.com/pinterest/ktlint/issues/1188))

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
// ]}) should decrease expectedIndent by 1
val blockLine = ctx.blockOpeningLineStack.pop()
val prevBlockLine = ctx.blockOpeningLineStack.peek() ?: -1
val pairedLeftBrace = n.pairedLeftBrace()
if (prevBlockLine != blockLine && !pairedLeftBrace.isAfterLambdaArgumentOnSameLine()) {
val pairedLeft = n.pairedLeft()
if (prevBlockLine != blockLine && !pairedLeft.isAfterLambdaArgumentOnSameLine()) {
expectedIndent--
val byKeywordOnSameLine = pairedLeftBrace?.prevLeafOnSameLine(BY_KEYWORD)
val byKeywordOnSameLine = pairedLeft?.prevLeafOnSameLine(BY_KEYWORD)
if (byKeywordOnSameLine != null &&
byKeywordOnSameLine.prevLeaf()?.isWhiteSpaceWithNewline() == true &&
!byKeywordOnSameLine.isPartOf(DELEGATED_SUPER_TYPE_ENTRY)
Expand Down Expand Up @@ -1104,13 +1104,19 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
private fun ASTNode.isPartOfTypeConstraint() =
isPartOf(TYPE_CONSTRAINT_LIST) || nextLeaf()?.elementType == WHERE_KEYWORD

private fun ASTNode.pairedLeftBrace(): ASTNode? {
if (elementType != RBRACE) return null
private fun ASTNode.pairedLeft(): ASTNode? {
val rightType = elementType
val leftType = when (rightType) {
RPAR -> LPAR
RBRACE -> LBRACE
RBRACKET -> LBRACKET
else -> return null
}
var node: ASTNode? = prevLeaf()
while (node != null) {
node = when (node.elementType) {
LBRACE -> return node
RBRACE -> node.treeParent
leftType -> return node
rightType -> node.treeParent
else -> node.prevLeaf()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,45 @@ internal class IndentationRuleTest {
).isEmpty()
}

@Test
fun `lint property delegate is indented properly 4`() {
assertThat(
IndentationRule().lint(
"""
fun lazyList() = lazy { mutableListOf<String>() }
class Test {
val list: List<String>
by lazyList()
val aVar = 0
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint property delegate is indented properly 5`() {
assertThat(
IndentationRule().lint(
"""
fun lazyList(a: Int, b: Int) = lazy { mutableListOf<String>() }
class Test {
val list: List<String>
by lazyList(
1,
2
)
val aVar = 0
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint delegation 1`() {
assertThat(
Expand Down

0 comments on commit 66eef59

Please sign in to comment.