From 8c0424d4d7ed2650ce7841b420bd724d2d762204 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Thu, 8 Jul 2021 10:55:56 +0800 Subject: [PATCH] fix corner case in `reduce_vars` fixes #5061 --- lib/compress.js | 1 + test/compress/functions.js | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/compress.js b/lib/compress.js index 90261d0a..dbfc2e75 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -889,6 +889,7 @@ merge(Compressor.prototype, { } else { right.parent_scope.resolve().fn_defs.push(right); right.safe_ids = null; + if (!node.write_only) mark_fn_def(tw, ld, right); } return true; } else if (scan) { diff --git a/test/compress/functions.js b/test/compress/functions.js index 6d41a265..7c6ebe03 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -6346,3 +6346,55 @@ issue_5046: { } expect_stdout: "PASS" } + +issue_5061_1: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + } + input: { + var f, a = 1; + (f = function() { + console.log(a ? "foo" : "bar"); + })(); + f(a = 0); + } + expect: { + var f, a = 1; + (f = function() { + console.log(a ? "foo" : "bar"); + })(); + f(a = 0); + } + expect_stdout: [ + "foo", + "bar", + ] +} + +issue_5061_2: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var f, a = 1; + (f = function() { + console.log(a ? "foo" : "bar"); + })(); + f(a = 0); + } + expect: { + var f, a = 1; + (f = function() { + console.log(a ? "foo" : "bar"); + })(); + f(a = 0); + } + expect_stdout: [ + "foo", + "bar", + ] +}