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

Loosen dependency between chain-method-continuation and argument-list-wrapping #2468

Merged
merged 3 commits into from
Dec 29, 2023
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 @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -566,6 +587,7 @@ class ChainMethodContinuationRuleTest {
""".trimIndent()
chainMethodContinuationRuleAssertThat(code)
.setMaxLineLength()
.addAdditionalRuleProvider { ArgumentListWrappingRule() }
.hasNoLintViolationsExceptInAdditionalRules()
.isFormattedAs(formattedCode)
}
Expand Down