Skip to content

Commit

Permalink
fix corner case in unused (#5361)
Browse files Browse the repository at this point in the history
fixes #5360
  • Loading branch information
alexlamsl committed Feb 19, 2022
1 parent a7d0616 commit fbc5ecf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
31 changes: 26 additions & 5 deletions lib/compress.js
Expand Up @@ -7734,16 +7734,37 @@ Compressor.prototype.compress = function(node) {
return prop.key instanceof AST_Node && prop.key.has_side_effects(compressor);
}

function clear_write_only(node) {
if (node instanceof AST_Assign) {
node.write_only = false;
clear_write_only(node.right);
} else if (node instanceof AST_Binary) {
if (!lazy_op[node.operator]) return;
clear_write_only(node.left);
clear_write_only(node.right);
} else if (node instanceof AST_Conditional) {
clear_write_only(node.consequent);
clear_write_only(node.alternative);
} else if (node instanceof AST_Sequence) {
clear_write_only(node.tail_node());
} else if (node instanceof AST_Unary) {
node.write_only = false;
}
}

function retain_lhs(node) {
if (node instanceof AST_DefaultValue) return retain_lhs(node.name);
if (node instanceof AST_Destructured) {
if (value === null) {
value = make_node(AST_Number, node, { value: 0 });
} else if (value && (value.tail_node().write_only === true
|| value.may_throw_on_access(compressor, true))) {
value = make_node(AST_Array, node, {
elements: value instanceof AST_Sequence ? value.expressions : [ value ],
});
} else if (value) {
if (value.may_throw_on_access(compressor, true)) {
value = make_node(AST_Array, node, {
elements: value instanceof AST_Sequence ? value.expressions : [ value ],
});
} else {
clear_write_only(value);
}
}
return make_node(AST_DestructuredObject, node, { properties: [] });
}
Expand Down
36 changes: 34 additions & 2 deletions test/compress/rests.js
Expand Up @@ -1049,7 +1049,9 @@ issue_5100_1: {
p: {},
...a
} = [ {
p: [ a = 42["q"] ],
p: {
q: a,
} = 42,
r: "PASS",
} ][0]);
console.log(a.r);
Expand Down Expand Up @@ -1082,7 +1084,9 @@ issue_5100_2: {
p: {},
...a
} = [ {
p: [ console.log("PASS"), a = 42["q"] ],
p: (console.log("PASS"), {
q: a,
} = 42),
} ][0]);
}
expect_stdout: "PASS"
Expand Down Expand Up @@ -1267,3 +1271,31 @@ issue_5246_3: {
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5360: {
options = {
keep_fargs: false,
pure_getters: "strict",
unused: true,
}
input: {
var a;
console.log(function({ p: {}, ...b }) {
return b.q;
}({
p: ~a && ([ a ] = []),
q: "PASS",
}));
}
expect: {
var a;
console.log(function({ p: {}, ...b }) {
return b.q;
}({
p: ~a && ([ a ] = []),
q: "PASS",
}));
}
expect_stdout: "PASS"
node_version: ">=8.3.0"
}

0 comments on commit fbc5ecf

Please sign in to comment.