From ab3d230fd82456059155dbb858c721bc54bbe6db Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 2 Jul 2024 14:49:02 +0200 Subject: [PATCH] Fix "unset" value for property ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than when it has When value is set to "unset" the number of chain operators on a single line is not restricted as long as the max line length is not exceeded. Closes #2712 --- .../standard/rules/ChainMethodContinuationRule.kt | 4 ++-- .../rules/ChainMethodContinuationRuleTest.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 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 4b0a48e54e..9824bdcf34 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 @@ -489,7 +489,7 @@ public class ChainMethodContinuationRule : private val chainOperatorTokenSet = TokenSet.create(DOT, SAFE_ACCESS) private val groupClosingElementType = TokenSet.create(CLOSING_QUOTE, RBRACE, RBRACKET, RPAR) - private const val FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY_UNSET = 4 + private const val FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY_UNSET = Int.MAX_VALUE public val FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY: EditorConfigProperty = EditorConfigProperty( type = @@ -500,7 +500,7 @@ public class ChainMethodContinuationRule : PropertyType.PropertyValueParser.POSITIVE_INT_VALUE_PARSER, setOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "unset"), ), - defaultValue = FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY_UNSET, + defaultValue = 4, propertyMapper = { property, _ -> if (property?.isUnset == true) { FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY_UNSET 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 1340e320be..9907a90320 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 @@ -1117,4 +1117,16 @@ class ChainMethodContinuationRuleTest { LintViolation(4, 59, "Expected newline before '.'"), ).isFormattedAs(formattedCode) } + + @Test + fun `Issue 2712 - Given some code having more chain operators than the default, but with ktlint_chain_method_rule_force_multiline_when_chain_operator_count_greater_or_equal_than unset`() { + val code = + """ + val foo = listOf(1, 2, 3).plus(4).plus(5).plus(6).plus(7).plus(8) + """.trimIndent() + require(code.count { it == '.' } > FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY.defaultValue) + chainMethodContinuationRuleAssertThat(code) + .withEditorConfigOverride(FORCE_MULTILINE_WHEN_CHAIN_OPERATOR_COUNT_GREATER_OR_EQUAL_THAN_PROPERTY to "unset") + .hasNoLintViolations() + } }