fix corner case in `arguments` (#4609)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 2 Feb 2021 15:07:31 +0000 (15:07 +0000)
committerGitHub <noreply@github.com>
Tue, 2 Feb 2021 15:07:31 +0000 (23:07 +0800)
fixes #4608

lib/compress.js
lib/scope.js
test/compress/destructured.js

index 001f2e1..7293f9d 100644 (file)
@@ -3703,14 +3703,6 @@ merge(Compressor.prototype, {
 
     var lazy_op = makePredicate("&& ||");
 
-    function is_lhs(node, parent) {
-        if (parent instanceof AST_Assign) return parent.left === node && node;
-        if (parent instanceof AST_DefaultValue) return parent.name === node && node;
-        if (parent instanceof AST_Destructured) return node;
-        if (parent instanceof AST_DestructuredKeyVal) return node;
-        if (parent instanceof AST_Unary) return unary_side_effects[parent.operator] && parent.expression;
-    }
-
     (function(def) {
         function to_node(value, orig) {
             if (value instanceof AST_Node) return value.clone(true);
index a241cc5..5f10e33 100644 (file)
@@ -101,6 +101,14 @@ SymbolDef.prototype = {
 
 var unary_side_effects = makePredicate("delete ++ --");
 
+function is_lhs(node, parent) {
+    if (parent instanceof AST_Assign) return parent.left === node && node;
+    if (parent instanceof AST_DefaultValue) return parent.name === node && node;
+    if (parent instanceof AST_Destructured) return node;
+    if (parent instanceof AST_DestructuredKeyVal) return node;
+    if (parent instanceof AST_Unary) return unary_side_effects[parent.operator] && parent.expression;
+}
+
 AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
     options = defaults(options, {
         cache: null,
@@ -269,8 +277,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
                 sym = self.def_global(node);
             } else if (name == "arguments" && is_arguments(sym)) {
                 var parent = tw.parent();
-                if (parent instanceof AST_Assign && parent.left === node
-                    || parent instanceof AST_Unary && unary_side_effects[parent.operator]) {
+                if (is_lhs(node, parent)) {
                     sym.scope.uses_arguments = 3;
                 } else if (sym.scope.uses_arguments < 2
                     && !(parent instanceof AST_PropAccess && parent.expression === node)) {
index 58fec60..dd2daca 100644 (file)
@@ -2514,3 +2514,45 @@ issue_4584: {
     expect_stdout: "PASS"
     node_version: ">=6"
 }
+
+issue_4608_1: {
+    options = {
+        arguments: true,
+        keep_fargs: false,
+    }
+    input: {
+        (function() {
+            [ arguments ] = [ "foo" ];
+            console.log(arguments[0]);
+        })();
+    }
+    expect: {
+        (function() {
+            [ arguments ] = [ "foo" ];
+            console.log(arguments[0]);
+        })();
+    }
+    expect_stdout: "f"
+    node_version: ">=6"
+}
+
+issue_4608_2: {
+    options = {
+        arguments: true,
+        reduce_vars: true,
+    }
+    input: {
+        (function(a) {
+            [ arguments ] = [ "foo" ];
+            console.log(arguments[0]);
+        })();
+    }
+    expect: {
+        (function(a) {
+            [ arguments ] = [ "foo" ];
+            console.log(arguments[0]);
+        })();
+    }
+    expect_stdout: "f"
+    node_version: ">=6"
+}