From 548f0938e8fd608efff7d5923db64db424914764 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 30 Nov 2022 21:59:22 +0200 Subject: [PATCH] fix corner case in `collapse_vars` (#5748) fixes #5747 --- lib/compress.js | 10 ++++--- test/compress/classes.js | 60 ++++++++++++++++++++++++++++++++++++++-- test/reduce.js | 8 ++++-- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index c6a82674..e939044c 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2474,8 +2474,10 @@ Compressor.prototype.compress = function(node) { if (node instanceof AST_Scope) return node; // Scan computed keys, static fields & initializers in class if (node instanceof AST_Class) { - if (node.name) node.name = node.name.transform(tt); - if (!abort && node.extends) node.extends = node.extends.transform(tt); + var replace = can_replace; + can_replace = false; + if (node.name) node.name.transform(tt); + if (!abort && node.extends) node.extends.transform(tt); var fields = [], stats = []; for (var i = 0; !abort && i < node.properties.length; i++) { var prop = node.properties[i]; @@ -2491,9 +2493,9 @@ Compressor.prototype.compress = function(node) { stats[i].transform(tt); } for (var i = 0; !abort && i < fields.length; i++) { - var prop = fields[i]; - prop.value = prop.value.transform(tt); + fields[i].value.transform(tt); } + can_replace = replace; return node; } // Scan object only in a for-in/of statement diff --git a/test/compress/classes.js b/test/compress/classes.js index feee4030..19954445 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -1165,9 +1165,9 @@ collapse_rhs_static: { "use strict"; var a = "FAIL"; class A { - static p = a = "PASS"; + static p = "PASS"; } - console.log(a); + console.log(a = "PASS"); } expect_stdout: "PASS" node_version: ">=12" @@ -3974,3 +3974,59 @@ issue_5735_2: { ] node_version: ">=12" } + +issue_5747_1: { + options = { + collapse_vars: true, + } + input: { + "use strict"; + (async function() { + var a = await 42; + class A { + static P = a && console.log(typeof this); + } + })(); + } + expect: { + "use strict"; + (async function() { + var a = await 42; + class A { + static P = a && console.log(typeof this); + } + })(); + } + expect_stdout: "function" + node_version: ">=12" +} + +issue_5747_2: { + options = { + collapse_vars: true, + } + input: { + "use strict"; + (async function() { + var a = await 42; + class A { + static { + a && console.log(typeof this); + } + } + })(); + } + expect: { + "use strict"; + (async function() { + var a = await 42; + class A { + static { + a && console.log(typeof this); + } + } + })(); + } + expect_stdout: "function" + node_version: ">=16" +} diff --git a/test/reduce.js b/test/reduce.js index ba953ca0..beb05f59 100644 --- a/test/reduce.js +++ b/test/reduce.js @@ -539,7 +539,9 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options) var before_iterations, diff_error_message, passes = 3, testcase_ast; for (var pass = 1; pass <= passes; pass++) { if (before_iterations !== testcase) { - testcase_ast = U.parse(testcase); + testcase_ast = U.parse(testcase, { + module: minify_options.module, + }); if (diff_error_message === testcase) { // only difference detected is in error message, so expose that and try again testcase_ast.transform(new U.TreeTransformer(function(node, descend) { @@ -561,7 +563,9 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options) testcase = code; differs = diff; } else { - testcase_ast = U.parse(testcase); + testcase_ast = U.parse(testcase, { + module: minify_options.module, + }); } } diff_error_message = null;