From a437a61518f98bd4131d6713b3dbbfe5025ecdb7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 16 Jan 2023 15:30:45 +0200 Subject: [PATCH] fix corner case in `reduce_vars` (#5778) fixes #5777 --- lib/compress.js | 10 +---- test/compress/reduce_vars.js | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index c96f3de535d..4e3469ec668 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -624,15 +624,9 @@ Compressor.prototype.compress = function(node) { } function pop_scope(tw, scope) { - var fn_defs = scope.fn_defs; - var tangled = scope.may_call_this === return_true ? fn_defs : fn_defs.filter(function(fn) { - if (fn.safe_ids === false) return true; - fn.safe_ids = tw.safe_ids; - walk_fn_def(tw, fn); - return false; - }); pop(tw); - tangled.forEach(function(fn) { + var fn_defs = scope.fn_defs; + fn_defs.forEach(function(fn) { fn.safe_ids = tw.safe_ids; walk_fn_def(tw, fn); }); diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 15187846bb3..674d98c312d 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -8148,3 +8148,77 @@ issue_5730_3: { } expect_stdout: "PASS" } + +issue_5777_1: { + options = { + reduce_vars: true, + unused: true, + } + input: { + function f() { + (function(a) { + function g() { + h(); + } + g(); + a = function() {}; + function h() { + console.log(a); + } + })("PASS"); + } + f(); + } + expect: { + function f() { + (function(a) { + (function() { + h(); + })(); + a = function() {}; + function h() { + console.log(a); + } + })("PASS"); + } + f(); + } + expect_stdout: "PASS" +} + +issue_5777_2: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f(a) { + (function() { + function g() { + h(); + } + g(); + a = function() {}; + function h() { + console.log(a); + } + })(); + } + f("PASS"); + } + expect: { + (function(a) { + (function() { + (function() { + h(); + })(); + a = function() {}; + function h() { + console.log(a); + } + })(); + })("PASS"); + } + expect_stdout: "PASS" +}