fix corner cases in templates (#4610)

This commit is contained in:
Alex Lam S.L
2021-02-02 17:39:30 +00:00
committed by GitHub
parent 3c556b8689
commit a2f27c7640
3 changed files with 63 additions and 4 deletions

View File

@@ -4416,8 +4416,8 @@ merge(Compressor.prototype, {
}
var exprs = eval_all(this.expressions, compressor, ignore_side_effects, cached, depth);
if (!exprs) return this;
var ret = decode(this.strings[0]);
var malformed = false;
var ret = decode(this.strings[0]);
for (var i = 0; i < exprs.length; i++) {
ret += exprs[i] + decode(this.strings[i + 1]);
}
@@ -4426,7 +4426,7 @@ merge(Compressor.prototype, {
return this;
function decode(str) {
return str.replace(/\\(u[0-9a-fA-F]{4}|u\{[0-9a-fA-F]+\}|x[0-9a-fA-F]{2}|[0-9]+|[\s\S])/g, function(match, seq) {
return str.replace(/\\(u\{[^}]*\}?|u[\s\S]{0,4}|x[\s\S]{0,2}|[0-9]+|[\s\S])/g, function(match, seq) {
var s = decode_escape_sequence(seq);
if (typeof s != "string") malformed = true;
return s;
@@ -9952,8 +9952,18 @@ merge(Compressor.prototype, {
return "\\" + (s == "\r" ? "r" : s);
});
if (ev.length > node.print_to_string().length + 3) continue;
var combined = strs[i] + ev + strs[i + 1];
if (typeof make_node(AST_Template, self, {
expressions: [],
strings: [ combined ],
tag: self.tag,
}).evaluate(compressor) != typeof make_node(AST_Template, self, {
expressions: [ node ],
strings: strs.slice(i, i + 2),
tag: self.tag,
}).evaluate(compressor)) continue;
exprs.splice(i, 1);
strs.splice(i, 2, strs[i] + ev + strs[i + 1]);
strs.splice(i, 2, combined);
}
}
return try_evaluate(compressor, self);