Skip to content

Commit

Permalink
StringTemplateRule: don't report when template expression is keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama committed Sep 5, 2020
1 parent 60f74f2 commit 99b6880
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY
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
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtThisExpression

class StringTemplateRule : Rule("string-template") {

Expand All @@ -30,6 +34,7 @@ class StringTemplateRule : Rule("string-template") {
}
*/
if (elementType == LONG_STRING_TEMPLATE_ENTRY) {
var entryExpression = (node.psi as? KtBlockStringTemplateEntry)?.expression
val entryStart = node.firstChildNode
val dotQualifiedExpression = entryStart.treeNext
if (dotQualifiedExpression?.elementType == DOT_QUALIFIED_EXPRESSION) {
Expand All @@ -41,13 +46,15 @@ class StringTemplateRule : Rule("string-template") {
) {
emit(dot.startOffset, "Redundant \"toString()\" call in string template", true)
if (autoCorrect) {
entryExpression = (entryExpression as? KtDotQualifiedExpression)?.receiverExpression
node.removeChild(dot)
node.removeChild(callExpression)
}
}
}
if (node.text.startsWith("${'$'}{") &&
node.text.let { it.substring(2, it.length - 1) }.all { it.isPartOfIdentifier() } &&
(entryExpression is KtNameReferenceExpression || entryExpression is KtThisExpression) &&
node.treeNext.let { nextSibling ->
nextSibling.elementType == CLOSING_QUOTE ||
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,21 @@ class E {
override fun toString(): String = "${s0}"
}

class F {
fun keyword() {
println("${this}")
println("${this@F}")
println("${null}")
println("${true}")
println("${false}")
}
}

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

0 comments on commit 99b6880

Please sign in to comment.