Don't call redundant toString for variables of type Int
Refactor StringTemplateTranslator a little
This commit is contained in:
@@ -71,6 +71,10 @@ public final class StringTest extends AbstractExpressionTest {
|
|||||||
fooBoxTest();
|
fooBoxTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testNumbersInTemplate() throws Exception {
|
||||||
|
fooBoxIsValue("2354");
|
||||||
|
}
|
||||||
|
|
||||||
private void checkHasNoToStringCalls() throws IOException {
|
private void checkHasNoToStringCalls() throws IOException {
|
||||||
for (EcmaVersion ecmaVersion : DEFAULT_ECMA_VERSIONS) {
|
for (EcmaVersion ecmaVersion : DEFAULT_ECMA_VERSIONS) {
|
||||||
String filePath = getOutputFilePath(getTestName(true) + ".kt", ecmaVersion);
|
String filePath = getOutputFilePath(getTestName(true) + ".kt", ecmaVersion);
|
||||||
|
|||||||
+20
-9
@@ -27,15 +27,14 @@ import org.jetbrains.jet.lang.resolve.name.Name;
|
|||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
import org.jetbrains.k2js.translate.general.Translation;
|
import org.jetbrains.k2js.translate.general.Translation;
|
||||||
import org.jetbrains.k2js.translate.utils.JsDescriptorUtils;
|
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.JsAstUtils.sum;
|
import static org.jetbrains.k2js.translate.utils.JsAstUtils.sum;
|
||||||
|
import static org.jetbrains.k2js.translate.utils.JsDescriptorUtils.getNameIfStandardType;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
//TODO: add toString call for non-primitive object
|
|
||||||
public final class StringTemplateTranslator extends AbstractTranslator {
|
public final class StringTemplateTranslator extends AbstractTranslator {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -85,18 +84,30 @@ public final class StringTemplateTranslator extends AbstractTranslator {
|
|||||||
JsExpression translatedExpression = Translation.translateAsExpression(entryExpression, context());
|
JsExpression translatedExpression = Translation.translateAsExpression(entryExpression, context());
|
||||||
if (translatedExpression instanceof JsNumberLiteral) {
|
if (translatedExpression instanceof JsNumberLiteral) {
|
||||||
append(context().program().getStringLiteral(translatedExpression.toString()));
|
append(context().program().getStringLiteral(translatedExpression.toString()));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else {
|
if (mustCallToString(entryExpression)) {
|
||||||
Name typeName = JsDescriptorUtils.getNameIfStandardType(entryExpression, context());
|
|
||||||
if (typeName != null && typeName.getName().equals("String")) {
|
|
||||||
append(translatedExpression);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
append(new JsInvocation(new JsNameRef("toString", translatedExpression)));
|
append(new JsInvocation(new JsNameRef("toString", translatedExpression)));
|
||||||
|
} else {
|
||||||
|
append(translatedExpression);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean mustCallToString(@NotNull JetExpression entryExpression) {
|
||||||
|
Name typeName = getNameIfStandardType(entryExpression, context());
|
||||||
|
if (typeName == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
//TODO: this is a hacky optimization, should use some generic approach
|
||||||
|
if (typeName.getName().equals("String")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (typeName.getName().equals("Int") && resultingExpression != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
|
public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
|
||||||
appendText(entry.getText());
|
appendText(entry.getText());
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun box() : String {
|
||||||
|
val number = 3
|
||||||
|
val s1 = "${number-1}${number}"
|
||||||
|
val s2 = "${5}${4}"
|
||||||
|
return "${s1}${s2}"
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user