Skip to content

Commit

Permalink
fix corner case in reduce_vars
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jun 20, 2024
1 parent 8195a66 commit 27c591d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,9 @@ Compressor.prototype.compress = function(node) {
});
def(AST_Dot, function(tw, descend) {
descend();
var expr = this.expression;
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
var node = this;
var expr = node.expression;
if (!node.optional && expr instanceof AST_SymbolRef) access(tw, expr.definition());
return true;
});
def(AST_For, function(tw, descend, compressor) {
Expand Down Expand Up @@ -1362,15 +1363,18 @@ Compressor.prototype.compress = function(node) {
pop_scope(tw, fn);
return true;
});
def(AST_Sub, function(tw) {
def(AST_Sub, function(tw, descend) {
var node = this;
if (!node.optional) return;
var expr = node.expression;
expr.walk(tw);
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
push(tw, true);
node.property.walk(tw);
pop(tw);
if (node.optional) {
expr.walk(tw);
push(tw, true);
node.property.walk(tw);
pop(tw);
} else {
descend();
if (expr instanceof AST_SymbolRef) access(tw, expr.definition());
}
return true;
});
def(AST_Switch, function(tw, descend, compressor) {
Expand Down
30 changes: 30 additions & 0 deletions test/compress/optional-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,33 @@ issue_5292_sub_pure_getters_strict: {
]
node_version: ">=14"
}

issue_5856: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
try {
var a;
a?.p;
a.q;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect: {
try {
var a;
a?.p;
a.q;
console.log("FAIL");
} catch (e) {
console.log("PASS");
}
}
expect_stdout: "PASS"
node_version: ">=14"
}

0 comments on commit 27c591d

Please sign in to comment.