fix corner case in `inline` (#4660)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 18 Feb 2021 08:15:44 +0000 (08:15 +0000)
committerGitHub <noreply@github.com>
Thu, 18 Feb 2021 08:15:44 +0000 (16:15 +0800)
fixes #4659

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

index a2a28ab..09e78d4 100644 (file)
@@ -5964,7 +5964,7 @@ merge(Compressor.prototype, {
                             if (old_def) old_def.forEach(function(node) {
                                 node.name = name_def.name;
                                 node.thedef = name_def;
-                                node.reference({});
+                                node.reference();
                             });
                             body.push(defun);
                         } else {
@@ -6798,7 +6798,7 @@ merge(Compressor.prototype, {
                         scope: self,
                         thedef: decl.definition()
                     });
-                    sym.reference({});
+                    sym.reference();
                     assignments.push(make_node(AST_Assign, node, {
                         operator: "=",
                         left: sym,
@@ -6845,7 +6845,7 @@ merge(Compressor.prototype, {
                     scope: node.expression.scope,
                     thedef: def
                 });
-                sym.reference({});
+                sym.reference();
                 return sym;
             }
             if (node instanceof AST_Unary) {
@@ -8469,12 +8469,12 @@ merge(Compressor.prototype, {
                     node = maintain_this_binding(compressor, parent, current, node);
                     if (replacing || best_of_expression(node, self) === node) {
                         refs.forEach(function(ref) {
-                            var def = ref.definition();
-                            def.references.push(ref);
+                            ref.scope = exp === fn ? fn.parent_scope : exp.scope;
+                            ref.reference();
                             if (replacing) {
-                                def.replaced++;
+                                ref.definition().replaced++;
                             } else {
-                                def.single_use = false;
+                                ref.definition().single_use = false;
                             }
                         });
                         return node;
@@ -10853,7 +10853,7 @@ merge(Compressor.prototype, {
             }, fn.argnames) === argname) {
                 def.reassigned = false;
                 var sym = make_node(AST_SymbolRef, self, argname);
-                sym.reference({});
+                sym.reference();
                 delete argname.__unused;
                 return sym;
             }
index 0f94d4e..f638890 100644 (file)
@@ -433,7 +433,9 @@ AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
     var def = this.definition();
     for (var s = this.scope; s; s = s.parent_scope) {
         push_uniq(s.enclosed, def);
-        if (options.keep_fnames) {
+        if (!options) {
+            delete s._var_names;
+        } else if (options.keep_fnames) {
             s.functions.each(function(d) {
                 push_uniq(def.scope.enclosed, d);
             });
index 7e2b45d..f521433 100644 (file)
@@ -5436,3 +5436,125 @@ issue_4655: {
     }
     expect_stdout: "PASS"
 }
+
+issue_4659_1: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+    }
+    input: {
+        var a = 0;
+        (function() {
+            function f() {
+                return a++;
+            }
+            (function() {
+                f && f();
+                (function() {
+                    var a = console && a;
+                })();
+            })();
+        })();
+        console.log(a);
+    }
+    expect: {
+        var a = 0;
+        (function() {
+            function f() {
+                return a++;
+            }
+            (function() {
+                f && a++;
+                (function() {
+                    var a = console && a;
+                })();
+            })();
+        })();
+        console.log(a);
+    }
+    expect_stdout: "1"
+}
+
+issue_4659_2: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+    }
+    input: {
+        var a = 0;
+        (function() {
+            function f() {
+                return a++;
+            }
+            (function() {
+                (function() {
+                    f && f();
+                })();
+                (function() {
+                    var a = console && a;
+                })();
+            })();
+        })();
+        console.log(a);
+    }
+    expect: {
+        var a = 0;
+        (function() {
+            function f() {
+                return a++;
+            }
+            (function() {
+                void (f && a++);
+                (function() {
+                    var a = console && a;
+                })();
+            })();
+        })();
+        console.log(a);
+    }
+    expect_stdout: "1"
+}
+
+issue_4659_3: {
+    options = {
+        inline: true,
+        reduce_vars: true,
+        unused: true,
+    }
+    input: {
+        var a = 0;
+        (function() {
+            function f() {
+                return a++;
+            }
+            (function() {
+                function g() {
+                    while (!console);
+                }
+                g(f && f());
+                (function() {
+                    var a = console && a;
+                })();
+            })();
+        })();
+        console.log(a);
+    }
+    expect: {
+        var a = 0;
+        (function() {
+            function f() {
+                return a++;
+            }
+            (function() {
+                (function() {
+                    while (!console);
+                })(f && a++);
+                (function() {
+                    var a = console && a;
+                })();
+            })();
+        })();
+        console.log(a);
+    }
+    expect_stdout: "1"
+}