fix corner cases in `strings` & `templates` (#5147)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 16 Oct 2021 10:02:23 +0000 (11:02 +0100)
committerGitHub <noreply@github.com>
Sat, 16 Oct 2021 10:02:23 +0000 (18:02 +0800)
fixes #5145

lib/compress.js
test/compress/concat-strings.js
test/compress/templates.js

index 0f567f5..a718410 100644 (file)
@@ -10663,7 +10663,8 @@ merge(Compressor.prototype, {
                 && self.left.operator == "+"
                 && self.left.left instanceof AST_String
                 && self.left.left.value == ""
-                && self.right.is_string(compressor)) {
+                && self.right.is_string(compressor)
+                && (self.left.right.is_constant() || !self.right.has_side_effects(compressor))) {
                 self.left = self.left.right;
                 return self.optimize(compressor);
             }
@@ -11392,15 +11393,29 @@ merge(Compressor.prototype, {
                     }).transform(compressor),
                     right: exprs[exprs.length - 1],
                 }).optimize(compressor);
-                if (strs[0] == "") return make_node(AST_Binary, self, {
-                    operator: "+",
-                    left: exprs[0],
-                    right: make_node(AST_Template, self, {
-                        expressions: exprs.slice(1),
-                        strings: strs.slice(1),
-                        tag: tag,
-                    }).transform(compressor),
-                }).optimize(compressor);
+                if (strs[0] == "") {
+                    var left = make_node(AST_Binary, self, {
+                        operator: "+",
+                        left: make_node(AST_String, self, { value: "" }),
+                        right: exprs[0],
+                    });
+                    for (var i = 1; strs[i] == "" && i < exprs.length; i++) {
+                        left = make_node(AST_Binary, self, {
+                            operator: "+",
+                            left: left,
+                            right: exprs[i],
+                        });
+                    }
+                    return best_of(compressor, self, make_node(AST_Binary, self, {
+                        operator: "+",
+                        left: left.transform(compressor),
+                        right: make_node(AST_Template, self, {
+                            expressions: exprs.slice(i),
+                            strings: strs.slice(i),
+                            tag: tag,
+                        }).transform(compressor),
+                    }).optimize(compressor));
+                }
             }
             self.expressions = exprs;
             self.strings = strs;
index 48b8d21..3512fbc 100644 (file)
@@ -289,3 +289,18 @@ issue_3689: {
     }
     expect_stdout: "00"
 }
+
+issue_5145: {
+    options = {
+        strings: true,
+    }
+    input: {
+        var a = [];
+        console.log("" + a + ((a[0] = 4) + "2"));
+    }
+    expect: {
+        var a = [];
+        console.log("" + a + (a[0] = 4) + "2");
+    }
+    expect_stdout: "42"
+}
index cbd4b85..f0b6ebe 100644 (file)
@@ -699,3 +699,47 @@ issue_5136: {
     expect_stdout: "42"
     node_version: ">=4"
 }
+
+issue_5145_1: {
+    options = {
+        strings: true,
+        templates: true,
+    }
+    input: {
+        var a = [];
+        console.log(`${a}${a[0] = 42}
+`);
+    }
+    expect: {
+        var a = [];
+        console.log(`${a}${a[0] = 42}
+`);
+    }
+    expect_stdout: [
+        "42",
+        "",
+    ]
+    node_version: ">=4"
+}
+
+issue_5145_2: {
+    options = {
+        strings: true,
+        templates: true,
+    }
+    input: {
+        var a = [];
+        console.log(`${a}${a}${a[0] = 42}
+`);
+    }
+    expect: {
+        var a = [];
+        console.log("" + a + a + (a[0] = 42) + `
+`);
+    }
+    expect_stdout: [
+        "42",
+        "",
+    ]
+    node_version: ">=4"
+}