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

Ignore max line length in case the line contains only a string template followed by a comma #2598

Merged
merged 3 commits into from
Mar 17, 2024
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
@@ -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()
}
}