aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/compress.js12
-rw-r--r--test/compress/optional-chains.js22
2 files changed, 29 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 1be74033..a418925a 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -7861,11 +7861,13 @@ merge(Compressor.prototype, {
var prop = this.property;
if (expr.may_throw_on_access(compressor)) {
if (!this.optional) return this;
- prop = prop.drop_side_effect_free(compressor);
- if (!prop) return expr.drop_side_effect_free(compressor, first_in_statement);
- var node = this.clone();
- node.property = prop;
- return node;
+ if (prop.has_side_effects(compressor)) {
+ prop = prop.drop_side_effect_free(compressor);
+ if (!prop) return expr.drop_side_effect_free(compressor, first_in_statement);
+ var node = this.clone();
+ node.property = prop;
+ return node;
+ }
}
expr = expr.drop_side_effect_free(compressor, first_in_statement);
if (!expr) return prop.drop_side_effect_free(compressor, first_in_statement);
diff --git a/test/compress/optional-chains.js b/test/compress/optional-chains.js
index ced841d2..be82b64e 100644
--- a/test/compress/optional-chains.js
+++ b/test/compress/optional-chains.js
@@ -305,3 +305,25 @@ issue_4947_2: {
expect_stdout: "PASS"
node_version: ">=14"
}
+
+issue_5039: {
+ options = {
+ ie8: true,
+ side_effects: true,
+ toplevel: true,
+ unused: true,
+ }
+ input: {
+ var a = a?.[function f() {
+ f;
+ a;
+ }];
+ console.log("PASS");
+ }
+ expect: {
+ (function f() {});
+ console.log("PASS");
+ }
+ expect_stdout: "PASS"
+ node_version: ">=14"
+}