From f473b4db383ae069f05bbdafbcdfea0af0d20177 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sat, 8 Jan 2022 01:46:21 +0000 Subject: [PATCH] fix corner case in `collapse_vars` (#5278) fixes #5277 --- lib/compress.js | 4 ++-- test/compress/collapse_vars.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 01b1845f..24d0d040 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1982,7 +1982,7 @@ Compressor.prototype.compress = function(node) { abort = true; folded = make_node(AST_Binary, candidate, { operator: compound, - left: lhs.fixed ? lhs.fixed.to_binary() : lhs, + left: lhs.fixed && lhs.definition().fixed ? lhs.fixed.to_binary() : lhs, right: rvalue, }); } @@ -2040,7 +2040,7 @@ Compressor.prototype.compress = function(node) { }); if (candidate instanceof AST_UnaryPostfix) return make_node(AST_UnaryPrefix, candidate, { operator: candidate.operator, - expression: lhs.fixed ? lhs.fixed.to_prefix() : lhs, + expression: lhs.fixed && lhs.definition().fixed ? lhs.fixed.to_prefix() : lhs, }); if (candidate instanceof AST_VarDef) { var def = candidate.name.definition(); diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 40ba3bb2..2c547274 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -9790,3 +9790,23 @@ issue_5276: { } expect_stdout: "PASS" } + +issue_5277: { + options = { + collapse_vars: true, + reduce_vars: true, + unused: true, + } + input: { + console.log(function() { + var a = function() { + a += null; + a -= 42; + }; + }()); + } + expect: { + console.log(function() {}()); + } + expect_stdout: "undefined" +}