Skip to content

Commit 95d3ede

Browse files
authored
fix corner cases in pure_getters & reduce_vars (#5859)
fixes #5856 fixes #5857 fixes #5858
1 parent 8195a66 commit 95d3ede

4 files changed

Lines changed: 82 additions & 17 deletions

File tree

lib/compress.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,8 +1261,9 @@ Compressor.prototype.compress = function(node) {
12611261
});
12621262
def(AST_Dot, function(tw, descend) {
12631263
descend();
1264-
var expr = this.expression;
1265-
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
1264+
var node = this;
1265+
var expr = node.expression;
1266+
if (!node.optional && expr instanceof AST_SymbolRef) access(tw, expr.definition());
12661267
return true;
12671268
});
12681269
def(AST_For, function(tw, descend, compressor) {
@@ -1362,15 +1363,18 @@ Compressor.prototype.compress = function(node) {
13621363
pop_scope(tw, fn);
13631364
return true;
13641365
});
1365-
def(AST_Sub, function(tw) {
1366+
def(AST_Sub, function(tw, descend) {
13661367
var node = this;
1367-
if (!node.optional) return;
13681368
var expr = node.expression;
1369-
expr.walk(tw);
1370-
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
1371-
push(tw, true);
1372-
node.property.walk(tw);
1373-
pop(tw);
1369+
if (node.optional) {
1370+
expr.walk(tw);
1371+
push(tw, true);
1372+
node.property.walk(tw);
1373+
pop(tw);
1374+
} else {
1375+
descend();
1376+
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
1377+
}
13741378
return true;
13751379
});
13761380
def(AST_Switch, function(tw, descend, compressor) {
@@ -8020,8 +8024,14 @@ Compressor.prototype.compress = function(node) {
80208024
prop.walk(tw);
80218025
});
80228026
if (node instanceof AST_Assign) {
8023-
var right = get_rhs(node), shared = false;
8024-
if (init && node.write_only === true && !right.has_side_effects(compressor)) {
8027+
var fixed = sym.fixed_value();
8028+
var right = get_rhs(node);
8029+
var safe = fixed && fixed.is_constant();
8030+
var shared = false;
8031+
if (init
8032+
&& node.write_only === true
8033+
&& (safe || node.left === sym || right.equals(sym))
8034+
&& !right.has_side_effects(compressor)) {
80258035
initializations.add(node_def.id, right);
80268036
} else {
80278037
right.walk(tw);
@@ -8031,11 +8041,8 @@ Compressor.prototype.compress = function(node) {
80318041
if (!node.write_only || shared) {
80328042
verify_safe_usage(node_def, sym, value_modified[node_def.id]);
80338043
}
8034-
} else {
8035-
var fixed = sym.fixed_value();
8036-
if (!fixed || !fixed.is_constant()) {
8037-
verify_safe_usage(node_def, value_read[node_def.id], true);
8038-
}
8044+
} else if (!safe) {
8045+
verify_safe_usage(node_def, value_read[node_def.id], true);
80398046
}
80408047
}
80418048
if (track_assigns(node_def, sym) && is_lhs(sym, node) !== sym) add_assigns(node_def, sym);

test/compress/issue-5614.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ record_update: {
2626
currying: {
2727
options = {
2828
inline: true,
29-
passes: 2,
29+
passes: 3,
3030
pure_getters: "strict",
3131
reduce_funcs: true,
3232
reduce_vars: true,

test/compress/optional-chains.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,33 @@ issue_5292_sub_pure_getters_strict: {
617617
]
618618
node_version: ">=14"
619619
}
620+
621+
issue_5856: {
622+
options = {
623+
pure_getters: "strict",
624+
reduce_vars: true,
625+
side_effects: true,
626+
}
627+
input: {
628+
try {
629+
var a;
630+
a?.p;
631+
a.q;
632+
console.log("FAIL");
633+
} catch (e) {
634+
console.log("PASS");
635+
}
636+
}
637+
expect: {
638+
try {
639+
var a;
640+
a?.p;
641+
a.q;
642+
console.log("FAIL");
643+
} catch (e) {
644+
console.log("PASS");
645+
}
646+
}
647+
expect_stdout: "PASS"
648+
node_version: ">=14"
649+
}

test/compress/pure_getters.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,3 +1687,31 @@ issue_4939: {
16871687
}
16881688
expect_stdout: "PASS"
16891689
}
1690+
1691+
issue_5856: {
1692+
options = {
1693+
pure_getters: true,
1694+
reduce_vars: true,
1695+
side_effects: true,
1696+
unused: true,
1697+
}
1698+
input: {
1699+
var a = [ "FAIL", "PASS" ];
1700+
(function(b) {
1701+
var c = b[0];
1702+
b[0] = b[1];
1703+
b[1] = c;
1704+
})(a);
1705+
console.log(a[0]);
1706+
}
1707+
expect: {
1708+
var a = [ "FAIL", "PASS" ];
1709+
(function(b) {
1710+
var c = b[0];
1711+
b[0] = b[1];
1712+
b[1] = c;
1713+
})(a);
1714+
console.log(a[0]);
1715+
}
1716+
expect_stdout: "PASS"
1717+
}

0 commit comments

Comments
 (0)