aboutsummaryrefslogtreecommitdiff
path: root/lib/scope.js
blob: 05585fe0ed77c616d7f7ca25306dd7e4d7a85f67 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
    // 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 labels = {};
    var tw = new TreeWalker(function(node, descend){
        if (node instanceof AST_Scope) {
            node.init_scope_vars();
            var save_scope = node.parent_scope = scope;
            scope = node;
            descend();
            scope = save_scope;
            return true;        // don't descend again in TreeWalker
        }
        if (node instanceof AST_With) {
            for (var s = scope; s; s = s.parent_scope)
                s.uses_with = true;
            return;
        }
        if (node instanceof AST_LabeledStatement) {
            var l = node.label;
            if (labels[l.name])
                throw new Error(string_template("Label {name} defined twice", l));
            labels[l.name] = l;
            descend();
            delete labels[l.name];
            return true;        // no descend again
        }
        if (node instanceof AST_SymbolDeclaration) {
            node.init_scope_vars();
        }
        if (node instanceof AST_SymbolLambda) {
            scope.def_function(node);
        }
        else if (node instanceof AST_SymbolDefun) {
            // Careful here, the scope where this should be defined is
            // the parent scope.  The reason is that we enter a new
            // scope when we encounter the AST_Defun node (which is
            // instanceof AST_Scope) but we get to the symbol a bit
            // later.
            scope.parent_scope.def_function(node);
        }
        else if (node instanceof AST_SymbolVar) {
            scope.def_variable(node);
        }
        else if (node instanceof AST_SymbolCatch) {
            // 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.  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 = labels[node.name];
            if (!sym) throw new Error("Undefined label " + node.name);
            node.reference(sym);
        }
    });
    this.walk(tw);

    // pass 2: find back references and eval
    var func = null;
    var tw = new TreeWalker(function(node, descend){
        if (node instanceof AST_Lambda) {
            var prev_func = func;
            func = node;
            descend();
            func = prev_func;
            return true;
        }
        if (node instanceof AST_SymbolRef) {
            var sym = node.scope.find_variable(node);
            node.reference(sym);
            if (!sym) {
                if (node.name == "eval") {
                    for (var s = node.scope;
                         s && !s.uses_eval;
                         s = s.parent_scope) s.uses_eval = true;
                }
                if (node.name == "arguments") {
                    func.uses_arguments = true;
                }
            }
        }
    });
    this.walk(tw);
});

AST_Scope.DEFMETHOD("init_scope_vars", function(){
    this.variables = {};      // map name to AST_SymbolVar (variables defined in this scope; includes functions)
    this.functions = {};      // map name to AST_SymbolDefun (functions defined in this scope)
    this.uses_with = false;   // will be set to true if this or some nested scope uses the `with` statement
    this.uses_eval = false;   // will be set to true if this or nested scope uses the global `eval`
    this.parent_scope = null; // the parent scope
    this.enclosed = [];       // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
    this.cname = -1;          // the current index for mangling functions/variables
});

AST_Lambda.DEFMETHOD("init_scope_vars", function(){
    AST_Scope.prototype.init_scope_vars.call(this);
    this.uses_arguments = false;
});

AST_SymbolDeclaration.DEFMETHOD("init_scope_vars", function(){
    this.references = [];
});

AST_Toplevel.DEFMETHOD("scope_warnings", function(options){
    options = defaults(options, {
        undeclared       : false, // this makes a lot of noise
        unreferenced     : true,
        assign_to_global : true,
        func_arguments   : true,
        eval             : true
    });
    var tw = new TreeWalker(function(node){
        if (options.undeclared
            && node instanceof AST_SymbolRef
            && node.undeclared)
        {
            // XXX: this also warns about JS standard names,
            // i.e. Object, Array, parseInt etc.  Should add a list of
            // exceptions.
            AST_Node.warn("Undeclared symbol: {name} [{line},{col}]", {
                name: node.name,
                line: node.start.line,
                col: node.start.col
            });
        }
        if (options.assign_to_global)
        {
            var sym = null;
            if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef)
                sym = node.left;
            else if (node instanceof AST_ForIn && node.init instanceof AST_SymbolRef)
                sym = node.init;
            if (sym
                && (sym.undeclared
                    || (sym.symbol.global
                        && sym.scope !== sym.symbol.scope))) {
                AST_Node.warn("{msg}: {name} [{line},{col}]", {
                    msg: sym.undeclared ? "Accidental global?" : "Assignment to global",
                    name: sym.name,
                    line: sym.start.line,
                    col: sym.start.col
                });
            }
        }
        if (options.eval
            && node instanceof AST_SymbolRef
            && node.undeclared
            && node.name == "eval") {
            AST_Node.warn("Eval is used [{line},{col}]", node.start);
        }
        if (options.unreferenced
            && node instanceof AST_SymbolDeclaration
            && node.unreferenced()) {
            AST_Node.warn("{type} {name} is declared but not referenced [{line},{col}]", {
                type: node instanceof AST_Label ? "Label" : "Symbol",
                name: node.name,
                line: node.start.line,
                col: node.start.col
            });
        }
        if (options.func_arguments
            && node instanceof AST_Lambda
            && node.uses_arguments) {
            AST_Node.warn("arguments used in function {name} [{line},{col}]", {
                name: node.name ? node.name.name : "anonymous",
                line: node.start.line,
                col: node.start.col
            });
        }
    });
    this.walk(tw);
});

AST_SymbolRef.DEFMETHOD("reference", function(symbol) {
    if (symbol) {
        this.symbol = symbol;
        var origin = symbol.scope;
        symbol.references.push(this);
        for (var s = this.scope; s; s = s.parent_scope) {
            push_uniq(s.enclosed, symbol);
            if (s === origin) break;
        }
    } else {
        this.undeclared = true;
        for (var s = this.scope; s; s = s.parent_scope) {
            push_uniq(s.enclosed, this);
        }
    }
});

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("def_function", function(symbol){
    this.functions[symbol.name] = symbol;
    this.def_variable(symbol);
});

AST_Scope.DEFMETHOD("def_variable", function(symbol){
    symbol.global = !this.parent_scope;
    var existing = this.variables[symbol.name];
    if (!existing) {
        this.variables[symbol.name] = symbol;
    } else {
        symbol.uniq = existing;
    }
    symbol.scope = this;
});

AST_Scope.DEFMETHOD("next_mangled", function(){
    var ext = this.enclosed, n = ext.length;
    out: while (true) {
        var m = base54(++this.cname);
        if (!is_identifier(m)) continue; // skip over "do"
        // if it's for functions or variables, we must ensure that the
        // mangled name does not shadow a name from some parent scope
        // that is referenced in this or in inner scopes.
        for (var i = n; --i >= 0;) {
            var sym = ext[i];
            var name = sym.mangled_name || sym.name;
            if (m == name) continue out;
        }
        return m;
    }
});

AST_SymbolDeclaration.DEFMETHOD("mangle", function(){
    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();
    }
});

AST_Label.DEFMETHOD("mangle", function(){
    throw new Error("Don't call this");
});

AST_SymbolDeclaration.DEFMETHOD("unreferenced", function(){
    return this.definition().references.length == 0;
});

AST_SymbolDeclaration.DEFMETHOD("definition", function(){
    return this.uniq || this;
});

AST_Toplevel.DEFMETHOD("mangle_names", function(){
    // We only need to mangle declaration nodes.  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).
    var lname = -1;
    var tw = new TreeWalker(function(node, descend){
        if (node instanceof AST_LabeledStatement) {
            // lname is incremented when we get to the AST_Label
            var save_nesting = lname;
            descend();
            lname = save_nesting;
            return true;        // don't descend again in TreeWalker
        }
        if (node instanceof AST_Scope) {
            var a = node.variables;
            for (var i in a) if (HOP(a, i)) {
                a[i].mangle();
            }
            return;
        }
        if (node instanceof AST_Label) {
            var name;
            do name = base54(++lname); while (!is_identifier(name));
            node.mangled_name = name;
            return;
        }
    });
    this.walk(tw);
});