Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive with delegated properties (indent) #1190

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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