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"));
});
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;
differs = test_for_diff(testcase, minify_options, result_cache, max_timeout);
}
if (!differs) {
// same stdout result produced when minified
return {
if (!differs) return {
code: [
"// Can't reproduce test failure",
"// minify options: " + to_comment(minify_options_json)
].join("\n"),
warnings: warnings,
};
} else if (differs.timed_out) {
return {
if (differs.timed_out) return {
code: [
"// Can't reproduce test failure within " + max_timeout + "ms",
"// minify options: " + to_comment(minify_options_json)
].join("\n"),
warnings: warnings,
};
} else if (differs.error) {
if (differs.error) {
differs.warnings = warnings;
return differs;
} else if (sandbox.is_error(differs.unminified_result)
}
if (sandbox.is_error(differs.unminified_result)
&& sandbox.is_error(differs.minified_result)
&& differs.unminified_result.name == differs.minified_result.name) {
return {
&& differs.unminified_result.name == differs.minified_result.name) return {
code: [
"// No differences except in error message",
"// minify options: " + to_comment(minify_options_json)
].join("\n"),
warnings: warnings,
};
} else {
max_timeout = Math.min(100 * differs.elapsed, max_timeout);
// Replace expressions with constants that will be parsed into
// 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;
for (var pass = 1; pass <= 3; ++pass) {
var testcase_ast = U.parse(testcase);
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);
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) {
@@ -546,8 +544,9 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
node.start = JSON.parse(JSON.stringify(node.start));
node.start._permute = 0;
}));
var before_iterations = testcase;
for (var c = 0; c < max_iterations; ++c) {
before_iterations = testcase;
}
for (var c = 0; c < max_iterations; c++) {
if (verbose && c % (pass == 1 ? 25 : 100) == 0) {
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 (verbose) {
log("// reduce test pass " + pass + ": " + testcase.length + " bytes");
}
if (before_iterations !== testcase) continue;
if (c < max_iterations) break;
passes++;
}
var beautified = U.minify(testcase, {
compress: false,
@@ -639,7 +637,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
testcase.code += lines.join("\n");
testcase.warnings = warnings;
return testcase;
}
};
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) {
if (!header) return "";
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() {
return '({ url: "https://example.com/path/index.html" })';
}).replace(/\bimport\b(?:\s*([^\s('"][^('"]*)\bfrom\b)?\s*(['"]).*?\2(?:$|\n|;)/g, function(match, symbols) {