Skip to content

Commit

Permalink
fix corner case in unused (#5909)
Browse files Browse the repository at this point in the history
fixes #5908
  • Loading branch information
alexlamsl authored Aug 6, 2024
1 parent 8bbbc51 commit e9cf8de
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,14 @@ Compressor.prototype.compress = function(node) {
descend();
var node = this;
var expr = node.expression;
if (!node.optional && expr instanceof AST_SymbolRef) access(tw, expr.definition());
if (!node.optional) {
while (expr instanceof AST_Assign && expr.operator == "=") {
var lhs = expr.left;
if (lhs instanceof AST_SymbolRef) access(tw, lhs.definition());
expr = expr.right;
}
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
}
return true;
});
def(AST_For, function(tw, descend, compressor) {
Expand Down Expand Up @@ -1385,6 +1392,11 @@ Compressor.prototype.compress = function(node) {
pop(tw);
} else {
descend();
while (expr instanceof AST_Assign && expr.operator == "=") {
var lhs = expr.left;
if (lhs instanceof AST_SymbolRef) access(tw, lhs.definition());
expr = expr.right;
}
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
}
return true;
Expand Down Expand Up @@ -8056,6 +8068,7 @@ Compressor.prototype.compress = function(node) {
if (node.write_only === "p" && node.right.may_throw_on_access(compressor, true)) return;
var assign = props.assign;
if (assign) {
initializations.add(node_def.id, assign.left);
assign.write_only = true;
assign.walk(tw);
}
Expand Down
56 changes: 56 additions & 0 deletions test/compress/drop-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -3814,3 +3814,59 @@ issue_5533_drop_fargs: {
}
expect_stdout: "PASS"
}

issue_5908_1: {
options = {
collapse_vars: true,
pure_getters: "strict",
reduce_vars: true,
unused: true,
}
input: {
var a = function(b) {
function f() {}
b = f.prototype;
b.p = 42;
b.q = "PASS";
return f;
}();
console.log(a.prototype.q);
}
expect: {
var a = function(b) {
function f() {}
(b = f.prototype).p = 42;
b.q = "PASS";
return f;
}();
console.log(a.prototype.q);
}
expect_stdout: "PASS"
}

issue_5908_2: {
options = {
pure_getters: "strict",
reduce_vars: true,
unused: true,
}
input: {
var a = function(b) {
function f() {}
(b = f.prototype).p = 42;
b.q = "PASS";
return f;
}();
console.log(a.prototype.q);
}
expect: {
var a = function(b) {
function f() {}
(b = f.prototype).p = 42;
b.q = "PASS";
return f;
}();
console.log(a.prototype.q);
}
expect_stdout: "PASS"
}
2 changes: 1 addition & 1 deletion test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7139,7 +7139,7 @@ reduce_cross_reference_2_toplevel: {
reduce_cross_reference_3: {
options = {
collapse_vars: true,
passes: 3,
passes: 4,
pure_getters: "strict",
reduce_vars: true,
sequences: true,
Expand Down

0 comments on commit e9cf8de

Please sign in to comment.