doc (WIP)
This commit is contained in:
17
lib/scope.js
17
lib/scope.js
@@ -1,5 +1,12 @@
|
||||
AST_Scope.DEFMETHOD("figure_out_scope", function(){
|
||||
// step 1: handle definitions
|
||||
// This does what ast_add_scope did in UglifyJS v1.
|
||||
//
|
||||
// Part of it could be done at parse time, but it would complicate
|
||||
// the parser (and it's already kinda complex). It's also worth
|
||||
// having it separated because we might need to call it multiple
|
||||
// times on the same tree.
|
||||
|
||||
// pass 1: setup scope chaining and handle definitions
|
||||
var scope = null;
|
||||
var tw = new TreeWalker(function(node, descend){
|
||||
if (node instanceof AST_Scope) {
|
||||
@@ -41,7 +48,8 @@ AST_Scope.DEFMETHOD("figure_out_scope", function(){
|
||||
}
|
||||
});
|
||||
this.walk(tw);
|
||||
// step 2: find back references and eval/with
|
||||
|
||||
// pass 2: find back references and eval/with
|
||||
var tw = new TreeWalker(function(node){
|
||||
if (node instanceof AST_LabelRef) {
|
||||
var sym = node.scope.find_label(node);
|
||||
@@ -62,26 +70,31 @@ AST_Scope.DEFMETHOD("figure_out_scope", function(){
|
||||
});
|
||||
this.walk(tw);
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("find_variable", function(name){
|
||||
if (name instanceof AST_Symbol) name = name.name;
|
||||
return this.variables[name] ||
|
||||
(this.name && this.name.name == name && this.name) ||
|
||||
(this.parent_scope && this.parent_scope.find_variable(name));
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("find_label", function(name){
|
||||
if (name instanceof AST_Symbol) name = name.name;
|
||||
return this.labels[name];
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("def_function", function(symbol){
|
||||
this.def_variable(symbol);
|
||||
this.functions[symbol.name] = symbol;
|
||||
symbol.scope = this;
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
||||
this.variables[symbol.name] = symbol;
|
||||
delete this.functions[symbol.name];
|
||||
symbol.scope = this;
|
||||
});
|
||||
|
||||
AST_Scope.DEFMETHOD("def_label", function(symbol){
|
||||
this.labels[symbol.name] = symbol;
|
||||
symbol.scope = this;
|
||||
|
||||
Reference in New Issue
Block a user