From 37d3e4feaa1feae6a68dd6069484e464ae94a91b Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 22 Sep 2022 16:27:06 +0100 Subject: [PATCH] fix corner case in `if_return` (#5675) --- lib/compress.js | 5 ++--- test/compress/if_return.js | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 214dc72b31..cd8280f58d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1828,12 +1828,11 @@ Compressor.prototype.compress = function(node) { return wrap ? make_sequence(orig, [ make_node(AST_Number, orig, { value: 0 }), val ]) : val; } - function merge_expression(base, target, scope) { + function merge_expression(base, target) { var fixed_by_id = new Dictionary(); base.walk(new TreeWalker(function(node) { if (!(node instanceof AST_SymbolRef)) return; var def = node.definition(); - if (scope && def.scope.resolve() !== scope) return; var fixed = node.fixed; if (!fixed || !fixed_by_id.has(def.id)) { fixed_by_id.set(def.id, fixed); @@ -3836,7 +3835,7 @@ Compressor.prototype.compress = function(node) { case 2: value = value.tail_node(); } - merge_expression(value, jump.value, scope); + merge_expression(value, jump.value); } function next_index(i) { diff --git a/test/compress/if_return.js b/test/compress/if_return.js index ead1ddf39d..d53f6072ce 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -1834,7 +1834,7 @@ switch_return_5: { ] } -merged_references: { +merged_references_1: { options = { if_return: true, reduce_vars: true, @@ -1861,6 +1861,36 @@ merged_references: { expect_stdout: "PASS" } +merged_references_2: { + options = { + if_return: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + A = "PASS"; + var a; + console.log(function(b) { + if (a = b) + return console && a; + a = FAIL; + return console && a; + }(A)); + } + expect: { + A = "PASS"; + var a; + console.log(function(b) { + if (a = b); + else + a = FAIL; + return console && a; + }(A)); + } + expect_stdout: "PASS" +} + issue_5583: { options = { conditionals: true,