support dumping AST

closes #769
This commit is contained in:
alexlamsl 2017-05-08 03:37:49 +08:00
parent 4f8ca4626e
commit 21f4a6aea1
2 changed files with 39 additions and 3 deletions

View File

@ -95,8 +95,9 @@ The available options are:
`wrap_iife` Wrap IIFEs in parenthesis. Note: you may
want to disable `negate_iife` under
compressor options.
-o, --output <file> Output file (default STDOUT). Specify "spidermonkey"
to dump SpiderMonkey AST format (as JSON) to STDOUT.
-o, --output <file> Output file path (default STDOUT). Specify `ast` or
`spidermonkey` to write UglifyJS or SpiderMonkey AST
as JSON to STDOUT respectively.
--comments [filter] Preserve copyright comments in the output. By
default this works like Google Closure, keeping
JSDoc-style comments that contain "@license" or

View File

@ -107,6 +107,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 +217,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,
@ -352,3 +374,16 @@ function to_cache(key) {
}
return cache[key];
}
function skip_key(key) {
switch (key) {
case "cname":
case "enclosed":
case "parent_scope":
case "scope":
case "thedef":
case "uses_eval":
case "uses_with":
return true;
}
}