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();
|
||||
}
|
||||
|
||||
public void testNumbersInTemplate() throws Exception {
|
||||
fooBoxIsValue("2354");
|
||||
}
|
||||
|
||||
private void checkHasNoToStringCalls() throws IOException {
|
||||
for (EcmaVersion ecmaVersion : DEFAULT_ECMA_VERSIONS) {
|
||||
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.general.AbstractTranslator;
|
||||
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.JsDescriptorUtils.getNameIfStandardType;
|
||||
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
//TODO: add toString call for non-primitive object
|
||||
public final class StringTemplateTranslator extends AbstractTranslator {
|
||||
|
||||
@NotNull
|
||||
@@ -85,18 +84,30 @@ public final class StringTemplateTranslator extends AbstractTranslator {
|
||||
JsExpression translatedExpression = Translation.translateAsExpression(entryExpression, context());
|
||||
if (translatedExpression instanceof JsNumberLiteral) {
|
||||
append(context().program().getStringLiteral(translatedExpression.toString()));
|
||||
return;
|
||||
}
|
||||
else {
|
||||
Name typeName = JsDescriptorUtils.getNameIfStandardType(entryExpression, context());
|
||||
if (typeName != null && typeName.getName().equals("String")) {
|
||||
append(translatedExpression);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mustCallToString(entryExpression)) {
|
||||
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
|
||||
public void visitLiteralStringTemplateEntry(@NotNull JetLiteralStringTemplateEntry entry) {
|
||||
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