From 4084948d3b74e2250dd6ffa0a87349670a1a9d37 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 1 Apr 2021 11:52:29 +0100 Subject: [PATCH] suppress invalid AST transform in `--reduce-test` (#4833) --- test/mocha/reduce.js | 35 +++++++++++++++++++++++++++++++++++ test/reduce.js | 29 +++++++++++++++++------------ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/test/mocha/reduce.js b/test/mocha/reduce.js index 64fea56d..8d7e72a5 100644 --- a/test/mocha/reduce.js +++ b/test/mocha/reduce.js @@ -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, []); + }); }); diff --git a/test/reduce.js b/test/reduce.js index 231e9586..4b3d3fe6 100644 --- a/test/reduce.js +++ b/test/reduce.js @@ -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; -- 2.34.1