fix corner case in `templates` (#4607)
authorAlex Lam S.L <alexlamsl@gmail.com>
Mon, 1 Feb 2021 18:29:43 +0000 (18:29 +0000)
committerGitHub <noreply@github.com>
Mon, 1 Feb 2021 18:29:43 +0000 (02:29 +0800)
fixes #4606

lib/compress.js
test/compress/templates.js

index ac7aa3e..001f2e1 100644 (file)
@@ -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]);
index 3b0e983..3c7957f 100644 (file)
@@ -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"
+}