fix corner case in `inline` (#5174)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sun, 14 Nov 2021 23:14:08 +0000 (23:14 +0000)
committerGitHub <noreply@github.com>
Sun, 14 Nov 2021 23:14:08 +0000 (07:14 +0800)
fixes #5173

lib/compress.js
test/compress/functions.js

index ab3d9df..2f5233b 100644 (file)
@@ -9601,7 +9601,7 @@ merge(Compressor.prototype, {
                 if (can_substitute_directly()) {
                     var args = self.args.slice();
                     var refs = [];
-                    args.push(value.clone(true).transform(new TreeTransformer(function(node) {
+                    var retValue = value.clone(true).transform(new TreeTransformer(function(node) {
                         if (node instanceof AST_SymbolRef) {
                             var def = node.definition();
                             if (fn.variables.get(node.name) !== def) {
@@ -9615,12 +9615,20 @@ merge(Compressor.prototype, {
                             var parent = this.parent();
                             return parent ? maintain_this_binding(compressor, parent, node, arg) : arg;
                         }
-                    })));
+                    }));
                     var save_inlined = fn.inlined;
                     if (exp !== fn) fn.inlined = true;
-                    var node = make_sequence(self, args.filter(function(arg) {
-                        return arg;
-                    })).optimize(compressor);
+                    var exprs = [];
+                    args.forEach(function(arg) {
+                        if (!arg) return;
+                        arg = arg.clone(true);
+                        arg.walk(new TreeWalker(function(node) {
+                            if (node instanceof AST_SymbolRef) refs.push(node);
+                        }));
+                        exprs.push(arg);
+                    }, []);
+                    exprs.push(retValue);
+                    var node = make_sequence(self, exprs).optimize(compressor);
                     fn.inlined = save_inlined;
                     node = maintain_this_binding(compressor, parent, current, node);
                     if (replacing || best_of_expression(node, self) === node) {
index d5687a1..fc3e7e2 100644 (file)
@@ -6655,3 +6655,49 @@ issue_5140: {
     }
     expect_stdout: "42"
 }
+
+issue_5173_1: {
+    options = {
+        conditionals: true,
+        inline: true,
+        reduce_vars: true,
+        side_effects: true,
+        toplevel: true,
+    }
+    input: {
+        function f(a, b) {
+            console.log(b);
+        }
+        f([ A = 42, [] + "" || (A = f) ]);
+    }
+    expect: {
+        function f(a, b) {
+            console.log(b);
+        }
+        f([ A = 42, [] + "" || (A = f) ]);
+    }
+    expect_stdout: "undefined"
+}
+
+issue_5173_2: {
+    options = {
+        conditionals: true,
+        inline: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function f(a, b) {
+            console.log(b);
+        }
+        f([ A = 42, [] + "" || (A = f) ]);
+    }
+    expect: {
+        function f(a, b) {
+            console.log(b);
+        }
+        f(A = [] + "" ? 42 : f);
+    }
+    expect_stdout: "undefined"
+}