From b20d711857b81cc07250eae32a202091c64533d0 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Fri, 6 May 2022 00:42:51 +0800 Subject: [PATCH] fix corner case in `reduce_vars` fixes #5434 --- lib/compress.js | 2 +- test/compress/reduce_vars.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 6cb155bf..63ae427a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -649,7 +649,7 @@ Compressor.prototype.compress = function(node) { } if (!HOP(tw.safe_ids, def.id)) { if (!safe) return false; - if (safe.read) { + if (safe.read || tw.in_loop) { var scope = tw.find_parent(AST_BlockScope); if (scope instanceof AST_Class) return false; if (def.scope.resolve() !== scope.resolve()) return false; diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index a7360434..348d2c9e 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -7861,3 +7861,38 @@ issue_5324: { } expect_stdout: "NaN" } + +issue_5434: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + console.log(function(a) { + for (var i = 0; i < 2; i++) { + var b = "FAIL"; + f && f(); + a = b; + var f = function() { + b = "PASS"; + }; + } + return a; + }()); + } + expect: { + console.log(function(a) { + for (var i = 0; i < 2; i++) { + var b = "FAIL"; + f && f(); + a = b; + var f = function() { + b = "PASS"; + }; + } + return a; + }()); + } + expect_stdout: "PASS" +}