Skip to content

Commit

Permalink
Fix string-template for dot-expression of which the redundant toStrin…
Browse files Browse the repository at this point in the history
…g is removed (pinterest#996)

The no-unused-imports rule fails on the existence of the dot-expression node but for which the actual call to the toString method was removed.
  • Loading branch information
Paul Dingemans committed Dec 20, 2020
1 parent 509b236 commit 98f9fd5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ 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 com.pinterest.ktlint.core.ast.children
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
Expand Down Expand Up @@ -65,9 +69,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 (node.children().count() == 3 && 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

0 comments on commit 98f9fd5

Please sign in to comment.