Skip to content

Commit

Permalink
fix corner case in unused (#5086)
Browse files Browse the repository at this point in the history
fixes #5085
  • Loading branch information
alexlamsl committed Jul 18, 2021
1 parent ef5f7fc commit a7e7865
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6513,8 +6513,10 @@ merge(Compressor.prototype, {
return null;
}, true);
if (trimmed.name) {
def.name = trimmed.name;
def.value = value = trimmed.value;
def = make_node(AST_VarDef, def, {
name: trimmed.name,
value: value = trimmed.value,
});
flush();
} else if (trimmed.value) {
side_effects.push(trimmed.value);
Expand Down Expand Up @@ -6722,19 +6724,18 @@ merge(Compressor.prototype, {
}
if (node instanceof AST_Assign) {
descend(node, tt);
if (node.left instanceof AST_Destructured) {
var trimmed = trim_destructured(node.left, node.right, function(node) {
return node;
}, node.write_only === true);
if (!trimmed.name) {
if (trimmed.value) return trimmed.value;
if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip;
return make_node(AST_Number, node, { value: 0 });
}
node.left = trimmed.name;
node.right = trimmed.value;
}
return node;
if (!(node.left instanceof AST_Destructured)) return node;
var trimmed = trim_destructured(node.left, node.right, function(node) {
return node;
}, node.write_only === true);
if (trimmed.name) return make_node(AST_Assign, node, {
operator: node.operator,
left: trimmed.name,
right: trimmed.value,
});
if (trimmed.value) return trimmed.value;
if (parent instanceof AST_Sequence && parent.tail_node() !== node) return List.skip;
return make_node(AST_Number, node, { value: 0 });
}
if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) {
// Certain combination of unused name + side effect leads to invalid AST:
Expand Down
50 changes: 50 additions & 0 deletions test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -2948,3 +2948,53 @@ issue_5074_method_pure_getters: {
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5085_1: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unsafe: true,
unused: true,
}
input: {
var a = "PASS";
var [ b ] = [ 42, a ], c = b ? 0 : a = "FAIL";
console.log(a);
}
expect: {
var a = "PASS";
var b = [ 42 ][0];
b;
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5085_2: {
options = {
evaluate: true,
reduce_vars: true,
side_effects: true,
unsafe: true,
unused: true,
}
input: {
var a = "PASS";
(function(b) {
[ b ] = [ 42, a ];
var c = b ? 0 : a = "FAIL";
})();
console.log(a);
}
expect: {
var a = "PASS";
(function(b) {
b = [ 42 ][0];
})();
console.log(a);
}
expect_stdout: "PASS"
node_version: ">=6"
}

0 comments on commit a7e7865

Please sign in to comment.