Skip to content

Commit

Permalink
improve SymbolDef info in --output ast (#2778)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
kzc authored and alexlamsl committed Jan 13, 2018
1 parent 460218a commit 2cab348
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
27 changes: 10 additions & 17 deletions bin/uglifyjs
Expand Up @@ -227,28 +227,15 @@ function run() {
result.ast.figure_out_scope({});
}
print(JSON.stringify(result.ast, function(key, value) {
switch (key) {
if (value) switch (key) {
case "thedef":
if (typeof value == "object" && typeof value.id == "number") {
return value.id;
}
return;
return symdef(value);
case "enclosed":
return value.map(function(sym){
return sym.id;
});
return value.length ? value.map(symdef) : undefined;
case "variables":
case "functions":
case "globals":
if (value && value.size()) {
var ret = {};
value.each(function(val, key) {
// key/val inverted for readability.
ret[val.id] = key;
});
return ret;
}
return;
return value.size() ? value.map(symdef) : undefined;
}
if (skip_key(key)) return;
if (value instanceof UglifyJS.AST_Token) return;
Expand Down Expand Up @@ -403,6 +390,12 @@ function skip_key(key) {
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) {
var lines = [];
var padding = "";
Expand Down
2 changes: 1 addition & 1 deletion lib/scope.js
Expand Up @@ -57,7 +57,7 @@ function SymbolDef(scope, orig, init) {
this.id = SymbolDef.next_id++;
};

SymbolDef.next_id = 1e6;
SymbolDef.next_id = 1;

SymbolDef.prototype = {
unmangleable: function(options) {
Expand Down

0 comments on commit 2cab348

Please sign in to comment.