improve SymbolDef info in --output ast (#2778)

* SymbolDef info (a.k.a. `thedef`) is now represented as a string containing `"ID name [mangled_name]"`. 
* Enhance display of `globals`, `variables`, `functions` and `enclosed`.
* `SymbolDef.next_id` starts at `1` and the `id` is adjusted for `-o ast` display.
This commit is contained in:
kzc
2018-01-13 12:40:51 -05:00
committed by Alex Lam S.L
parent 460218a3f8
commit 2cab348341
2 changed files with 11 additions and 18 deletions

View File

@@ -227,28 +227,15 @@ function run() {
result.ast.figure_out_scope({}); result.ast.figure_out_scope({});
} }
print(JSON.stringify(result.ast, function(key, value) { print(JSON.stringify(result.ast, function(key, value) {
switch (key) { if (value) switch (key) {
case "thedef": case "thedef":
if (typeof value == "object" && typeof value.id == "number") { return symdef(value);
return value.id;
}
return;
case "enclosed": case "enclosed":
return value.map(function(sym){ return value.length ? value.map(symdef) : undefined;
return sym.id;
});
case "variables": case "variables":
case "functions": case "functions":
case "globals": case "globals":
if (value && value.size()) { return value.size() ? value.map(symdef) : undefined;
var ret = {};
value.each(function(val, key) {
// key/val inverted for readability.
ret[val.id] = key;
});
return ret;
}
return;
} }
if (skip_key(key)) return; if (skip_key(key)) return;
if (value instanceof UglifyJS.AST_Token) return; if (value instanceof UglifyJS.AST_Token) return;
@@ -403,6 +390,12 @@ function skip_key(key) {
return skip_keys.indexOf(key) >= 0; return skip_keys.indexOf(key) >= 0;
} }
function symdef(def) {
var ret = (1e6 + def.id) + " " + def.name;
if (def.mangled_name) ret += " " + def.mangled_name;
return ret;
}
function format_object(obj) { function format_object(obj) {
var lines = []; var lines = [];
var padding = ""; var padding = "";

View File

@@ -57,7 +57,7 @@ function SymbolDef(scope, orig, init) {
this.id = SymbolDef.next_id++; this.id = SymbolDef.next_id++;
}; };
SymbolDef.next_id = 1e6; SymbolDef.next_id = 1;
SymbolDef.prototype = { SymbolDef.prototype = {
unmangleable: function(options) { unmangleable: function(options) {