fix dead_code on AST_Destructuring (#2392)

fixes #2383
This commit is contained in:
Alex Lam S.L
2017-10-23 00:34:34 +08:00
committed by GitHub
parent 44352eb26a
commit 7d9a8596a9
2 changed files with 76 additions and 1 deletions

View File

@@ -3315,7 +3315,21 @@ merge(Compressor.prototype, {
});
AST_Definitions.DEFMETHOD("remove_initializers", function(){
this.definitions.forEach(function(def){ def.value = null });
var decls = [];
this.definitions.forEach(function(def) {
if (def.name instanceof AST_SymbolDeclaration) {
def.value = null;
decls.push(def);
} else def.name.walk(new TreeWalker(function(node) {
if (node instanceof AST_SymbolDeclaration) {
decls.push(make_node(AST_VarDef, def, {
name: node,
value: null
}));
}
}));
});
this.definitions = decls;
});
AST_Definitions.DEFMETHOD("to_assignments", function(compressor){

View File

@@ -560,3 +560,64 @@ global_fns: {
"RangeError",
]
}
issue_2383_1: {
options = {
conditionals: true,
dead_code: true,
evaluate: true,
}
input: {
if (0) {
var {x, y} = foo();
}
}
expect: {
var x, y;
}
}
issue_2383_2: {
options = {
conditionals: true,
dead_code: true,
evaluate: true,
}
input: {
if (0) {
var {
x = 0,
y: [ w, , { z, p: q = 7 } ] = [ 1, 2, { z: 3 } ]
} = {};
}
console.log(x, q, w, z);
}
expect: {
var x, w, z, q;
console.log(x, q, w, z);
}
expect_stdout: "undefined undefined undefined undefined"
node_version: ">=6"
}
issue_2383_3: {
options = {
conditionals: true,
dead_code: true,
evaluate: true,
}
input: {
var b = 7, y = 8;
if (0) {
var a = 1, [ x, y, z ] = [ 2, 3, 4 ], b = 5;
}
console.log(a, x, y, z, b);
}
expect: {
var b = 7, y = 8;
var a, x, y, z, b;
console.log(a, x, y, z, b);
}
expect_stdout: "undefined undefined 8 undefined 7"
node_version: ">=6"
}