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

StringTemplateRule: don't report when template expression is keyword #888

Merged
merged 1 commit into from
Sep 6, 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
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this line for?

Copy link
Contributor Author

@t-kameyama t-kameyama Sep 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this line is not exists, testFormat will fail because entryExpression is KtDotQualifiedExpression.

callExpression of dotQualifiedExpression will be removed in the next line, so we need to save receiverExpression in entryExpression.

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