2014-08-03 17:48:59 +00:00
|
|
|
// Testing UglifyJS <-> SpiderMonkey AST conversion
|
2017-05-14 18:37:53 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
var acorn = require("acorn");
|
|
|
|
|
var ufuzz = require("./ufuzz");
|
|
|
|
|
var UglifyJS = require("..");
|
|
|
|
|
|
|
|
|
|
function try_beautify(code) {
|
|
|
|
|
var beautified = UglifyJS.minify(code, {
|
|
|
|
|
compress: false,
|
|
|
|
|
mangle: false,
|
|
|
|
|
output: {
|
|
|
|
|
beautify: true,
|
2021-04-25 20:23:52 +00:00
|
|
|
braces: true,
|
2014-08-03 17:48:59 +00:00
|
|
|
}
|
|
|
|
|
});
|
2017-05-14 18:37:53 +00:00
|
|
|
if (beautified.error) {
|
|
|
|
|
console.log("// !!! beautify failed !!!");
|
|
|
|
|
console.log(beautified.error.stack);
|
|
|
|
|
console.log(code);
|
|
|
|
|
} else {
|
|
|
|
|
console.log("// (beautified)");
|
|
|
|
|
console.log(beautified.code);
|
|
|
|
|
}
|
2014-08-03 17:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-09 21:25:44 +00:00
|
|
|
function validate(ast) {
|
|
|
|
|
try {
|
|
|
|
|
ast.walk(new UglifyJS.TreeWalker(function(node) {
|
|
|
|
|
node.validate();
|
|
|
|
|
}));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return { error: e };
|
|
|
|
|
}
|
|
|
|
|
return UglifyJS.minify(ast, {
|
2017-05-14 18:37:53 +00:00
|
|
|
compress: false,
|
2020-05-09 21:25:44 +00:00
|
|
|
mangle: false,
|
2021-04-25 20:23:52 +00:00
|
|
|
validate: true,
|
2017-05-14 18:37:53 +00:00
|
|
|
});
|
2020-05-09 21:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-25 20:23:52 +00:00
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 21:25:44 +00:00
|
|
|
function test(original, estree, description) {
|
|
|
|
|
var transformed = validate(UglifyJS.AST_Node.from_mozilla_ast(estree));
|
2021-04-25 20:23:52 +00:00
|
|
|
if (transformed.error || original !== transformed.code && fuzzy(original) !== fuzzy(transformed.code)) {
|
2017-05-14 18:37:53 +00:00
|
|
|
console.log("//=============================================================");
|
|
|
|
|
console.log("// !!!!!! Failed... round", round);
|
|
|
|
|
console.log("// original code");
|
|
|
|
|
try_beautify(original);
|
|
|
|
|
console.log();
|
|
|
|
|
console.log();
|
|
|
|
|
console.log("//-------------------------------------------------------------");
|
|
|
|
|
console.log("//", description);
|
|
|
|
|
if (transformed.error) {
|
|
|
|
|
console.log(transformed.error.stack);
|
|
|
|
|
} else {
|
|
|
|
|
try_beautify(transformed.code);
|
2014-08-03 17:48:59 +00:00
|
|
|
}
|
2017-05-14 18:37:53 +00:00
|
|
|
console.log("!!!!!! Failed... round", round);
|
2020-03-02 11:38:30 +00:00
|
|
|
return false;
|
2014-08-03 17:48:59 +00:00
|
|
|
}
|
2020-03-02 11:38:30 +00:00
|
|
|
return true;
|
2017-05-14 18:37:53 +00:00
|
|
|
}
|
2014-08-03 17:48:59 +00:00
|
|
|
|
2017-05-14 18:37:53 +00:00
|
|
|
var num_iterations = ufuzz.num_iterations;
|
2020-03-02 11:38:30 +00:00
|
|
|
var minify_options = require("./ufuzz/options.json").map(JSON.stringify);
|
|
|
|
|
minify_options.unshift(null);
|
2017-05-14 18:37:53 +00:00
|
|
|
for (var round = 1; round <= num_iterations; round++) {
|
|
|
|
|
process.stdout.write(round + " of " + num_iterations + "\r");
|
|
|
|
|
var code = ufuzz.createTopLevelCode();
|
2020-03-02 11:38:30 +00:00
|
|
|
minify_options.forEach(function(options) {
|
|
|
|
|
var input = options ? UglifyJS.minify(code, JSON.parse(options)).code : code;
|
|
|
|
|
var uglified = UglifyJS.minify(input, {
|
|
|
|
|
compress: false,
|
|
|
|
|
mangle: false,
|
|
|
|
|
output: {
|
2021-04-25 20:23:52 +00:00
|
|
|
ast: true,
|
|
|
|
|
},
|
2020-03-02 11:38:30 +00:00
|
|
|
});
|
2021-04-25 20:23:52 +00:00
|
|
|
var ok = true;
|
2020-03-02 11:38:30 +00:00
|
|
|
try {
|
2021-04-25 20:23:52 +00:00
|
|
|
var estree = uglified.ast.to_mozilla_ast();
|
2020-03-02 11:38:30 +00:00
|
|
|
} catch (e) {
|
2021-04-25 20:23:52 +00:00
|
|
|
ok = false;
|
2020-03-02 11:38:30 +00:00
|
|
|
console.log("//=============================================================");
|
2021-04-25 20:23:52 +00:00
|
|
|
console.log("// AST_Node.to_mozilla_ast() failed... round", round);
|
2020-03-02 11:38:30 +00:00
|
|
|
console.log(e);
|
|
|
|
|
console.log("// original code");
|
|
|
|
|
console.log(input);
|
2017-05-14 18:37:53 +00:00
|
|
|
}
|
2021-04-25 20:23:52 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-02 11:38:30 +00:00
|
|
|
if (!ok) process.exit(1);
|
2017-05-14 18:37:53 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
console.log();
|