From 4382bfe848f36165459b59ddcb605976c0055187 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 13 Jun 2022 00:55:15 +0100 Subject: [PATCH] fix corner case in `collapse_vars` (#5513) fixes #5512 --- lib/compress.js | 27 ++++++++++++++++----------- test/compress/classes.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 2494d1d55f9..59f579f007a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2351,20 +2351,25 @@ Compressor.prototype.compress = function(node) { // Scan computed keys, static fields & initializers in class if (node instanceof AST_Class) { if (node.name) node.name = node.name.transform(tt); - if (node.extends) node.extends = node.extends.transform(tt); - node.properties.reduce(function(props, prop) { + if (!abort && node.extends) node.extends = node.extends.transform(tt); + var fields = [], stats = []; + for (var i = 0; !abort && i < node.properties.length; i++) { + var prop = node.properties[i]; if (prop.key instanceof AST_Node) prop.key = prop.key.transform(tt); - if (prop.static) { - if (prop instanceof AST_ClassField) { - if (prop.value) props.push(prop); - } else if (prop instanceof AST_ClassInit) { - props.unshift(prop); - } + if (!prop.static) continue; + if (prop instanceof AST_ClassField) { + if (prop.value) fields.push(prop); + } else if (prop instanceof AST_ClassInit) { + [].push.apply(stats, prop.value.body); } - return props; - }, []).forEach(function(prop) { + } + for (var i = 0; !abort && i < stats.length; i++) { + stats[i].transform(tt); + } + for (var i = 0; !abort && i < fields.length; i++) { + var prop = fields[i]; prop.value = prop.value.transform(tt); - }); + } return node; } // Scan object only in a for-in/of statement diff --git a/test/compress/classes.js b/test/compress/classes.js index e0ce3e10872..2dc7e3b0f5d 100644 --- a/test/compress/classes.js +++ b/test/compress/classes.js @@ -3300,3 +3300,33 @@ issue_5504: { expect_stdout: "undefined" node_version: ">=12" } + +issue_5512: { + options = { + collapse_vars: true, + } + input: { + "use strict"; + a = "PASS"; + class A { + static { + console.log(a); + } + static p = "PASS"; + } + var a; + } + expect: { + "use strict"; + a = "PASS"; + class A { + static { + console.log(a); + } + static p = "PASS"; + } + var a; + } + expect_stdout: "PASS" + node_version: ">=16" +}