Skip to content

Commit

Permalink
Detect new lines inside string template before wrapping (#1779)
Browse files Browse the repository at this point in the history
Closes #1776
  • Loading branch information
paul-dingemans authored Jan 15, 2023
1 parent c158350 commit 2aedbd4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Fixed
* Do not enable the experimental rules by default when `.editorconfig` properties `disabled_rules` or `ktlint_disabled_rules` are set. ([#1771](https://github.com/pinterest/ktlint/issues/1771))
* A function signature not having any parameters which exceeds the `max-line-length` should be ignored by rule `function-signature` ([#1773](https://github.com/pinterest/ktlint/issues/1773))
*

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.pinterest.ktlint.core.ast.ElementType.LT
import com.pinterest.ktlint.core.ast.ElementType.OBJECT_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.RBRACE
import com.pinterest.ktlint.core.ast.ElementType.RBRACKET
import com.pinterest.ktlint.core.ast.ElementType.REGULAR_STRING_PART
import com.pinterest.ktlint.core.ast.ElementType.RPAR
import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_CALL_ENTRY
Expand Down Expand Up @@ -142,8 +143,8 @@ public class WrappingRule :
node.firstChildLeafOrSelf().elementType != EOL_COMMENT
}
maxLineLength > 0 -> {
val startOfLine = node.prevLeaf { it.isWhiteSpaceWithNewline() }
val endOfLine = node.nextLeaf { it.isWhiteSpaceWithNewline() }
val startOfLine = node.prevLeaf { it.isWhiteSpaceWithNewline() || (it.elementType == REGULAR_STRING_PART && it.text == "\n") }
val endOfLine = node.nextLeaf { it.isWhiteSpaceWithNewline() || (it.elementType == REGULAR_STRING_PART && it.text == "\n") }
val line =
startOfLine
?.leaves()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,70 @@ internal class WrappingRuleTest {
""".trimIndent()
wrappingRuleAssertThat(code).hasNoLintViolations()
}

@Nested
inner class `Issue 1776 - Given a string template containing a block` {
@Test
fun `Given that the string template is on a separate line in the raw string literal`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun getQueryString(query: QueryRequest): String {
val q = $MULTILINE_STRING_QUOTE
SELECT *
FROM table
WHERE 1 = 1
$.{query.gameId?.let { "AND id = ?" } ?: ""}
$MULTILINE_STRING_QUOTE
return q
}
""".replacePlaceholderWithStringTemplate()
.trimIndent()
wrappingRuleAssertThat(code)
.setMaxLineLength()
.hasNoLintViolations()
}

@Test
fun `Given that the string template is preceded by some text on the same line in the raw string literal`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun getQueryString(query: QueryRequest): String {
val q = $MULTILINE_STRING_QUOTE
SELECT *
FROM table
WHERE $.{query.gameId?.let { "id = ?" } ?: "1 = 1"}
$MULTILINE_STRING_QUOTE
return q
}
""".replacePlaceholderWithStringTemplate()
.trimIndent()
wrappingRuleAssertThat(code)
.setMaxLineLength()
.hasNoLintViolations()
}

@Test
fun `Given that the string template is followed by some text on the same line in the raw string literal`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun getQueryString(query: QueryRequest): String {
val q = $MULTILINE_STRING_QUOTE
SELECT *
FROM table
WHERE $.{query.gameId?.let { "id = ?" } ?: "1 = 1"} OR level = ?
$MULTILINE_STRING_QUOTE
return q
}
""".replacePlaceholderWithStringTemplate()
.trimIndent()
wrappingRuleAssertThat(code)
.setMaxLineLength()
.hasNoLintViolations()
}
}
}

// Replace the "$." placeholder with an actual "$" so that string "$.{expression}" is transformed to a String template
Expand Down

0 comments on commit 2aedbd4

Please sign in to comment.