Skip to content

Commit

Permalink
fix corner case in side_effects (#5902)
Browse files Browse the repository at this point in the history
fixes #5900
  • Loading branch information
alexlamsl authored Aug 1, 2024
1 parent d82b7de commit 15c8edb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
19 changes: 13 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9345,14 +9345,21 @@ Compressor.prototype.compress = function(node) {
return expressions === this.expressions ? this : make_sequence(this, expressions);
});
def(AST_Sub, function(compressor, first_in_statement) {
var expr = this.expression;
if (expr.may_throw_on_access(compressor)) return this;
var prop = this.property;
expr = expr.drop_side_effect_free(compressor, first_in_statement);
if (!expr) return prop.drop_side_effect_free(compressor, first_in_statement);
var self = this;
var expr = self.expression;
if (expr.may_throw_on_access(compressor)) return self;
var prop = self.property;
var optional = self.optional;
if (!optional) {
expr = expr.drop_side_effect_free(compressor, first_in_statement);
if (!expr) return prop.drop_side_effect_free(compressor, first_in_statement);
}
prop = prop.drop_side_effect_free(compressor);
if (!prop) return expr;
return make_sequence(this, [ expr, prop ]);
if (!optional) return make_sequence(self, [ expr, prop ]);
self = self.clone();
self.property = prop;
return self;
});
def(AST_SymbolRef, function(compressor) {
return this.is_declared(compressor) && can_drop_symbol(this, compressor) ? null : this;
Expand Down
24 changes: 23 additions & 1 deletion test/compress/optional-chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ issue_5292_sub_pure_getters: {
console.log("foo");
}
};
console.log("bar");
o?.[console.log("bar")];
}
}

Expand Down Expand Up @@ -647,3 +647,25 @@ issue_5856: {
expect_stdout: "PASS"
node_version: ">=14"
}

issue_5900: {
options = {
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
}
input: {
var a = 42, b = "PASS";
a[a = null];
a?.[b = "FAIL"];
console.log(b);
}
expect: {
var a = 42, b = "PASS";
a[a = null];
a?.[b = "FAIL"];
console.log(b);
}
expect_stdout: "PASS"
node_version: ">=14"
}

0 comments on commit 15c8edb

Please sign in to comment.