diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-05-03 23:19:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-04 06:19:25 +0800 |
commit | ce3c35fa8b4c8e9a777d19220915880cdd45f3fb (patch) | |
tree | 544204934cebeefaaf20a2235b1bf2a3b544f37e | |
parent | 5d9224deb893c383e9a2c2b11c5926db96bd7a1f (diff) | |
download | tracifyjs-ce3c35fa8b4c8e9a777d19220915880cdd45f3fb.tar.gz tracifyjs-ce3c35fa8b4c8e9a777d19220915880cdd45f3fb.zip |
fix corner case in `unused` (#4907)
fixes #4906
-rw-r--r-- | lib/compress.js | 21 | ||||
-rw-r--r-- | test/compress/optional-chains.js | 17 |
2 files changed, 32 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js index d58b68bc..815797ca 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -7671,12 +7671,21 @@ merge(Compressor.prototype, { return expressions === this.expressions ? this : make_sequence(this, expressions); }); def(AST_Sub, function(compressor, first_in_statement) { - if (this.expression.may_throw_on_access(compressor)) return this; - var expression = this.expression.drop_side_effect_free(compressor, first_in_statement); - if (!expression) return this.property.drop_side_effect_free(compressor, first_in_statement); - var property = this.property.drop_side_effect_free(compressor); - if (!property) return expression; - return make_sequence(this, [ expression, property ]); + var expr = this.expression; + 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; + } + 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 ]); }); def(AST_SymbolRef, function(compressor) { return this.is_declared(compressor) && can_drop_symbol(this, compressor) ? null : this; diff --git a/test/compress/optional-chains.js b/test/compress/optional-chains.js index f55136ec..fbee6743 100644 --- a/test/compress/optional-chains.js +++ b/test/compress/optional-chains.js @@ -194,3 +194,20 @@ trim_2: { expect_stdout: "PASS" node_version: ">=14" } + +issue_4906: { + options = { + toplevel: true, + unused: true, + } + input: { + do { + var a = a?.[42]; + } while (console.log("PASS")); + } + expect: { + do {} while (console.log("PASS")); + } + expect_stdout: "PASS" + node_version: ">=14" +} |