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) }