From 63f16e4616099752fdf6e16aa25dfb2b9dcea3d0 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 17 May 2022 19:41:05 +0100 Subject: [PATCH] fix corner case in `merge_vars` (#5452) fixes #5451 --- lib/compress.js | 7 +++++-- test/compress/const.js | 6 +++--- test/compress/let.js | 6 +++--- test/compress/merge_vars.js | 22 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 4235617d..55c8f854 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6183,8 +6183,11 @@ Compressor.prototype.compress = function(node) { return true; } if (node instanceof AST_Scope) { - var parent = tw.parent(); - var in_iife = parent && parent.TYPE == "Call" && parent.expression === node; + var in_iife = false; + if (node instanceof AST_LambdaExpression && !node.name) { + var parent = tw.parent(); + in_iife = parent && parent.TYPE == "Call" && parent.expression === node; + } if (!in_iife) { push(); segment.block = node; diff --git a/test/compress/const.js b/test/compress/const.js index 9601c588..8178747d 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -184,12 +184,12 @@ merge_vars_2: { expect: { var a = 0; 1 && --a, - a = function f() { + b = function f() { const c = a && f; c.var += 0; }(), - void console.log(a); - var a; + void console.log(b); + var b; } expect_stdout: "undefined" } diff --git a/test/compress/let.js b/test/compress/let.js index 34308ab8..d0261fc4 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -237,12 +237,12 @@ merge_vars_2: { "use strict"; var a = 0; 1 && --a, - a = function f() { + b = function f() { let c = a && f; c.var += 0; }(), - void console.log(a); - var a; + void console.log(b); + var b; } expect_stdout: "undefined" node_version: ">=4" diff --git a/test/compress/merge_vars.js b/test/compress/merge_vars.js index 67635436..d6e95dec 100644 --- a/test/compress/merge_vars.js +++ b/test/compress/merge_vars.js @@ -3732,3 +3732,25 @@ issue_5420: { } expect_stdout: "PASS" } + +issue_5451: { + options = { + merge_vars: true, + toplevel: true, + } + input: { + A = 1; + var a = 1, b; + console.log(function f() { + return a-- && f(b = A, b); + }()); + } + expect: { + A = 1; + var a = 1, b; + console.log(function f() { + return a-- && f(b = A, b); + }()); + } + expect_stdout: "0" +}