diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-04-26 04:23:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-26 04:23:52 +0800 |
commit | 324587f76980114e030c59165a60bd285ca05be0 (patch) | |
tree | 1f609c396c01110faeb115151e45784c146a90fc /test/mozilla-ast.js | |
parent | 80efaa2f331b2c3f3ec35218d1086830a39bec93 (diff) | |
download | tracifyjs-324587f76980114e030c59165a60bd285ca05be0.tar.gz tracifyjs-324587f76980114e030c59165a60bd285ca05be0.zip |
upgrade AST<->ESTree translation (#4870)
fixes #968
Diffstat (limited to 'test/mozilla-ast.js')
-rw-r--r-- | test/mozilla-ast.js | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/test/mozilla-ast.js b/test/mozilla-ast.js index 5f5ad541..0f6db3ac 100644 --- a/test/mozilla-ast.js +++ b/test/mozilla-ast.js @@ -11,7 +11,7 @@ function try_beautify(code) { mangle: false, output: { beautify: true, - braces: true + braces: true, } }); if (beautified.error) { @@ -35,12 +35,18 @@ function validate(ast) { return UglifyJS.minify(ast, { compress: false, mangle: false, + validate: true, }); } +function fuzzy(code) { + return code.replace(/\bimport\s*\{\s*\}\s*from\s*(['"])/g, "import$1") + .replace(/\b(import\b.*?)\s*,\s*\{\s*\}\s*(from\s*['"])/g, "$1 $2"); +} + function test(original, estree, description) { var transformed = validate(UglifyJS.AST_Node.from_mozilla_ast(estree)); - if (transformed.error || original !== transformed.code) { + if (transformed.error || original !== transformed.code && fuzzy(original) !== fuzzy(transformed.code)) { console.log("//============================================================="); console.log("// !!!!!! Failed... round", round); console.log("// original code"); @@ -72,19 +78,36 @@ for (var round = 1; round <= num_iterations; round++) { compress: false, mangle: false, output: { - ast: true - } + ast: true, + }, }); - var ok = test(uglified.code, uglified.ast.to_mozilla_ast(), "AST_Node.to_mozilla_ast()"); + var ok = true; try { - ok = test(uglified.code, acorn.parse(input), "acorn.parse()") && ok; + var estree = uglified.ast.to_mozilla_ast(); } catch (e) { + ok = false; console.log("//============================================================="); - console.log("// acorn parser failed... round", round); + console.log("// AST_Node.to_mozilla_ast() failed... round", round); console.log(e); console.log("// original code"); console.log(input); } + if (ok) ok = test(uglified.code, estree, "AST_Node.to_mozilla_ast()"); + if (ok) try { + ok = test(uglified.code, acorn.parse(input, { + ecmaVersion: "latest", + locations: true, + sourceType: "module", + }), "acorn.parse()"); + } catch (e) { + if (ufuzz.verbose) { + console.log("//============================================================="); + console.log("// acorn parser failed... round", round); + console.log(e); + console.log("// original code"); + console.log(input); + } + } if (!ok) process.exit(1); }); } |