Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix corner case in unused #5361

Merged
merged 1 commit into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 additions & 5 deletions lib/compress.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"
}