|
|
|
@ -107,9 +107,7 @@ export default class Renderer {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Optimize the TemplateLiteral to remove unnecessary nodes
|
|
|
|
// Optimize the TemplateLiteral to remove unnecessary nodes
|
|
|
|
// that both increase code size but also add additional and
|
|
|
|
collapse_literal(popped.literal);
|
|
|
|
// unnecessary string formatting at runtime.
|
|
|
|
|
|
|
|
collapse_literal(popped.literal)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return popped.literal;
|
|
|
|
return popped.literal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -132,33 +130,32 @@ function collapse_literal(literal: TemplateLiteral) {
|
|
|
|
if (literal.quasis.length) {
|
|
|
|
if (literal.quasis.length) {
|
|
|
|
// flatMap() to produce an array containing [quasi, expr, quasi, expr, ..., quasi]
|
|
|
|
// flatMap() to produce an array containing [quasi, expr, quasi, expr, ..., quasi]
|
|
|
|
const zip = literal.quasis.reduce((acc, cur, index) => {
|
|
|
|
const zip = literal.quasis.reduce((acc, cur, index) => {
|
|
|
|
const expr = literal.expressions[index]
|
|
|
|
const expr = literal.expressions[index];
|
|
|
|
acc.push(cur)
|
|
|
|
acc.push(cur);
|
|
|
|
if (expr) {
|
|
|
|
if (expr) {
|
|
|
|
acc.push(expr)
|
|
|
|
acc.push(expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
// If an expression is a simple string literal, combine it with its preceeding
|
|
|
|
// If an expression is a simple string literal, combine it with its preceeding
|
|
|
|
// and following quasi
|
|
|
|
// and following quasi
|
|
|
|
let curQuasi = zip[0]
|
|
|
|
let curQuasi = zip[0];
|
|
|
|
const newZip = [curQuasi]
|
|
|
|
const newZip = [curQuasi];
|
|
|
|
for (let i = 1; i < zip.length; i += 2) {
|
|
|
|
for (let i = 1; i < zip.length; i += 2) {
|
|
|
|
const expr = zip[i]
|
|
|
|
const expr = zip[i];
|
|
|
|
const nextQuasi = zip[i + 1]
|
|
|
|
const nextQuasi = zip[i + 1];
|
|
|
|
if (expr.type === 'Literal' && typeof expr.value === 'string') {
|
|
|
|
if (expr.type === 'Literal' && typeof expr.value === 'string') {
|
|
|
|
curQuasi.value.raw += escape_template(expr.value) + nextQuasi.value.raw
|
|
|
|
curQuasi.value.raw += escape_template(expr.value) + nextQuasi.value.raw;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
newZip.push(expr)
|
|
|
|
newZip.push(expr);
|
|
|
|
newZip.push(nextQuasi)
|
|
|
|
newZip.push(nextQuasi);
|
|
|
|
curQuasi = nextQuasi
|
|
|
|
curQuasi = nextQuasi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Reconstitute the quasi and expressions arrays
|
|
|
|
// Reconstitute the quasi and expressions arrays
|
|
|
|
literal.quasis = newZip.filter((_, index) => index % 2 === 0)
|
|
|
|
literal.quasis = newZip.filter((_, index) => index % 2 === 0);
|
|
|
|
literal.expressions = newZip.filter((_, index) => index % 2 === 1)
|
|
|
|
literal.expressions = newZip.filter((_, index) => index % 2 === 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|