Skip to content

Commit

Permalink
fix corner case in unsafe evaluate (#4932)
Browse files Browse the repository at this point in the history
fixes #4931
  • Loading branch information
alexlamsl committed May 14, 2021
1 parent 3c1898f commit 7576048
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -10778,14 +10778,16 @@ merge(Compressor.prototype, {

OPT(AST_Template, function(self, compressor) {
if (!compressor.option("templates")) return self;
if (!self.tag || is_raw_tag(compressor, self.tag)) {
var tag = self.tag;
if (!tag || is_raw_tag(compressor, tag)) {
var exprs = self.expressions.slice();
var strs = self.strings.slice();
var CHANGED = false;
for (var i = exprs.length; --i >= 0;) {
var node = exprs[i];
var ev = node.evaluate(compressor);
if (ev === node) continue;
if (tag && /\r|\\|`/.test(ev)) continue;
ev = ("" + ev).replace(/\r|\\|`/g, function(s) {
return "\\" + (s == "\r" ? "r" : s);
});
Expand All @@ -10794,11 +10796,11 @@ merge(Compressor.prototype, {
if (typeof make_node(AST_Template, self, {
expressions: [],
strings: [ combined ],
tag: self.tag,
tag: tag,
}).evaluate(compressor) != typeof make_node(AST_Template, self, {
expressions: [ node ],
strings: strs.slice(i, i + 2),
tag: self.tag,
tag: tag,
}).evaluate(compressor)) continue;
exprs.splice(i, 1);
strs.splice(i, 2, combined);
Expand Down
21 changes: 21 additions & 0 deletions test/compress/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,24 @@ issue_4676: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_4931: {
options = {
evaluate: true,
templates: true,
unsafe: true,
}
input: {
console.log(String.raw`${typeof A} ${"\r"}`);
console.log(String.raw`${"\\"} ${"`"}`);
}
expect: {
console.log(String.raw`${typeof A} ${"\r"}`);
console.log("\\ `");
}
expect_stdout: [
"undefined \r",
"\\ `",
]
node_version: ">=4"
}

0 comments on commit 7576048

Please sign in to comment.