From 3c556b8689346f8256781455c0e1b2f00975570f Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 2 Feb 2021 15:07:31 +0000 Subject: [PATCH] fix corner case in `arguments` (#4609) fixes #4608 --- lib/compress.js | 8 ------- lib/scope.js | 11 +++++++-- test/compress/destructured.js | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 001f2e14..7293f9df 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); diff --git a/lib/scope.js b/lib/scope.js index a241cc59..5f10e33d 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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)) { diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 58fec606..dd2daca9 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -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" +} -- 2.34.1