support dumping AST (#1879)

Re-order `AST_Binary` properties to make dump more readable.

closes #769
This commit is contained in:
Alex Lam S.L
2017-05-08 06:23:01 +08:00
committed by GitHub
parent 4f8ca4626e
commit da295de82b
4 changed files with 45 additions and 6 deletions

View File

@@ -15,6 +15,7 @@ var path = require("path");
var program = require("commander");
var UglifyJS = require("../tools/node");
var skip_keys = [ "cname", "enclosed", "parent_scope", "scope", "thedef", "uses_eval", "uses_with" ];
var files = {};
var options = {
compress: false,
@@ -107,6 +108,12 @@ if (program.nameCache) {
}
}
}
if (program.output == "ast") {
options.output = {
ast: true,
code: false
};
}
if (program.parse) {
if (program.parse.acorn || program.parse.spidermonkey) {
if (program.sourceMap) fatal("ERROR: inline source map only works with built-in parser");
@@ -211,7 +218,23 @@ function run() {
}
fatal("ERROR: " + ex.message);
}
if (program.output == "spidermonkey") {
if (program.output == "ast") {
console.log(JSON.stringify(result.ast, function(key, value) {
if (skip_key(key)) return;
if (value instanceof UglifyJS.AST_Token) return;
if (value instanceof UglifyJS.Dictionary) return;
if (value instanceof UglifyJS.AST_Node) {
var result = {
_class: "AST_" + value.TYPE
};
value.CTOR.PROPS.forEach(function(prop) {
result[prop] = value[prop];
});
return result;
}
return value;
}, 2));
} else if (program.output == "spidermonkey") {
console.log(JSON.stringify(UglifyJS.minify(result.code, {
compress: false,
mangle: false,
@@ -237,8 +260,8 @@ function run() {
}
function fatal(message) {
console.error(message);
process.exit(1);
console.error(message);
process.exit(1);
}
// A file glob function that only supports "*" and "?" wildcards in the basename.
@@ -352,3 +375,7 @@ function to_cache(key) {
}
return cache[key];
}
function skip_key(key) {
return skip_keys.indexOf(key) >= 0;
}