Skip to content

Commit

Permalink
Ignore max line length in case the line contains only a string templa…
Browse files Browse the repository at this point in the history
…te followed by a comma (#2598)

Closes #2597
  • Loading branch information
paul-dingemans committed Mar 17, 2024
1 parent afd32bc commit feb3226
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit feb3226

Please sign in to comment.