fix corner cases in templates (#4610)
This commit is contained in:
parent
3c556b8689
commit
a2f27c7640
|
|
@ -769,6 +769,9 @@ to be `false` and all symbol names will be omitted.
|
|||
|
||||
- `switches` (default: `true`) -- de-duplicate and remove unreachable `switch` branches
|
||||
|
||||
- `templates` (default: `true`) -- compact template literals by embedding expressions
|
||||
and/or converting to string literals, e.g. `` `foo ${42}` → "foo 42"``
|
||||
|
||||
- `top_retain` (default: `null`) -- prevent specific toplevel functions and
|
||||
variables from `unused` removal (can be array, comma-separated, RegExp or
|
||||
function. Implies `toplevel`)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ partial_evaluate: {
|
|||
node_version: ">=4"
|
||||
}
|
||||
|
||||
malformed_evaluate: {
|
||||
malformed_evaluate_1: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
templates: true,
|
||||
|
|
@ -149,6 +149,52 @@ malformed_evaluate: {
|
|||
node_version: ">=4"
|
||||
}
|
||||
|
||||
malformed_evaluate_2: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
templates: true,
|
||||
}
|
||||
input: {
|
||||
console.log(`\u0${0}b${5}`);
|
||||
}
|
||||
expect: {
|
||||
console.log(`\u0${0}b5`);
|
||||
}
|
||||
expect_stdout: true
|
||||
node_version: ">=4"
|
||||
}
|
||||
|
||||
malformed_evaluate_3: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
templates: true,
|
||||
}
|
||||
input: {
|
||||
console.log(`\u${0}b${5}`);
|
||||
}
|
||||
expect: {
|
||||
console.log(`\u0b5`);
|
||||
}
|
||||
expect_stdout: true
|
||||
node_version: ">=4"
|
||||
}
|
||||
|
||||
malformed_evaluate_4: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
templates: true,
|
||||
unsafe: true,
|
||||
}
|
||||
input: {
|
||||
console.log(String.raw`\u0${0}b${5}`);
|
||||
}
|
||||
expect: {
|
||||
console.log("\\u00b5");
|
||||
}
|
||||
expect_stdout: "\\u00b5"
|
||||
node_version: ">=8"
|
||||
}
|
||||
|
||||
unsafe_evaluate: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user