Skip to content

Commit

Permalink
fix corner case in collapse_vars (#5748)
Browse files Browse the repository at this point in the history
fixes #5747
  • Loading branch information
alexlamsl committed Nov 30, 2022
1 parent 2352909 commit 548f093
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
Expand Down
60 changes: 58 additions & 2 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
8 changes: 6 additions & 2 deletions test/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 548f093

Please sign in to comment.