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

Fix string-template for dot-expression from which the toString() is removed #1026

Merged
merged 3 commits into from
Dec 21, 2020
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
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](http://semver.org/).
- Fix '.editorconfig' generation for "import-ordering" rule ([#1011](https://github.com/pinterest/ktlint/issues/1004))
- Fix "filename" rule will not work when '.editorconfig' file is not found ([#997](https://github.com/pinterest/ktlint/issues/1004))
- EditorConfig generation for `import-ordering` ([#1011](https://github.com/pinterest/ktlint/pull/1011))
- Internal error (`no-unused-imports`) ([#996](https://github.com/pinterest/ktlint/issues/996))

### Changed
- Update Gradle shadow plugin to `6.1.0` version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.LITERAL_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LONG_TEMPLATE_ENTRY_END
import com.pinterest.ktlint.core.ast.ElementType.LONG_TEMPLATE_ENTRY_START
import com.pinterest.ktlint.core.ast.ElementType.REGULAR_STRING_PART
import com.pinterest.ktlint.core.ast.ElementType.SUPER_EXPRESSION
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -65,9 +68,19 @@ class StringTemplateRule : Rule("string-template") {
) {
emit(node.treePrev.startOffset + 2, "Redundant curly braces", true)
if (autoCorrect) {
// fixme: a proper way would be to downcast to SHORT_STRING_TEMPLATE_ENTRY
(node.firstChildNode as LeafPsiElement).rawReplaceWithText("$") // entry start
(node.lastChildNode as LeafPsiElement).rawReplaceWithText("") // entry end
val leftCurlyBraceNode = node.findChildByType(LONG_TEMPLATE_ENTRY_START)
val rightCurlyBraceNode = node.findChildByType(LONG_TEMPLATE_ENTRY_END)
if (leftCurlyBraceNode != null && rightCurlyBraceNode != null) {
node.removeChild(leftCurlyBraceNode)
node.removeChild(rightCurlyBraceNode)
val remainingNode = node.firstChildNode
val newNode = if (remainingNode.elementType == DOT_QUALIFIED_EXPRESSION) {
LeafPsiElement(REGULAR_STRING_PART, "\$${remainingNode.text}")
} else {
LeafPsiElement(remainingNode.elementType, "\$${remainingNode.text}")
}
node.replaceChild(node.firstChildNode, newNode)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.test.diffFileFormat
import com.pinterest.ktlint.test.diffFileLint
import com.pinterest.ktlint.test.format
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

Expand All @@ -21,4 +22,23 @@ class StringTemplateRuleTest {
)
).isEmpty()
}

@Test
fun testFormatIssue996() {
assertThat(
StringTemplateRule().format(
"""
fun getDrafts(val draftsIds: List<Long>) {
println("draftIds=[${'$'}{draftsIds.toString()}]")
}
""".trimIndent()
)
).isEqualTo(
"""
fun getDrafts(val draftsIds: List<Long>) {
println("draftIds=[${'$'}draftsIds]")
}
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ fun main() {
val x = "${String::class}"
println("$x.hello")
println("$x.hello")
println("$x")
println("${x}hello")
println("${x.length}.hello")
println("$x.hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ fun main() {
val x = "${String::class.toString()}"
println("${x}.hello")
println("${x.toString()}.hello")
println("${x.toString()}")
println("${x}hello")
println("${x.length}.hello")
println("$x.hello")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fun main() {
println("${String::class.toString()}")
println("${hello.toString()}")
println("""${Int::class.toString()}""")
println("$s0")
println("""$s1""")
Expand Down Expand Up @@ -62,9 +63,10 @@ class F {

// expect
// 2:29:Redundant "toString()" call in string template
// 3:28:Redundant "toString()" call in string template
// 6:15:Redundant curly braces
// 3:21:Redundant "toString()" call in string template
// 4:28:Redundant "toString()" call in string template
// 7:15:Redundant curly braces
// 28:79:Redundant "toString()" call in string template
// 45:20:Redundant curly braces
// 55:19:Redundant curly braces
// 8:15:Redundant curly braces
// 29:79:Redundant "toString()" call in string template
// 46:20:Redundant curly braces
// 56:19:Redundant curly braces