fix corner cases in strings & templates (#5147)

fixes #5145
This commit is contained in:
Alex Lam S.L
2021-10-16 11:02:23 +01:00
committed by GitHub
parent faf0190546
commit 03aec89f60
3 changed files with 84 additions and 10 deletions

View File

@@ -10663,7 +10663,8 @@ merge(Compressor.prototype, {
&& self.left.operator == "+"
&& self.left.left instanceof AST_String
&& self.left.left.value == ""
&& self.right.is_string(compressor)) {
&& self.right.is_string(compressor)
&& (self.left.right.is_constant() || !self.right.has_side_effects(compressor))) {
self.left = self.left.right;
return self.optimize(compressor);
}
@@ -11392,15 +11393,29 @@ merge(Compressor.prototype, {
}).transform(compressor),
right: exprs[exprs.length - 1],
}).optimize(compressor);
if (strs[0] == "") return make_node(AST_Binary, self, {
operator: "+",
left: exprs[0],
right: make_node(AST_Template, self, {
expressions: exprs.slice(1),
strings: strs.slice(1),
tag: tag,
}).transform(compressor),
}).optimize(compressor);
if (strs[0] == "") {
var left = make_node(AST_Binary, self, {
operator: "+",
left: make_node(AST_String, self, { value: "" }),
right: exprs[0],
});
for (var i = 1; strs[i] == "" && i < exprs.length; i++) {
left = make_node(AST_Binary, self, {
operator: "+",
left: left,
right: exprs[i],
});
}
return best_of(compressor, self, make_node(AST_Binary, self, {
operator: "+",
left: left.transform(compressor),
right: make_node(AST_Template, self, {
expressions: exprs.slice(i),
strings: strs.slice(i),
tag: tag,
}).transform(compressor),
}).optimize(compressor));
}
}
self.expressions = exprs;
self.strings = strs;