Fixed failing StringTemplateRuleTest
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 364dd99..e63d792 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,8 +32,9 @@
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) {
+ 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)
@@ -41,17 +42,17 @@
}
}
}
- }
- 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
+ }
}
}
}
diff --git a/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec b/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec
index 3866ac1..20e0990 100644
--- a/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec
+++ b/ktlint-ruleset-standard/src/test/resources/spec/string-template/lint.kt.spec
@@ -22,8 +22,8 @@
}
// expect
-// 2:29:Redundant 'toString()' call in string template
-// 3:28:Redundant 'toString()' call in string template
+// 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
-// 21:79:Redundant 'toString()' call in string template
+// 21:79:Redundant "toString()" call in string template