Merge pull request #1733 from hzoo/i-1732

remove empty strings from beginning of template - fixes #1732
This commit is contained in:
Sebastian McKenzie 2015-06-15 10:46:28 +01:00
commit e08d400b36
3 changed files with 34 additions and 3 deletions

View File

@ -45,6 +45,10 @@ export function TaggedTemplateExpression(node, parent, scope, file) {
return t.callExpression(node.tag, args); return t.callExpression(node.tag, args);
} }
function isString(node) {
return t.isLiteral(node) && typeof node.value === "string";
}
export function TemplateLiteral(node, parent, scope, file) { export function TemplateLiteral(node, parent, scope, file) {
var nodes = []; var nodes = [];
@ -56,9 +60,18 @@ export function TemplateLiteral(node, parent, scope, file) {
} }
if (nodes.length > 1) { if (nodes.length > 1) {
// remove redundant '' at the end of the expression // filter out empty string literals
var last = nodes[nodes.length - 1]; nodes = nodes.filter(n => !t.isLiteral(n, { value: "" }));
if (t.isLiteral(last, { value: "" })) nodes.pop();
if (nodes.length === 1 && isString(nodes[0])) {
return nodes[0];
}
// since `+` is left-to-right associative
// ensure the first node is a string if first/second isn't
if (!isString(nodes[0]) && !isString(nodes[1])) {
nodes.unshift(t.literal(""));
}
var root = buildBinaryExpression(nodes.shift(), nodes.shift()); var root = buildBinaryExpression(nodes.shift(), nodes.shift());

View File

@ -0,0 +1,8 @@
const foo = 5;
const bar = 10;
const baz = 15;
const example = `${"a"}`;
const example2 = `${1}`;
const example3 = 1 + `${foo}${bar}${baz}`;
const example4 = 1 + `${foo}bar${baz}`;

View File

@ -0,0 +1,10 @@
"use strict";
var foo = 5;
var bar = 10;
var baz = 15;
var example = "a";
var example2 = "" + 1;
var example3 = 1 + ("" + foo + bar + baz);
var example4 = 1 + (foo + "bar" + baz);