Skip to content

Commit

Permalink
avoid false positive in --reduce-test (#4648)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Feb 13, 2021
1 parent 83197ff commit 94e8944
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
33 changes: 33 additions & 0 deletions test/mocha/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,39 @@ describe("test/reduce.js", function() {
"// }",
]).join("\n"));
});
it("Should maintain block-scope for const & let", function() {
if (semver.satisfies(process.version, "<4")) return;
var code = [
'"use strict";',
"",
"L: for (let a = (1 - .8).toString(); ;) {",
" if (!console.log(a)) {",
" break L;",
" }",
"}",
].join("\n");
var result = reduce_test(code, {
compress: {
unsafe_math: true,
},
mangle: false,
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"// (beautified)",
code,
"// output: 0.19999999999999996",
"// ",
"// minify: 0.2",
"// ",
"// options: {",
'// "compress": {',
'// "unsafe_math": true',
'// },',
'// "mangle": false',
"// }",
].join("\n"));
});
it("Should handle corner cases when intermediate case differs only in Error.message", function() {
if (semver.satisfies(process.version, "<=0.10")) return;
var result = reduce_test(read("test/input/reduce/diff_error.js"), {
Expand Down
11 changes: 9 additions & 2 deletions test/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
node.start._permute += step;
if (expr && (expr !== node.body || !has_loopcontrol(expr, node, parent))) {
CHANGED = true;
return to_statement(expr);
return to_statement_init(expr);
}
}
else if (node instanceof U.AST_ForEnumeration) {
Expand All @@ -322,7 +322,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
node.start._permute += step;
if (expr) {
CHANGED = true;
return to_statement(expr);
return to_statement_init(expr);
}
}
else if (node instanceof U.AST_If) {
Expand Down Expand Up @@ -695,6 +695,13 @@ function to_statement(node) {
});
}

function to_statement_init(node) {
return node instanceof U.AST_Const || node instanceof U.AST_Let ? new U.AST_BlockStatement({
body: [ node ],
start: {},
}) : to_statement(node);;
}

function wrap_with_console_log(node) {
// wrap with console.log()
return new U.AST_Call({
Expand Down
21 changes: 11 additions & 10 deletions test/ufuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2139,6 +2139,17 @@ for (var round = 1; round <= num_iterations; round++) {
ok = sandbox.same_stdout(original_result, uglify_result);
// ignore v8 parser bug
if (!ok && bug_async_arrow_rest(uglify_result)) ok = true;
// handle difference caused by time-outs
if (!ok && errored && is_error_timeout(original_result)) {
if (is_error_timeout(uglify_result)) {
// ignore difference in error message
ok = true;
} else {
// ignore spurious time-outs
if (!orig_result[toplevel ? 3 : 2]) orig_result[toplevel ? 3 : 2] = sandbox.run_code(original_code, toplevel, 10000);
ok = sandbox.same_stdout(orig_result[toplevel ? 3 : 2], uglify_result);
}
}
// ignore declaration order of global variables
if (!ok && !toplevel) {
ok = sandbox.same_stdout(sandbox.run_code(sort_globals(original_code)), sandbox.run_code(sort_globals(uglify_code)));
Expand All @@ -2157,16 +2168,6 @@ for (var round = 1; round <= num_iterations; round++) {
}
// ignore difference in error message caused by Temporal Dead Zone
if (!ok && errored && uglify_result.name == "ReferenceError" && original_result.name == "ReferenceError") ok = true;
if (!ok && errored && is_error_timeout(original_result)) {
if (is_error_timeout(uglify_result)) {
// ignore difference in error message
ok = true;
} else {
// ignore spurious time-outs
if (!orig_result[toplevel ? 3 : 2]) orig_result[toplevel ? 3 : 2] = sandbox.run_code(original_code, toplevel, 10000);
ok = sandbox.same_stdout(orig_result[toplevel ? 3 : 2], uglify_result);
}
}
// ignore difference in error message caused by `in`
if (!ok && errored && is_error_in(uglify_result) && is_error_in(original_result)) ok = true;
// ignore difference in error message caused by spread syntax
Expand Down

0 comments on commit 94e8944

Please sign in to comment.