move up template literal simplification logic - fixes #1874

This commit is contained in:
Sebastian McKenzie 2015-06-29 23:36:06 +01:00
parent d63ae5fce8
commit e55ce575cd
3 changed files with 11 additions and 13 deletions

View File

@ -60,20 +60,16 @@ export var visitor = {
if (expr) nodes.push(expr); if (expr) nodes.push(expr);
} }
// filter out empty string literals
nodes = nodes.filter(n => !t.isLiteral(n, { value: "" }));
// 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(""));
}
if (nodes.length > 1) { if (nodes.length > 1) {
// filter out empty string literals
nodes = nodes.filter(n => !t.isLiteral(n, { value: "" }));
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());
for (let node of (nodes: Array)) { for (let node of (nodes: Array)) {

View File

@ -6,3 +6,4 @@ const example = `${"a"}`;
const example2 = `${1}`; const example2 = `${1}`;
const example3 = 1 + `${foo}${bar}${baz}`; const example3 = 1 + `${foo}${bar}${baz}`;
const example4 = 1 + `${foo}bar${baz}`; const example4 = 1 + `${foo}bar${baz}`;
const example5 = `${""}`;

View File

@ -8,3 +8,4 @@ var example = "a";
var example2 = "" + 1; var example2 = "" + 1;
var example3 = 1 + ("" + foo + bar + baz); var example3 = 1 + ("" + foo + bar + baz);
var example4 = 1 + (foo + "bar" + baz); var example4 = 1 + (foo + "bar" + baz);
var example5 = "";