fix corner case in collapse_vars (#5748)

fixes #5747
This commit is contained in:
Alex Lam S.L
2022-11-30 21:59:22 +02:00
committed by GitHub
parent 2352909c3d
commit 548f0938e8
3 changed files with 70 additions and 8 deletions

View File

@@ -2474,8 +2474,10 @@ Compressor.prototype.compress = function(node) {
if (node instanceof AST_Scope) return node; if (node instanceof AST_Scope) return node;
// Scan computed keys, static fields & initializers in class // Scan computed keys, static fields & initializers in class
if (node instanceof AST_Class) { if (node instanceof AST_Class) {
if (node.name) node.name = node.name.transform(tt); var replace = can_replace;
if (!abort && node.extends) node.extends = node.extends.transform(tt); can_replace = false;
if (node.name) node.name.transform(tt);
if (!abort && node.extends) node.extends.transform(tt);
var fields = [], stats = []; var fields = [], stats = [];
for (var i = 0; !abort && i < node.properties.length; i++) { for (var i = 0; !abort && i < node.properties.length; i++) {
var prop = node.properties[i]; var prop = node.properties[i];
@@ -2491,9 +2493,9 @@ Compressor.prototype.compress = function(node) {
stats[i].transform(tt); stats[i].transform(tt);
} }
for (var i = 0; !abort && i < fields.length; i++) { for (var i = 0; !abort && i < fields.length; i++) {
var prop = fields[i]; fields[i].value.transform(tt);
prop.value = prop.value.transform(tt);
} }
can_replace = replace;
return node; return node;
} }
// Scan object only in a for-in/of statement // Scan object only in a for-in/of statement

View File

@@ -1165,9 +1165,9 @@ collapse_rhs_static: {
"use strict"; "use strict";
var a = "FAIL"; var a = "FAIL";
class A { class A {
static p = a = "PASS"; static p = "PASS";
} }
console.log(a); console.log(a = "PASS");
} }
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=12" node_version: ">=12"
@@ -3974,3 +3974,59 @@ issue_5735_2: {
] ]
node_version: ">=12" 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"
}

View File

@@ -539,7 +539,9 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
var before_iterations, diff_error_message, passes = 3, testcase_ast; var before_iterations, diff_error_message, passes = 3, testcase_ast;
for (var pass = 1; pass <= passes; pass++) { for (var pass = 1; pass <= passes; pass++) {
if (before_iterations !== testcase) { if (before_iterations !== testcase) {
testcase_ast = U.parse(testcase); testcase_ast = U.parse(testcase, {
module: minify_options.module,
});
if (diff_error_message === testcase) { if (diff_error_message === testcase) {
// only difference detected is in error message, so expose that and try again // only difference detected is in error message, so expose that and try again
testcase_ast.transform(new U.TreeTransformer(function(node, descend) { 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; testcase = code;
differs = diff; differs = diff;
} else { } else {
testcase_ast = U.parse(testcase); testcase_ast = U.parse(testcase, {
module: minify_options.module,
});
} }
} }
diff_error_message = null; diff_error_message = null;