From: Alex Lam S.L Date: Mon, 1 Feb 2021 18:29:43 +0000 (+0000) Subject: fix corner case in `templates` (#4607) X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=7110c6923b8ef82f295f5d9ff9d42d2f88432810;p=UglifyJS.git fix corner case in `templates` (#4607) fixes #4606 --- diff --git a/lib/compress.js b/lib/compress.js index ac7aa3e0..001f2e14 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -9956,7 +9956,9 @@ merge(Compressor.prototype, { var node = exprs[i]; var ev = node.evaluate(compressor); if (ev === node) continue; - ev = "" + ev; + ev = ("" + ev).replace(/\r|\\|`/g, function(s) { + return "\\" + (s == "\r" ? "r" : s); + }); if (ev.length > node.print_to_string().length + 3) continue; exprs.splice(i, 1); strs.splice(i, 2, strs[i] + ev + strs[i + 1]); diff --git a/test/compress/templates.js b/test/compress/templates.js index 3b0e983c..3c7957f8 100644 --- a/test/compress/templates.js +++ b/test/compress/templates.js @@ -222,3 +222,18 @@ issue_4604: { expect_stdout: "PASS" node_version: ">=4" } + +issue_4606: { + options = { + evaluate: true, + templates: true, + } + input: { + console.log(`${typeof A} ${"\r"} ${"\\"} ${"`"}`); + } + expect: { + console.log(`${typeof A} \r \\ \``); + } + expect_stdout: "undefined \r \\ `" + node_version: ">=4" +}