diff options
Diffstat (limited to 'ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/StringTemplateRule.kt')
-rw-r--r-- | ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/StringTemplateRule.kt | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/StringTemplateRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/StringTemplateRule.kt index f58a4bfe..e63d7927 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/StringTemplateRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/StringTemplateRule.kt @@ -32,26 +32,27 @@ class StringTemplateRule : Rule("string-template") { if (dotQualifiedExpression?.node?.elementType == KtStubElementTypes.DOT_QUALIFIED_EXPRESSION) { val callExpression = dotQualifiedExpression!!.lastChild val dot = callExpression.prevSibling - if (dot.node.elementType == KtTokens.DOT && callExpression.text == "toString()" && - dotQualifiedExpression.firstChild.node.elementType != KtNodeTypes.SUPER_EXPRESSION) { - emit(dot.node.startOffset, "Redundant 'toString()' call in string template", true) + if (dot?.node?.elementType == KtTokens.DOT && + callExpression.text == "toString()" && + dotQualifiedExpression.firstChild?.node?.elementType != KtNodeTypes.SUPER_EXPRESSION) { + emit(dot.node.startOffset, "Redundant \"toString()\" call in string template", true) if (autoCorrect) { node.removeChild(dot.node) node.removeChild(callExpression.node) } } } - } - if (elementType == KtNodeTypes.LONG_STRING_TEMPLATE_ENTRY && - node.text.let { it.substring(2, it.length - 1) }.all { it.isPartOfIdentifier() } && - (node.treeNext.elementType == KtTokens.CLOSING_QUOTE || - (node.psi.nextSibling.node.elementType == KtNodeTypes.LITERAL_STRING_TEMPLATE_ENTRY && - !node.psi.nextSibling.text[0].isPartOfIdentifier()))) { - 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.psi.firstChild as LeafPsiElement).rawReplaceWithText("$") // entry start - (node.psi.lastChild as LeafPsiElement).rawReplaceWithText("") // entry end + if (node.text.startsWith("${'$'}{") && + node.text.let { it.substring(2, it.length - 1) }.all { it.isPartOfIdentifier() } && + (node.treeNext.elementType == KtTokens.CLOSING_QUOTE || + (node.psi.nextSibling.node.elementType == KtNodeTypes.LITERAL_STRING_TEMPLATE_ENTRY && + !node.psi.nextSibling.text[0].isPartOfIdentifier()))) { + 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.psi.firstChild as LeafPsiElement).rawReplaceWithText("$") // entry start + (node.psi.lastChild as LeafPsiElement).rawReplaceWithText("") // entry end + } } } } |