Skip to content

Commit

Permalink
fix corner case in collapse_vars
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jun 12, 2022
1 parent b6f250f commit db39f53
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit db39f53

Please sign in to comment.