support dumping AST (#1879)
Re-order `AST_Binary` properties to make dump more readable. closes #769
This commit is contained in:
@@ -95,8 +95,9 @@ The available options are:
|
|||||||
`wrap_iife` Wrap IIFEs in parenthesis. Note: you may
|
`wrap_iife` Wrap IIFEs in parenthesis. Note: you may
|
||||||
want to disable `negate_iife` under
|
want to disable `negate_iife` under
|
||||||
compressor options.
|
compressor options.
|
||||||
-o, --output <file> Output file (default STDOUT). Specify "spidermonkey"
|
-o, --output <file> Output file path (default STDOUT). Specify `ast` or
|
||||||
to dump SpiderMonkey AST format (as JSON) to STDOUT.
|
`spidermonkey` to write UglifyJS or SpiderMonkey AST
|
||||||
|
as JSON to STDOUT respectively.
|
||||||
--comments [filter] Preserve copyright comments in the output. By
|
--comments [filter] Preserve copyright comments in the output. By
|
||||||
default this works like Google Closure, keeping
|
default this works like Google Closure, keeping
|
||||||
JSDoc-style comments that contain "@license" or
|
JSDoc-style comments that contain "@license" or
|
||||||
|
|||||||
29
bin/uglifyjs
29
bin/uglifyjs
@@ -15,6 +15,7 @@ var path = require("path");
|
|||||||
var program = require("commander");
|
var program = require("commander");
|
||||||
var UglifyJS = require("../tools/node");
|
var UglifyJS = require("../tools/node");
|
||||||
|
|
||||||
|
var skip_keys = [ "cname", "enclosed", "parent_scope", "scope", "thedef", "uses_eval", "uses_with" ];
|
||||||
var files = {};
|
var files = {};
|
||||||
var options = {
|
var options = {
|
||||||
compress: false,
|
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) {
|
||||||
if (program.parse.acorn || program.parse.spidermonkey) {
|
if (program.parse.acorn || program.parse.spidermonkey) {
|
||||||
if (program.sourceMap) fatal("ERROR: inline source map only works with built-in parser");
|
if (program.sourceMap) fatal("ERROR: inline source map only works with built-in parser");
|
||||||
@@ -211,7 +218,23 @@ function run() {
|
|||||||
}
|
}
|
||||||
fatal("ERROR: " + ex.message);
|
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, {
|
console.log(JSON.stringify(UglifyJS.minify(result.code, {
|
||||||
compress: false,
|
compress: false,
|
||||||
mangle: false,
|
mangle: false,
|
||||||
@@ -352,3 +375,7 @@ function to_cache(key) {
|
|||||||
}
|
}
|
||||||
return cache[key];
|
return cache[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function skip_key(key) {
|
||||||
|
return skip_keys.indexOf(key) >= 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -619,7 +619,7 @@ var AST_UnaryPostfix = DEFNODE("UnaryPostfix", null, {
|
|||||||
$documentation: "Unary postfix expression, i.e. `i++`"
|
$documentation: "Unary postfix expression, i.e. `i++`"
|
||||||
}, AST_Unary);
|
}, AST_Unary);
|
||||||
|
|
||||||
var AST_Binary = DEFNODE("Binary", "left operator right", {
|
var AST_Binary = DEFNODE("Binary", "operator left right", {
|
||||||
$documentation: "Binary expression, i.e. `a + b`",
|
$documentation: "Binary expression, i.e. `a + b`",
|
||||||
$propdoc: {
|
$propdoc: {
|
||||||
left: "[AST_Node] left-hand side expression",
|
left: "[AST_Node] left-hand side expression",
|
||||||
|
|||||||
@@ -509,4 +509,15 @@ describe("bin/uglifyjs", function () {
|
|||||||
return JSON.stringify(map).replace(/"/g, '\\"');
|
return JSON.stringify(map).replace(/"/g, '\\"');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
it("Should dump AST as JSON", function(done) {
|
||||||
|
var command = uglifyjscmd + " test/input/global_defs/simple.js -mco ast";
|
||||||
|
exec(command, function (err, stdout) {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
|
var ast = JSON.parse(stdout);
|
||||||
|
assert.strictEqual(ast._class, "AST_Toplevel");
|
||||||
|
assert.ok(Array.isArray(ast.body));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user