-
Notifications
You must be signed in to change notification settings - Fork 507
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
Add new experimental rule chain-method-continuation
#2088
Add new experimental rule chain-method-continuation
#2088
Conversation
...d/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationTest.kt
Outdated
Show resolved
Hide resolved
...d/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationTest.kt
Outdated
Show resolved
Hide resolved
...d/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationTest.kt
Outdated
Show resolved
Hide resolved
...d/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationTest.kt
Outdated
Show resolved
Hide resolved
...d/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationTest.kt
Outdated
Show resolved
Hide resolved
Formatting code below:
results in code which can not be compiled:
|
2cb679e
to
4842266
Compare
038746c
to
dec3753
Compare
...ndard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuation.kt
Outdated
Show resolved
Hide resolved
...ndard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuation.kt
Outdated
Show resolved
Hide resolved
...d/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationTest.kt
Outdated
Show resolved
Hide resolved
This rule ensures chain operators(`.` or `?.`) aligns with next call When there is `}` in the new line this also ensures that the chain operators align with `}` along with next call method
Handle minor comments Update docs and remove release docs
Simplify the error message
dec3753
to
baef5ba
Compare
Remove the usage of `noNewLineInOpenRange`
7b2f9af
to
41c1721
Compare
- Fix offsets - Messages only chain operator - if chain operator is at end of line then two messages needs to be emitted. One message because as a whitespace was expected before the chain operator. The other message because no whitespace was expected after the chain operator. - Break chained methods when max line is exceeded - A chained method should not continue on the previous line if that ends with "!!" Add discourage locations for comments between chain operator and the expression. Comments before the chain operator have to be allowed.
…rces the chain method to be wrapped even when the maximum line length is not exceeded
- Rename ChainMethodContinuation to ChainMethodContinuationRule - Add new function replaceStringTemplatePlaceholder in TestLiterals
# Conflicts: # CHANGELOG.md # documentation/release-latest/docs/rules/experimental.md
.filter { | ||
listOf(1, 2, 3).map { it * it }.size > 1 | ||
}!! | ||
.map { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this be as below
}!!.map {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it disaligns filter
and map
too much. At least, I found it very distracting.
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo = listOf(1, 2, 3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also test nested chains? for example
val foo = listOf(1, 2, 3).foo1(listOf(1, 2, 3).foo1().foo2().foo3().foo4().foo5().foo6()).foo2().foo3().foo4().foo5().foo6()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already catered for. Above is formatted as:
val foo =
listOf(1, 2, 3)
.foo1(
listOf(1, 2, 3)
.foo1()
.foo2()
.foo3()
.foo4()
.foo5()
.foo6(),
).foo2()
.foo3()
.foo4()
.foo5()
.foo6()
} | ||
|
||
@Test | ||
fun `Given nested chains which are correctly wrapped`() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also add TC with index operator like below
list(1)[
0
]
.inc()
list(1)[
0
].
inc()
list(1)[
0
]!!
.inc()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is not handled well (yet). It is formatted as:
list(1)[
0,
]
.inc()
list(1)[
0,
]
?.inc() // Assumption is that here the `?.` chain operator was intended
list(1)[
0,
]!!
.inc()
So a TC and fix is needed. I would say that expected outcome should be:
list(1)[
0,
].inc()
list(1)[
0,
]?.inc() // Assumption is that here the `?.` chain operator was intended
list(1)[
0,
]!!
.inc()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array access expressions are now added.
...c/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt
Show resolved
Hide resolved
...c/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt
Show resolved
Hide resolved
chain-method-continuation
Description
Add ChainMethodContinuation(chain-method-continuation) rule
This rule ensures chain operators(
.
or?.
) aligns with next callWhen there is
}
in the new line this also ensures that the chain operators align with}
along with the next call methodThis partially fixes #1953
Checklist
CHANGELOG.md
is updatedIn case of adding a new rule: