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 when argument list is after multiline dot-qualified expression #1025

Merged
merged 2 commits into from
Dec 22, 2020
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 @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix "filename" rule will not work when '.editorconfig' file is not found ([#997](https://github.com/pinterest/ktlint/issues/1004))
- EditorConfig generation for `import-ordering` ([#1011](https://github.com/pinterest/ktlint/pull/1011))
- Internal error (`no-unused-imports`) ([#996](https://github.com/pinterest/ktlint/issues/996))
- Fix false positive when argument list is after multiline dot-qualified expression (`argument-list-wrapping`) ([#893](https://github.com/pinterest/ktlint/issues/893))

### Changed
- Update Gradle shadow plugin to `6.1.0` version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ 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.TYPE_ARGUMENT_LIST
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.column
import com.pinterest.ktlint.core.ast.isPartOf
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isRoot
import com.pinterest.ktlint.core.ast.isWhiteSpace
Expand Down Expand Up @@ -71,21 +74,38 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
// max_line_length exceeded
maxLineLength > -1 && (node.column - 1 + node.textLength) > maxLineLength && !node.textContains('\n')
if (putArgumentsOnSeparateLines) {
// IDEA quirk:
// generic<
// T,
// R>(
// 1,
// 2
// )
// instead of
// generic<
// T,
// R>(
// 1,
// 2
// )
val adjustedIndent = if (node.hasTypeParameterListInFront()) indentSize else 0
val prevWhitespaceWithNewline = node.prevLeaf { it.isWhiteSpaceWithNewline() }
val adjustedIndent = when {
// IDEA quirk:
// generic<
// T,
// R>(
// 1,
// 2
// )
// instead of
// generic<
// T,
// R>(
// 1,
// 2
// )
prevWhitespaceWithNewline?.isPartOf(TYPE_ARGUMENT_LIST) == true -> indentSize
// IDEA quirk:
// foo
// .bar(
// 1,
// 2
// )
// instead of
// foo
// .bar(
// 1,
// 2
// )
prevWhitespaceWithNewline?.isPartOf(DOT_QUALIFIED_EXPRESSION) == true -> indentSize
else -> 0
}

// aiming for
// ... LPAR
Expand Down Expand Up @@ -199,12 +219,6 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
}
}

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

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 @@ -528,4 +528,47 @@ class ArgumentListWrappingRuleTest {
)
).isEmpty()
}

@Test
fun `lint argument list after multiline dot qualified expression`() {
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
.firebaseLogger(
mode = 0,
appInstanceIdentity = deviceId,
org = orgName
)
}
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint argument list after multiline type argument list`() {
assertThat(
ArgumentListWrappingRule().lint(
"""
fun test() {
generic<
Int,
Int>(
1,
2
)
}
""".trimIndent()
)
).isEmpty()
}
}