Merge pull request #1733 from hzoo/i-1732
remove empty strings from beginning of template - fixes #1732
This commit is contained in:
commit
e08d400b36
@ -45,6 +45,10 @@ export function TaggedTemplateExpression(node, parent, scope, file) {
|
||||
return t.callExpression(node.tag, args);
|
||||
}
|
||||
|
||||
function isString(node) {
|
||||
return t.isLiteral(node) && typeof node.value === "string";
|
||||
}
|
||||
|
||||
export function TemplateLiteral(node, parent, scope, file) {
|
||||
var nodes = [];
|
||||
|
||||
@ -56,9 +60,18 @@ export function TemplateLiteral(node, parent, scope, file) {
|
||||
}
|
||||
|
||||
if (nodes.length > 1) {
|
||||
// remove redundant '' at the end of the expression
|
||||
var last = nodes[nodes.length - 1];
|
||||
if (t.isLiteral(last, { value: "" })) nodes.pop();
|
||||
// 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());
|
||||
|
||||
|
||||
@ -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}`;
|
||||
@ -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);
|
||||
Loading…
x
Reference in New Issue
Block a user