From 736e4ab6662e4f1da1f685138d278f66c11a0216 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Fri, 29 Dec 2023 20:02:17 +0100 Subject: [PATCH] Loosen dependency between chain-method-continuation and argument-list-wrapping (#2468) * Loosen dependency between chain-method-continuation and argument-list-wrapping Allow the chain-method-continuation rule to be run when argument-list-wrapping is disabled or not loaded. Whenever both rules are loaded, the argument-list-wrapping takes precedence and runs before the chain-method-continuation. Closes #2452 --- .../rules/ChainMethodContinuationRule.kt | 6 ++--- .../rules/ChainMethodContinuationRuleTest.kt | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt index 7e199b9684..5ddf6d1bdd 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRule.kt @@ -26,7 +26,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.rule.engine.core.api.IndentConfig import com.pinterest.ktlint.rule.engine.core.api.Rule import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule -import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule.Mode.ONLY_WHEN_RUN_AFTER_RULE_IS_LOADED_AND_ENABLED +import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule.Mode.REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED import com.pinterest.ktlint.rule.engine.core.api.RuleId import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL @@ -71,7 +71,7 @@ import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet public class ChainMethodContinuationRule : StandardRule( id = "chain-method-continuation", - visitorModifiers = setOf(RunAfterRule(ARGUMENT_LIST_WRAPPING_RULE_ID, ONLY_WHEN_RUN_AFTER_RULE_IS_LOADED_AND_ENABLED)), + visitorModifiers = setOf(RunAfterRule(ARGUMENT_LIST_WRAPPING_RULE_ID, REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED)), usesEditorConfigProperties = setOf( CODE_STYLE_PROPERTY, @@ -249,8 +249,6 @@ public class ChainMethodContinuationRule : private fun ASTNode.isPrecededByComment() = treeParent.children().any { it.isPartOfComment() } - private fun ASTNode.isSucceededByComment() = nextSibling()?.isPartOfComment() ?: false - private fun insertWhiteSpaceBeforeChainOperator( chainOperator: ASTNode, emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt index ce56d437b9..da23d7c1a3 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/ChainMethodContinuationRuleTest.kt @@ -546,6 +546,27 @@ class ChainMethodContinuationRuleTest { @Nested inner class `Given a chained expression as function argument` { + @Test + fun `Issue 2452 - Given that the argument-list-wrapping rule is not loaded or not enabled`() { + val code = + """ + val foo1 = requireNotNull(bar.filter { it == 'b' }.filter { it == 'a' } + .filter { it == 'r' }) + val foo2 = requireNotNull(bar.filter { it == 'b' }.filter { it == 'a' }.filter { it == 'r' }) + """.trimIndent() + val formattedCode = + """ + val foo1 = requireNotNull(bar + .filter { it == 'b' } + .filter { it == 'a' } + .filter { it == 'r' }) + val foo2 = requireNotNull(bar.filter { it == 'b' }.filter { it == 'a' }.filter { it == 'r' }) + """.trimIndent() + chainMethodContinuationRuleAssertThat(code) + .addAdditionalRuleProvider { IndentationRule() } + .isFormattedAs(formattedCode) + } + @Test fun `Wrapping the argument has precedence above wrapping on the chain operator`() { val code = @@ -566,6 +587,7 @@ class ChainMethodContinuationRuleTest { """.trimIndent() chainMethodContinuationRuleAssertThat(code) .setMaxLineLength() + .addAdditionalRuleProvider { ArgumentListWrappingRule() } .hasNoLintViolationsExceptInAdditionalRules() .isFormattedAs(formattedCode) }