diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-07-20 19:47:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-21 02:47:53 +0800 |
commit | 7fac839c62ef4881d3132e45aa9a18e30d7a3932 (patch) | |
tree | cc6ef4415ae7c9715a227b67ee3057ae9659b8ec | |
parent | 8926a2f327f9954e856161169a92a4190f0318e4 (diff) | |
download | tracifyjs-7fac839c62ef4881d3132e45aa9a18e30d7a3932.tar.gz tracifyjs-7fac839c62ef4881d3132e45aa9a18e30d7a3932.zip |
fix corner case in `merge_vars` (#5092)
fixes #5091
-rw-r--r-- | lib/compress.js | 23 | ||||
-rw-r--r-- | test/compress/optional-chains.js | 26 |
2 files changed, 44 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js index 6d1f93d4..5bb378e5 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5672,7 +5672,10 @@ merge(Compressor.prototype, { if (node instanceof AST_Call) { var exp = node.expression; var tail = exp.tail_node(); - if (!(tail instanceof AST_LambdaExpression)) return walk_node_with_expr(node); + if (!(tail instanceof AST_LambdaExpression)) { + descend(); + return mark_expression(exp); + } if (exp !== tail) exp.expressions.slice(0, -1).forEach(function(node) { node.walk(tw); }); @@ -5788,7 +5791,18 @@ merge(Compressor.prototype, { pop(); return true; } - if (node instanceof AST_Sub) return walk_node_with_expr(node); + if (node instanceof AST_Sub) { + var exp = node.expression; + if (node.optional) { + exp.walk(tw); + push(); + node.property.walk(tw); + pop(); + } else { + descend(); + } + return mark_expression(exp); + } if (node instanceof AST_Switch) { node.expression.walk(tw); var save = segment; @@ -5888,10 +5902,9 @@ merge(Compressor.prototype, { return true; } - function walk_node_with_expr(node) { - descend(); + function mark_expression(exp) { if (compressor.option("ie")) { - var sym = root_expr(node.expression); + var sym = root_expr(exp); if (sym instanceof AST_SymbolRef) sym.walk(tw); } return true; diff --git a/test/compress/optional-chains.js b/test/compress/optional-chains.js index 1b810bcf..6e06f49c 100644 --- a/test/compress/optional-chains.js +++ b/test/compress/optional-chains.js @@ -327,3 +327,29 @@ issue_5039: { expect_stdout: "PASS" node_version: ">=14" } + +issue_5091: { + options = { + merge_vars: true, + } + input: { + function f(a) { + var b = a.p; + var c; + b?.[c = "FAIL 2"]; + return b || c; + } + console.log(f("FAIL 1") || "PASS"); + } + expect: { + function f(b) { + var b = b.p; + var c; + b?.[c = "FAIL 2"]; + return b || c; + } + console.log(f("FAIL 1") || "PASS"); + } + expect_stdout: "PASS" + node_version: ">=14" +} |