avoid false positive in `--reduce-test` (#4648)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 13 Feb 2021 13:15:11 +0000 (13:15 +0000)
committerGitHub <noreply@github.com>
Sat, 13 Feb 2021 13:15:11 +0000 (21:15 +0800)
test/mocha/reduce.js
test/reduce.js
test/ufuzz/index.js

index e365331..587aa7b 100644 (file)
@@ -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"), {
index d5758cc..4d628aa 100644 (file)
@@ -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) {
@@ -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) {
@@ -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({
index 7f7da39..9df4192 100644 (file)
@@ -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)));
@@ -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