From f343f6f2bbf25d424c5d5c61f856880b421b0cab Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Thu, 7 Mar 2024 21:02:31 +0100 Subject: [PATCH 1/2] Ignore max line length in case the line contains only a string template followed by a comma --- .../standard/rules/MaxLineLengthRule.kt | 2 ++ .../standard/rules/MaxLineLengthRuleTest.kt | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt index e6c893ea25..4f35ba9959 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt @@ -1,5 +1,6 @@ package com.pinterest.ktlint.ruleset.standard.rules +import com.pinterest.ktlint.rule.engine.core.api.ElementType.COMMA import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER import com.pinterest.ktlint.rule.engine.core.api.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule.Mode.REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED @@ -82,6 +83,7 @@ public class MaxLineLengthRule : ?.takeUnless { it.isPartOf(KDoc::class) } ?.takeUnless { it.isPartOfRawMultiLineString() } ?.takeUnless { it.isLineOnlyContainingSingleTemplateString() } + ?.takeUnless { it.elementType == COMMA && it.prevLeaf()?.isLineOnlyContainingSingleTemplateString() ?: false } ?.takeUnless { it.isLineOnlyContainingComment() } ?.let { // Calculate the offset at the last possible position at which the newline should be inserted on the line diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt index 6de8d216e0..5c0b9c0bef 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt @@ -205,4 +205,25 @@ class MaxLineLengthRuleTest { .withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 40) .hasNoLintViolations() } + + @Test + fun `Given a line containing a string template followed by comma then do not report it`() { + val code = + """ + // $MAX_LINE_LENGTH_MARKER $EOL_CHAR + fun foo() { + throw SomeException( + "A long exception message followed by a comma-----------", + e, + ) + // or + throw SomeException( + "A long exception message followed by a (trailing) comma", + ) + } + """.trimIndent() + maxLineLengthRuleAssertThat(code) + .setMaxLineLength() + .hasNoLintViolations() + } } From c8f7d363c236791c4e8b97b0a55b2e838ad9f2f3 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Thu, 7 Mar 2024 21:02:31 +0100 Subject: [PATCH 2/2] Ignore max line length in case the line contains only a string template followed by a comma Closes #2597 --- .../standard/rules/MaxLineLengthRule.kt | 2 ++ .../standard/rules/MaxLineLengthRuleTest.kt | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt index e6c893ea25..4f35ba9959 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt @@ -1,5 +1,6 @@ package com.pinterest.ktlint.ruleset.standard.rules +import com.pinterest.ktlint.rule.engine.core.api.ElementType.COMMA import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER import com.pinterest.ktlint.rule.engine.core.api.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule.Mode.REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED @@ -82,6 +83,7 @@ public class MaxLineLengthRule : ?.takeUnless { it.isPartOf(KDoc::class) } ?.takeUnless { it.isPartOfRawMultiLineString() } ?.takeUnless { it.isLineOnlyContainingSingleTemplateString() } + ?.takeUnless { it.elementType == COMMA && it.prevLeaf()?.isLineOnlyContainingSingleTemplateString() ?: false } ?.takeUnless { it.isLineOnlyContainingComment() } ?.let { // Calculate the offset at the last possible position at which the newline should be inserted on the line diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt index 6de8d216e0..5c0b9c0bef 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt @@ -205,4 +205,25 @@ class MaxLineLengthRuleTest { .withEditorConfigOverride(MAX_LINE_LENGTH_PROPERTY to 40) .hasNoLintViolations() } + + @Test + fun `Given a line containing a string template followed by comma then do not report it`() { + val code = + """ + // $MAX_LINE_LENGTH_MARKER $EOL_CHAR + fun foo() { + throw SomeException( + "A long exception message followed by a comma-----------", + e, + ) + // or + throw SomeException( + "A long exception message followed by a (trailing) comma", + ) + } + """.trimIndent() + maxLineLengthRuleAssertThat(code) + .setMaxLineLength() + .hasNoLintViolations() + } }