aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Dingemans <paul-dingemans@users.noreply.github.com>2024-03-17 18:35:45 +0100
committerGitHub <noreply@github.com>2024-03-17 18:35:45 +0100
commitfeb32261431e5605a39ca705f779150b32ec28b5 (patch)
tree253b7477a88058ee2f06a053f3bd438cd212231b
parentafd32bc573345c2b8c5795c8b27b13483bdf2ae9 (diff)
downloadktlint-feb32261431e5605a39ca705f779150b32ec28b5.tar.gz
Ignore max line length in case the line contains only a string template followed by a comma (#2598)
Closes #2597
-rw-r--r--ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRule.kt2
-rw-r--r--ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/MaxLineLengthRuleTest.kt21
2 files changed, 23 insertions, 0 deletions
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 e6c893ea..4f35ba99 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 6de8d216..5c0b9c0b 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()
+ }
}