fix unused on AST_Destructuring (#2146)

This commit is contained in:
Alex Lam S.L
2017-06-23 13:11:26 +08:00
committed by GitHub
parent b163b13a0b
commit 137e4c4753
3 changed files with 23 additions and 44 deletions

View File

@@ -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);
}
}
});

View File

@@ -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;

View File

@@ -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"
}