aboutsummaryrefslogtreecommitdiff
path: root/lib/scope.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/scope.js')
-rw-r--r--lib/scope.js72
1 files changed, 47 insertions, 25 deletions
diff --git a/lib/scope.js b/lib/scope.js
index d16ac8a0..c6d655ee 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -1,4 +1,4 @@
-AST_Scope.DEFMETHOD("figure_out_scope", function(){
+AST_Toplevel.DEFMETHOD("figure_out_scope", function(){
// This does what ast_add_scope did in UglifyJS v1.
//
// Part of it could be done at parse time, but it would complicate
@@ -42,28 +42,30 @@ AST_Scope.DEFMETHOD("figure_out_scope", function(){
// XXX: this is wrong according to ECMA-262 (12.4). the
// `catch` argument name should be visible only inside the
// catch block. For a quick fix AST_Catch should inherit
- // from AST_Scope.
+ // from AST_Scope. Keeping it this way because of IE,
+ // which doesn't obey the standard. (it introduces the
+ // identifier in the enclosing scope)
scope.def_variable(node);
}
else if (node instanceof AST_SymbolRef) {
node.scope = scope;
}
+ if (node instanceof AST_LabelRef) {
+ var sym = scope.find_label(node);
+ if (!sym) throw new Error("Undefined label " + node.name);
+ node.reference(sym);
+ }
});
this.walk(tw);
// 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);
- if (!sym) throw new Error("Undefined label " + node.name);
- node.reference(sym);
- }
- else if (node instanceof AST_SymbolRef) {
+ if (node instanceof AST_SymbolRef) {
var sym = node.scope.find_variable(node);
node.reference(sym);
if (!sym) {
if (node.name == "eval") {
- for (var s = scope; s; s = s.parent_scope)
+ for (var s = node.scope; s; s = s.parent_scope)
s.uses_eval = true;
}
}
@@ -72,10 +74,11 @@ AST_Scope.DEFMETHOD("figure_out_scope", function(){
this.walk(tw);
});
-AST_Scope.DEFMETHOD("scope_warnings", function(options){
+AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
options = defaults(options, {
undeclared : false,
- assign_to_global : true
+ assign_to_global : true,
+ eval : true
});
var tw = new TreeWalker(function(node){
if (options.undeclared
@@ -105,6 +108,12 @@ AST_Scope.DEFMETHOD("scope_warnings", function(options){
col: node.start.col
});
}
+ if (options.eval
+ && node instanceof AST_SymbolRef
+ && node.undeclared
+ && node.name == "eval") {
+ AST_Node.warn("Eval is used [{line},{col}]", node.start);
+ }
});
this.walk(tw);
});
@@ -139,15 +148,18 @@ AST_Scope.DEFMETHOD("find_label", function(name){
});
AST_Scope.DEFMETHOD("def_function", function(symbol){
- this.def_variable(symbol);
this.functions[symbol.name] = symbol;
- symbol.scope = this;
+ this.def_variable(symbol);
});
AST_Scope.DEFMETHOD("def_variable", function(symbol){
symbol.global = !this.parent_scope;
- this.variables[symbol.name] = symbol;
- delete this.functions[symbol.name];
+ var existing = this.variables[symbol.name];
+ if (!existing) {
+ this.variables[symbol.name] = symbol;
+ } else {
+ symbol.uniq = existing;
+ }
symbol.scope = this;
});
@@ -158,7 +170,7 @@ AST_Scope.DEFMETHOD("def_label", function(symbol){
AST_Scope.DEFMETHOD("next_mangled", function(for_label){
var ext = this.enclosed, n = ext.length;
- out: for (;;) {
+ out: while (true) {
var m = base54(for_label
? (++this.lname)
: (++this.cname));
@@ -184,22 +196,32 @@ AST_Scope.DEFMETHOD("next_mangled", function(for_label){
});
AST_SymbolDeclaration.DEFMETHOD("mangle", function(){
- if (!this.global)
- this.mangled_name = this.scope.next_mangled(false);
-});
-
-AST_Label.DEFMETHOD("mangle", function(){
- this.mangled_name = this.scope.next_mangled(true);
+ if (this.uniq) {
+ this.uniq.mangle();
+ }
+ else if (!(this.global
+ || this.scope.uses_eval
+ || this.scope.uses_with
+ || this.mangled_name)) {
+ this.mangled_name = this.scope.next_mangled(this instanceof AST_Label);
+ }
});
-AST_Scope.DEFMETHOD("mangle_names", function(){
+AST_Toplevel.DEFMETHOD("mangle_names", function(){
var tw = new TreeWalker(function(node){
// We only need to mangle declarations. Special logic wired
// into the code generator will display the mangled name if
// it's present (and for AST_SymbolRef-s it'll use the mangled
// name of the AST_SymbolDeclaration that it points to).
- if (node instanceof AST_SymbolDeclaration) {
- node.mangle();
+ if (node instanceof AST_Scope) {
+ var a = node.variables;
+ for (var i in a) if (HOP(a, i)) {
+ a[i].mangle();
+ }
+ var a = node.labels;
+ for (var i in a) if (HOP(a, i)) {
+ a[i].mangle();
+ }
}
});
this.walk(tw);