Skip to content

Commit

Permalink
Fix regression with argument-list-wrapping after dot qualified expres…
Browse files Browse the repository at this point in the history
…sion without assignment

Fixes pinterest#1159
  • Loading branch information
shashachu committed Aug 5, 2021
1 parent 6ec2944 commit 75bd848
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### 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))
- Fix regression from 0.41 with argument list wrapping after dot qualified expression (`argument-list-wrapping`)([#1159](https://github.com/pinterest/ktlint/issues/1159))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.pinterest.ktlint.ruleset.experimental
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.children
import com.pinterest.ktlint.core.ast.column
Expand All @@ -12,7 +11,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.parent
import com.pinterest.ktlint.core.ast.prevLeaf
import com.pinterest.ktlint.core.ast.visit
import kotlin.math.max
Expand Down Expand Up @@ -73,7 +71,6 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
// max_line_length exceeded
maxLineLength > -1 && (node.column - 1 + node.textLength) > maxLineLength && !node.textContains('\n')
if (putArgumentsOnSeparateLines) {
val prevWhitespaceWithNewline = node.prevLeaf { it.isWhiteSpaceWithNewline() }
val adjustedIndent = when {
// IDEA quirk:
// generic<
Expand All @@ -89,21 +86,20 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
// 1,
// 2
// )
node.hasTypeParameterListInFront() -> indentSize
node.hasTypeArgumentListInFront() -> indentSize
// IDEA quirk:
// foo
// .bar(
// .bar == Baz(
// 1,
// 2
// )
// instead of
// foo
// .bar(
// .bar == Baz(
// 1,
// 2
// )
prevWhitespaceWithNewline?.parent(DOT_QUALIFIED_EXPRESSION)
?.let { it.lastChildNode == node.parent(ElementType.CALL_EXPRESSION) } == true -> indentSize
node.isPartOfAssignmentExpression() -> indentSize
else -> 0
}

Expand Down Expand Up @@ -153,10 +149,10 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
emit(child.startOffset, errorMessage(child), true)
}
if (autoCorrect) {
val adjustedIndent =
val finalIndent =
(if (cut > -1) spacing.substring(0, cut) else "") + intendedIndent
argumentInnerIndentAdjustment = adjustedIndent.length - prevLeaf.getTextLength()
(prevLeaf as LeafPsiElement).rawReplaceWithText(adjustedIndent)
argumentInnerIndentAdjustment = finalIndent.length - prevLeaf.getTextLength()
(prevLeaf as LeafPsiElement).rawReplaceWithText(finalIndent)
}
} else {
emit(child.startOffset, errorMessage(child), true)
Expand Down Expand Up @@ -219,12 +215,15 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
}
}

private fun ASTNode.hasTypeParameterListInFront(): Boolean =
private fun ASTNode.hasTypeArgumentListInFront(): Boolean =
treeParent.children()
.firstOrNull { it.elementType == ElementType.TYPE_PARAMETER_LIST }
.firstOrNull { it.elementType == ElementType.TYPE_ARGUMENT_LIST }
?.children()
?.any { it.isWhiteSpaceWithNewline() } == true

private fun ASTNode.isPartOfAssignmentExpression(): Boolean =
treeParent?.treeParent?.elementType == ElementType.BINARY_EXPRESSION

private fun ASTNode.prevWhiteSpaceWithNewLine(): ASTNode? {
var prev = prevLeaf()
while (prev != null && (prev.isWhiteSpace() || prev.isPartOfComment())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,6 @@ class ArgumentListWrappingRuleTest {
assertThat(
ArgumentListWrappingRule().lint(
"""
class Logging(mode: Any, appInstanceIdentity: String, org: String)
class StateManager {
var firebaseLogger: Logging? = null
}
private fun replaceLogger(deviceId: String, orgName: String) {
val stateManager: StateManager = StateManager()
stateManager
Expand All @@ -570,6 +564,47 @@ class ArgumentListWrappingRuleTest {
""".trimIndent()
)
).isEmpty()

assertThat(
ArgumentListWrappingRule().lint(
"""
class MyClass {
private fun initCoilOkHttp() {
Coil.setImageLoader(
ImageLoader.Builder(this)
.crossfade(true)
.okHttpClient(
okHttpClient.newBuilder()
.cache(CoilUtils.createDefaultCache(this))
.build()
)
.build()
)
}
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint argument list after dot qualified expression with assignment`() {
assertThat(
ArgumentListWrappingRule().lint(
"""
private fun replaceLogger(deviceId: String, orgName: String) {
stateManager
.firebaseLogger = Logging(
mode = if (BuildConfig.DEBUG) Logging.Companion.LogDestination.DEV else Logging.Companion.LogDestination.PROD,
appInstanceIdentity = deviceId,
org = orgName
)
stateManager.firebaseLogger.tellTheCloudAboutMe()
customisation.attachToFirebase(stateManager.firebaseLogger.appCloudPrefix)
}
""".trimIndent()
)
).isEmpty()
}

@Test
Expand Down

0 comments on commit 75bd848

Please sign in to comment.