patch export default within sandbox correctly (#5346)

fixes #5345
This commit is contained in:
Alex Lam S.L
2022-02-10 08:07:40 +00:00
committed by GitHub
parent b6c72c84d4
commit 33c163f648
4 changed files with 569 additions and 538 deletions

View File

@@ -0,0 +1,17 @@
var unused;
export default class {
____11111() {
a, b, c, d, e;
f, g, h, i, j;
k, l, m, n, o;
p, q, r, s, t;
u, v, w, x, y, z;
A, B, C, D, E;
F, G, H, I, J;
K, L, M, N, O;
P, Q, R, S, T;
U, V, W, X, Y, Z;
$, _;
unused;
}
}

View File

@@ -434,4 +434,18 @@ describe("test/reduce.js", function() {
"// }", "// }",
].join("\n")); ].join("\n"));
}); });
it("Should transform `export default` correctly", function() {
var result = reduce_test(read("test/input/reduce/export_default.js"), {
compress: false,
toplevel: true,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"// Can't reproduce test failure",
"// minify options: {",
'// "compress": false,',
'// "toplevel": true',
"// }",
].join("\n"));
});
}); });

View File

@@ -50,37 +50,34 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
test_for_diff = test_minify; test_for_diff = test_minify;
differs = test_for_diff(testcase, minify_options, result_cache, max_timeout); differs = test_for_diff(testcase, minify_options, result_cache, max_timeout);
} }
if (!differs) {
// same stdout result produced when minified // same stdout result produced when minified
return { if (!differs) return {
code: [ code: [
"// Can't reproduce test failure", "// Can't reproduce test failure",
"// minify options: " + to_comment(minify_options_json) "// minify options: " + to_comment(minify_options_json)
].join("\n"), ].join("\n"),
warnings: warnings, warnings: warnings,
}; };
} else if (differs.timed_out) { if (differs.timed_out) return {
return {
code: [ code: [
"// Can't reproduce test failure within " + max_timeout + "ms", "// Can't reproduce test failure within " + max_timeout + "ms",
"// minify options: " + to_comment(minify_options_json) "// minify options: " + to_comment(minify_options_json)
].join("\n"), ].join("\n"),
warnings: warnings, warnings: warnings,
}; };
} else if (differs.error) { if (differs.error) {
differs.warnings = warnings; differs.warnings = warnings;
return differs; return differs;
} else if (sandbox.is_error(differs.unminified_result) }
if (sandbox.is_error(differs.unminified_result)
&& sandbox.is_error(differs.minified_result) && sandbox.is_error(differs.minified_result)
&& differs.unminified_result.name == differs.minified_result.name) { && differs.unminified_result.name == differs.minified_result.name) return {
return {
code: [ code: [
"// No differences except in error message", "// No differences except in error message",
"// minify options: " + to_comment(minify_options_json) "// minify options: " + to_comment(minify_options_json)
].join("\n"), ].join("\n"),
warnings: warnings, warnings: warnings,
}; };
} else {
max_timeout = Math.min(100 * differs.elapsed, max_timeout); max_timeout = Math.min(100 * differs.elapsed, max_timeout);
// Replace expressions with constants that will be parsed into // Replace expressions with constants that will be parsed into
// AST_Nodes as required. Each AST_Node has its own permutation count, // AST_Nodes as required. Each AST_Node has its own permutation count,
@@ -513,9 +510,10 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
} }
}); });
var diff_error_message; var before_iterations, diff_error_message, passes = 3, testcase_ast;
for (var pass = 1; pass <= 3; ++pass) { for (var pass = 1; pass <= passes; pass++) {
var testcase_ast = U.parse(testcase); if (before_iterations !== testcase) {
testcase_ast = U.parse(testcase);
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) {
@@ -546,8 +544,9 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
node.start = JSON.parse(JSON.stringify(node.start)); node.start = JSON.parse(JSON.stringify(node.start));
node.start._permute = 0; node.start._permute = 0;
})); }));
var before_iterations = testcase; before_iterations = testcase;
for (var c = 0; c < max_iterations; ++c) { }
for (var c = 0; c < max_iterations; c++) {
if (verbose && c % (pass == 1 ? 25 : 100) == 0) { if (verbose && c % (pass == 1 ? 25 : 100) == 0) {
log("// reduce test pass " + pass + ", iteration " + c + ": " + testcase.length + " bytes"); log("// reduce test pass " + pass + ", iteration " + c + ": " + testcase.length + " bytes");
} }
@@ -589,10 +588,9 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
} }
} }
} }
if (before_iterations === testcase) break; if (before_iterations !== testcase) continue;
if (verbose) { if (c < max_iterations) break;
log("// reduce test pass " + pass + ": " + testcase.length + " bytes"); passes++;
}
} }
var beautified = U.minify(testcase, { var beautified = U.minify(testcase, {
compress: false, compress: false,
@@ -639,7 +637,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
testcase.code += lines.join("\n"); testcase.code += lines.join("\n");
testcase.warnings = warnings; testcase.warnings = warnings;
return testcase; return testcase;
}
}; };
function to_comment(value) { function to_comment(value) {

View File

@@ -56,7 +56,10 @@ exports.patch_module_statements = function(code) {
code = code.replace(/\bexport(?:\s*\{[^{}]*}\s*?(?:$|\n|;)|\s+default\b(?:\s*(\(|\{|class\s*\{|class\s+(?=extends\b)|(?:async\s+)?function\s*(?:\*\s*)?\())?|\b)/g, function(match, header) { code = code.replace(/\bexport(?:\s*\{[^{}]*}\s*?(?:$|\n|;)|\s+default\b(?:\s*(\(|\{|class\s*\{|class\s+(?=extends\b)|(?:async\s+)?function\s*(?:\*\s*)?\())?|\b)/g, function(match, header) {
if (!header) return ""; if (!header) return "";
if (header.length == 1) return "0, " + header; if (header.length == 1) return "0, " + header;
return header.slice(0, -1) + " _" + ++count + header.slice(-1); do {
var name = "_export_default_" + ++count;
} while (code.indexOf(name) >= 0);
return header.slice(0, -1) + " " + name + header.slice(-1);
}).replace(/\bimport\.meta\b/g, function() { }).replace(/\bimport\.meta\b/g, function() {
return '({ url: "https://example.com/path/index.html" })'; return '({ url: "https://example.com/path/index.html" })';
}).replace(/\bimport\b(?:\s*([^\s('"][^('"]*)\bfrom\b)?\s*(['"]).*?\2(?:$|\n|;)/g, function(match, symbols) { }).replace(/\bimport\b(?:\s*([^\s('"][^('"]*)\bfrom\b)?\s*(['"]).*?\2(?:$|\n|;)/g, function(match, symbols) {