suppress invalid AST transform in `--reduce-test` (#4833)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 1 Apr 2021 10:52:29 +0000 (11:52 +0100)
committerGitHub <noreply@github.com>
Thu, 1 Apr 2021 10:52:29 +0000 (18:52 +0800)
test/mocha/reduce.js
test/reduce.js

index 64fea56..8d7e72a 100644 (file)
@@ -183,6 +183,24 @@ describe("test/reduce.js", function() {
             "// }",
         ].join("\n"));
     });
+    it("Should reduce `for (const ... in ...)` without invalid intermediate AST", function() {
+        if (semver.satisfies(process.version, "<4")) return;
+        var code = [
+            "var a = 0;",
+            "",
+            "for (const b in [ 1, 2, 3 ]) {",
+            "    a = +a + 1 - .2;",
+            "    console.log(a);",
+            "}",
+        ].join("\n");
+        var result = reduce_test(code, {
+            compress: {
+                unsafe_math: true,
+            },
+        });
+        if (result.error) throw result.error;
+        assert.deepEqual(result.warnings, []);
+    });
     it("Should reduce infinite loops with reasonable performance", function() {
         if (semver.satisfies(process.version, "<=0.10")) return;
         this.timeout(120000);
@@ -379,4 +397,21 @@ describe("test/reduce.js", function() {
             "// }",
         ].join("\n"));
     });
+    it("Should reduce object with method syntax without invalid intermediate AST", function() {
+        if (semver.satisfies(process.version, "<4")) return;
+        var code = [
+            "console.log({",
+            "    f() {",
+            "        return 1 - .8;",
+            "    },",
+            "}.f());",
+        ].join("\n");
+        var result = reduce_test(code, {
+            compress: {
+                unsafe_math: true,
+            },
+        });
+        if (result.error) throw result.error;
+        assert.deepEqual(result.warnings, []);
+    });
 });
index 231e958..4b3d3fe 100644 (file)
@@ -316,10 +316,11 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
                 var expr;
                 switch ((node.start._permute * steps | 0) % 3) {
                   case 0:
-                    if (!(node.init instanceof U.AST_Definitions
-                        && node.init.definitions[0].name instanceof U.AST_Destructured)) {
-                        expr = node.init;
+                    if (node.init instanceof U.AST_Definitions) {
+                        if (node.init instanceof U.AST_Const) break;
+                        if (node.init.definitions[0].name instanceof U.AST_Destructured) break;
                     }
+                    expr = node.init;
                     break;
                   case 1:
                     expr = node.object;
@@ -484,23 +485,27 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
             CHANGED = true;
             return newNode;
         }, function(node, in_list) {
-            if (node instanceof U.AST_Sequence) {
+            if (node instanceof U.AST_Definitions) {
+                // remove empty var statement
+                if (node.definitions.length == 0) return in_list ? List.skip : new U.AST_EmptyStatement({
+                    start: {},
+                });
+            } else if (node instanceof U.AST_ObjectMethod) {
+                if (!/Function$/.test(node.value.TYPE)) return new U.AST_ObjectKeyVal({
+                    key: node.key,
+                    value: node.value,
+                    start: {},
+                });
+            } else if (node instanceof U.AST_Sequence) {
                 // expand single-element sequence
                 if (node.expressions.length == 1) return node.expressions[0];
-            }
-            else if (node instanceof U.AST_Try) {
+            } else if (node instanceof U.AST_Try) {
                 // expand orphaned try block
                 if (!node.bcatch && !node.bfinally) return new U.AST_BlockStatement({
                     body: node.body,
                     start: {},
                 });
             }
-            else if (node instanceof U.AST_Definitions) {
-                // remove empty var statement
-                if (node.definitions.length == 0) return in_list ? List.skip : new U.AST_EmptyStatement({
-                    start: {},
-                });
-            }
         });
 
         var diff_error_message;