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

Ignore simple reference expressions in chain-method-continuation #2569

Merged
merged 1 commit into from
Feb 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public class ChainMethodContinuationRule :
chainedExpression
.chainOperators
.filterNot { it.isJavaClassReferenceExpression() }
.filterNot { it.isSimpleReferenceExpression() }
.forEach { chainOperator ->
when {
chainOperator.shouldBeOnSameLineAsClosingElementOfPreviousExpressionInMethodChain() -> {
Expand All @@ -160,6 +161,11 @@ public class ChainMethodContinuationRule :
nextCodeSibling()?.elementType == REFERENCE_EXPRESSION &&
nextCodeSibling()?.firstChildLeafOrSelf()?.text == "java"

private fun ASTNode.isSimpleReferenceExpression() =
treeParent.elementType == DOT_QUALIFIED_EXPRESSION &&
prevCodeSibling()?.elementType == REFERENCE_EXPRESSION &&
nextCodeSibling()?.elementType == REFERENCE_EXPRESSION

private fun ChainedExpression.wrapBeforeChainOperator() =
when {
hasNewlineBetweenFirstAndLastChainOperator -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,31 @@ class ChainMethodContinuationRuleTest {
LintViolation(6, 46, "Exceeded max line length (45)", false),
).hasNoLintViolationsExceptInAdditionalRules()
}

@Test
fun `Issue 2455 - Given a chained method including some simple reference expressions then do not wrap simple reference expressions`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun buildBar(): Foo.Bar = Foo.Bar.builder().baz().baz.build()
""".trimIndent()
val formattedCode =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun buildBar(): Foo.Bar = Foo.Bar
.builder()
.baz()
.baz
.build()
""".trimIndent()
chainMethodContinuationRuleAssertThat(code)
.setMaxLineLength()
.addAdditionalRuleProvider { MaxLineLengthRule() }
.hasLintViolations(
LintViolation(2, 34, "Expected newline before '.'"),
LintViolation(2, 44, "Expected newline before '.'"),
LintViolation(2, 50, "Expected newline before '.'"),
LintViolation(2, 54, "Expected newline before '.'"),
).isFormattedAs(formattedCode)
}
}