Skip to content

Commit

Permalink
Fix string-template for dot-expression from which the toString() is r…
Browse files Browse the repository at this point in the history
…emoved (pinterest#1026)

* Fix string-template for dot-expression of which the redundant toString 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.

* Update changelog

* Fix code style violation

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
  • Loading branch information
3 people committed Mar 7, 2021
1 parent 81481f1 commit ff49b9b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 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](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

0 comments on commit ff49b9b

Please sign in to comment.