aboutsummaryrefslogtreecommitdiff
path: root/test/mozilla-ast.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/mozilla-ast.js')
-rw-r--r--test/mozilla-ast.js111
1 files changed, 72 insertions, 39 deletions
diff --git a/test/mozilla-ast.js b/test/mozilla-ast.js
index 0f6db3ac..c10c679a 100644
--- a/test/mozilla-ast.js
+++ b/test/mozilla-ast.js
@@ -5,23 +5,23 @@ var acorn = require("acorn");
var ufuzz = require("./ufuzz");
var UglifyJS = require("..");
-function try_beautify(code) {
- var beautified = UglifyJS.minify(code, {
+function beautify(ast) {
+ var beautified = UglifyJS.minify(ast, {
compress: false,
mangle: false,
output: {
beautify: true,
braces: true,
- }
+ },
+ });
+ if (beautified.error) return beautified;
+ return UglifyJS.minify(beautified.code, {
+ compress: false,
+ mangle: false,
+ output: {
+ ast: true,
+ },
});
- if (beautified.error) {
- console.log("// !!! beautify failed !!!");
- console.log(beautified.error.stack);
- console.log(code);
- } else {
- console.log("// (beautified)");
- console.log(beautified.code);
- }
}
function validate(ast) {
@@ -35,22 +35,55 @@ function validate(ast) {
return UglifyJS.minify(ast, {
compress: false,
mangle: false,
+ output: {
+ ast: true,
+ },
validate: true,
});
}
-function fuzzy(code) {
+function patch_import(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 && fuzzy(original) !== fuzzy(transformed.code)) {
+function equals(input, transformed) {
+ if (input.code === transformed.code) return true;
+ return patch_import(input.code) === patch_import(transformed.code);
+}
+
+function test(input, to_moz, description, skip_on_error, beautified) {
+ try {
+ var ast = UglifyJS.AST_Node.from_mozilla_ast(to_moz(input));
+ } catch (e) {
+ if (skip_on_error) return true;
+ console.log("//=============================================================");
+ console.log("//", description, "failed... round", round);
+ console.log(e);
+ console.log("// original code");
+ if (beautified === true) console.log("// (beautified)");
+ console.log(input.code);
+ return false;
+ }
+ var transformed = validate(ast);
+ if (transformed.error || !equals(input, transformed)) {
+ if (!beautified) {
+ beautified = beautify(input.ast);
+ if (!beautified.error) {
+ beautified.raw = beautified.code;
+ if (!test(beautified, to_moz, description, skip_on_error, true)) return false;
+ }
+ }
console.log("//=============================================================");
console.log("// !!!!!! Failed... round", round);
console.log("// original code");
- try_beautify(original);
+ if (beautified.error) {
+ console.log("// !!! beautify failed !!!");
+ console.log(beautified.error.stack);
+ } else if (beautified === true) {
+ console.log("// (beautified)");
+ }
+ console.log(input.raw);
console.log();
console.log();
console.log("//-------------------------------------------------------------");
@@ -58,7 +91,15 @@ function test(original, estree, description) {
if (transformed.error) {
console.log(transformed.error.stack);
} else {
- try_beautify(transformed.code);
+ beautified = beautify(transformed.ast);
+ if (beautified.error) {
+ console.log("// !!! beautify failed !!!");
+ console.log(beautified.error.stack);
+ console.log(transformed.code);
+ } else {
+ console.log("// (beautified)");
+ console.log(beautified.code);
+ }
}
console.log("!!!!!! Failed... round", round);
return false;
@@ -73,41 +114,33 @@ for (var round = 1; round <= num_iterations; round++) {
process.stdout.write(round + " of " + num_iterations + "\r");
var code = ufuzz.createTopLevelCode();
minify_options.forEach(function(options) {
- var input = options ? UglifyJS.minify(code, JSON.parse(options)).code : code;
- var uglified = UglifyJS.minify(input, {
+ var ok = true;
+ var input = UglifyJS.minify(options ? UglifyJS.minify(code, JSON.parse(options)).code : code, {
compress: false,
mangle: false,
output: {
ast: true,
},
});
- var ok = true;
- try {
- var estree = uglified.ast.to_mozilla_ast();
- } catch (e) {
+ input.raw = options ? input.code : code;
+ if (input.error) {
ok = false;
console.log("//=============================================================");
- console.log("// AST_Node.to_mozilla_ast() failed... round", round);
- console.log(e);
+ console.log("// minify() failed... round", round);
+ console.log(input.error);
console.log("// original code");
- console.log(input);
+ console.log(code);
}
- if (ok) ok = test(uglified.code, estree, "AST_Node.to_mozilla_ast()");
- if (ok) try {
- ok = test(uglified.code, acorn.parse(input, {
+ if (ok) ok = test(input, function(input) {
+ return input.ast.to_mozilla_ast();
+ }, "AST_Node.to_mozilla_ast()");
+ if (ok) ok = test(input, function(input) {
+ return acorn.parse(input.raw, {
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);
- }
- }
+ });
+ }, "acorn.parse()", !ufuzz.verbose);
if (!ok) process.exit(1);
});
}