From 2426657daa226d5cdab18bf40a668301dff0aa65 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 29 Jun 2022 21:09:53 +0100 Subject: [PATCH] fix corner case in `inline` (#5532) fixes #5531 --- lib/compress.js | 4 +- test/compress/classes.js | 110 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 9b28bc7d..76491e14 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -10784,7 +10784,9 @@ Compressor.prototype.compress = function(node) { }); child = scope; scope = compressor.parent(level++); - if (scope instanceof AST_DWLoop) { + if (scope instanceof AST_ClassField) { + if (!scope.static) return false; + } else if (scope instanceof AST_DWLoop) { in_loop = []; } else if (scope instanceof AST_For) { if (scope.init === child) continue; diff --git a/test/compress/classes.js b/test/compress/classes.js index 3ea7e283..860293b3 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -3331,3 +3331,113 @@ issue_5512: { expect_stdout: "PASS" node_version: ">=16" } + +issue_5531_1: { + options = { + inline: true, + toplevel: true, + } + input: { + class A { + p = function() { + var a = function f() { + if (!a) + console.log("foo"); + return 42; + }(a++); + }(); + } + new A(); + new A(); + } + expect: { + class A { + p = function() { + var a = function f() { + if (!a) + console.log("foo"); + return 42; + }(a++); + }(); + } + new A(); + new A(); + } + expect_stdout: [ + "foo", + "foo", + ] + node_version: ">=12" +} + +issue_5531_2: { + options = { + inline: true, + toplevel: true, + } + input: { + class A { + static p = function() { + var a = function f() { + if (!a) + console.log("foo"); + return 42; + }(a++); + }(); + } + new A(); + new A(); + } + expect: { + class A { + static p = (a = function f() { + if (!a) + console.log("foo"); + return 42; + }(a++), void 0); + } + var a; + new A(); + new A(); + } + expect_stdout: "foo" + node_version: ">=12" +} + +issue_5531_3: { + options = { + inline: true, + } + input: { + class A { + static { + (function() { + var a = function f() { + if (!a) + console.log("foo"); + return 42; + }(a++); + })(); + } + } + new A(); + new A(); + } + expect: { + class A { + static { + a = function f() { + if (!a) + console.log("foo"); + return 42; + }(a++), + void 0; + var a; + } + } + new A(); + new A(); + } + expect_stdout: "foo" + node_version: ">=16" +}