From 137e4c4753f405480d23e0989446156d71bbfcce Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 23 Jun 2017 13:11:26 +0800 Subject: [PATCH] fix `unused` on `AST_Destructuring` (#2146) --- lib/compress.js | 45 ++-------------------------------- lib/scope.js | 2 +- test/compress/destructuring.js | 20 +++++++++++++++ 3 files changed, 23 insertions(+), 44 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 9d54406e..a41c9fac 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2086,7 +2086,6 @@ merge(Compressor.prototype, { var var_defs_by_id = new Dictionary(); var initializations = new Dictionary(); var destructuring_value = null; - var in_definition = false; // pass 1: find out which symbols are directly used in // this scope (not in nested scopes). var scope = this; @@ -2125,9 +2124,7 @@ merge(Compressor.prototype, { if (def.name instanceof AST_Destructuring) { var destructuring_cache = destructuring_value; destructuring_value = def.value; - in_definition = true; def.walk(tw); - in_definition = false; destructuring_value = destructuring_cache; } else { initializations.add(def.name.name, def.value); @@ -2163,46 +2160,8 @@ merge(Compressor.prototype, { scope = save_scope; return true; } - if (node instanceof AST_Destructuring) { - if (!in_definition) { - return true; - } - for (var i = 0; i < node.names.length; i++) { - if (node.names[i] instanceof AST_Destructuring) { - node.names[i].walk(tw); - } - else if (node.names[i] instanceof AST_Expansion) { - if (node.names[i].expression instanceof AST_Symbol) { - initializations.add(node.names[i].expression.name, destructuring_value); - } else if (node.names[i].expression instanceof AST_Destructuring) { - node.names[i].expression.walk(tw); - } else { - throw new Error(string_template("Can't handle expansion of type: {type}", { - type: Object.getPrototypeOf(node.names[i].expression).TYPE - })); - } - } - else if (node.names[i] instanceof AST_ObjectKeyVal) { - if (typeof node.names[i].key === "string") { - initializations.add(node.names[i].key, destructuring_value); - } - } - else if (node.names[i] instanceof AST_Symbol) { - initializations.add(node.names[i].name, destructuring_value); - } - else if (node.names[i] instanceof AST_DefaultAssign) { - continue; - } - else if (node.names[i] instanceof AST_Hole) { - continue; - } - else { - throw new Error(string_template("Unknown destructuring element of type: {type}", { - type: Object.getPrototypeOf(node.names[i]).TYPE - })); - } - } - return true; + if (node.destructuring && destructuring_value) { + initializations.add(node.name, destructuring_value); } } }); diff --git a/lib/scope.js b/lib/scope.js index 59e55bab..33890c66 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -132,7 +132,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ scope = save_scope; return true; } - if (node instanceof AST_Destructuring && node.is_array === false) { + if (node instanceof AST_Destructuring) { in_destructuring = node; // These don't nest descend(); in_destructuring = null; diff --git a/test/compress/destructuring.js b/test/compress/destructuring.js index 99b5842b..56adf266 100644 --- a/test/compress/destructuring.js +++ b/test/compress/destructuring.js @@ -639,3 +639,23 @@ issue_2044_ecma_6_beautify: { } expect_exact: "({x: a = 1, y = 2 + b, z = 3 - c} = obj);" } + +issue_2140: { + options = { + unused: true, + } + input: { + !function() { + var t = {}; + console.log(([t.a] = [42])[0]); + }(); + } + expect: { + !function() { + var t = {}; + console.log(([t.a] = [42])[0]); + }(); + } + expect_stdout: "42" + node_version: ">=6" +}