Skip to content

Commit e9cf8de

Browse files
authoredAug 6, 2024··
fix corner case in unused (#5909)
fixes #5908
1 parent 8bbbc51 commit e9cf8de

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
 

‎lib/compress.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,14 @@ Compressor.prototype.compress = function(node) {
12751275
descend();
12761276
var node = this;
12771277
var expr = node.expression;
1278-
if (!node.optional && expr instanceof AST_SymbolRef) access(tw, expr.definition());
1278+
if (!node.optional) {
1279+
while (expr instanceof AST_Assign && expr.operator == "=") {
1280+
var lhs = expr.left;
1281+
if (lhs instanceof AST_SymbolRef) access(tw, lhs.definition());
1282+
expr = expr.right;
1283+
}
1284+
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
1285+
}
12791286
return true;
12801287
});
12811288
def(AST_For, function(tw, descend, compressor) {
@@ -1385,6 +1392,11 @@ Compressor.prototype.compress = function(node) {
13851392
pop(tw);
13861393
} else {
13871394
descend();
1395+
while (expr instanceof AST_Assign && expr.operator == "=") {
1396+
var lhs = expr.left;
1397+
if (lhs instanceof AST_SymbolRef) access(tw, lhs.definition());
1398+
expr = expr.right;
1399+
}
13881400
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
13891401
}
13901402
return true;
@@ -8056,6 +8068,7 @@ Compressor.prototype.compress = function(node) {
80568068
if (node.write_only === "p" && node.right.may_throw_on_access(compressor, true)) return;
80578069
var assign = props.assign;
80588070
if (assign) {
8071+
initializations.add(node_def.id, assign.left);
80598072
assign.write_only = true;
80608073
assign.walk(tw);
80618074
}

‎test/compress/drop-unused.js

+56
Original file line numberDiff line numberDiff line change
@@ -3814,3 +3814,59 @@ issue_5533_drop_fargs: {
38143814
}
38153815
expect_stdout: "PASS"
38163816
}
3817+
3818+
issue_5908_1: {
3819+
options = {
3820+
collapse_vars: true,
3821+
pure_getters: "strict",
3822+
reduce_vars: true,
3823+
unused: true,
3824+
}
3825+
input: {
3826+
var a = function(b) {
3827+
function f() {}
3828+
b = f.prototype;
3829+
b.p = 42;
3830+
b.q = "PASS";
3831+
return f;
3832+
}();
3833+
console.log(a.prototype.q);
3834+
}
3835+
expect: {
3836+
var a = function(b) {
3837+
function f() {}
3838+
(b = f.prototype).p = 42;
3839+
b.q = "PASS";
3840+
return f;
3841+
}();
3842+
console.log(a.prototype.q);
3843+
}
3844+
expect_stdout: "PASS"
3845+
}
3846+
3847+
issue_5908_2: {
3848+
options = {
3849+
pure_getters: "strict",
3850+
reduce_vars: true,
3851+
unused: true,
3852+
}
3853+
input: {
3854+
var a = function(b) {
3855+
function f() {}
3856+
(b = f.prototype).p = 42;
3857+
b.q = "PASS";
3858+
return f;
3859+
}();
3860+
console.log(a.prototype.q);
3861+
}
3862+
expect: {
3863+
var a = function(b) {
3864+
function f() {}
3865+
(b = f.prototype).p = 42;
3866+
b.q = "PASS";
3867+
return f;
3868+
}();
3869+
console.log(a.prototype.q);
3870+
}
3871+
expect_stdout: "PASS"
3872+
}

‎test/compress/functions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7139,7 +7139,7 @@ reduce_cross_reference_2_toplevel: {
71397139
reduce_cross_reference_3: {
71407140
options = {
71417141
collapse_vars: true,
7142-
passes: 3,
7142+
passes: 4,
71437143
pure_getters: "strict",
71447144
reduce_vars: true,
71457145
sequences: true,

0 commit comments

Comments
 (0)
Please sign in to comment.