From 75760481184c6d63595f8f7c3456b664d221abb5 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 14 May 2021 15:51:19 +0100 Subject: [PATCH] fix corner case in `unsafe` `evaluate` (#4932) fixes #4931 --- lib/compress.js | 8 +++++--- test/compress/templates.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 38e76620..18c1d265 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10778,7 +10778,8 @@ 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; @@ -10786,6 +10787,7 @@ merge(Compressor.prototype, { 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); }); @@ -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); diff --git a/test/compress/templates.js b/test/compress/templates.js index bac1f061..3df3efb7 100644 --- a/test/compress/templates.js +++ b/test/compress/templates.js @@ -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" +}