-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscope.js
373 lines (314 loc) · 8.79 KB
/
scope.js
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
Analyse ast, create scopes
*/
var walker = require('./walker').astWalker();
var utils = require('./utils');
var hasOwnProperty = Object.prototype.hasOwnProperty;
var fnWalker = function(token, scope){
var type = token.type;
var name = token.id && token.id.name;
var args = token.params;
var newScope = new Scope('function', scope);
newScope.sourceToken = token;
token.scope = newScope;
if (name)
{
if (type === 'FunctionDeclaration')
scope.put(name, {
type: type,
token: token,
loc: token.loc.start
});
else
newScope.put(name, {
type: type,
token: token,
loc: token.loc.start
});
}
// arrow function does not have own 'arguments' reference
if (type !== 'ArrowFunctionExpression')
newScope.put('arguments', {
type: 'arguments',
token: null,
args: args
});
for (var i = 0; i < args.length; i++)
newScope.put(args[i].name, {
type: 'arg',
token: null,
loc: args[i].loc.start,
args: args,
index: i
});
this.scopes.push(newScope);
};
//
// main function
//
function process(ast, scope){
ast.scope = scope;
ast.names = [];
ast.scopes = [];
var varWalker = function(token, scope){
scope.declarations.push({
type: 'var',
name: token.id.name,
parent: this.top(1),
loc: token.loc.start
});
scope.put(token.id.name, {
type: 'var',
token: token.init,
loc: token.loc.start
});
};
return walker.walk(ast, {
VariableDeclarator: varWalker,
FunctionDeclaration: fnWalker,
FunctionExpression: fnWalker,
ArrowFunctionExpression: fnWalker,
ExpressionStatement: function(token){
if (token.directive && token.directive.toLowerCase() === 'use strict')
scope.strict = true;
},
CatchClause: function(token, scope){
var newScope = new Scope('catch', scope);
scope.put(token.param.name, {
type: 'catch',
token: null
});
token.body.scope = newScope;
this.scopes.push(scope);
},
Identifier: function(token, scope){
var parent = this.top(1);
// let insane begin!
// todo implement more efficient solution in a future version
var isLabel = parent.type === 'LabeledStatement' || parent.type === 'ContinueStatement' || parent.type === 'BreakStatement';
var isFunctionName = parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' || parent.type === 'ClassDeclaration' || parent.type === 'ClassExpression';
var isProperty = parent.type === 'Property';
var isMemberExpression = parent.type === 'MemberExpression';
var isAssignmentExpression = parent.type === 'AssignmentExpression';
var isVariableDeclarator = parent.type === 'VariableDeclarator';
if (isLabel || isFunctionName || isProperty || isMemberExpression || isAssignmentExpression || isVariableDeclarator)
{
var isObjectKey = isProperty && parent.key === token && parent.computed;
var isObjectValue = isProperty && parent.value === token;
var isMemberExpressionProperty = isMemberExpression && parent.property === token && parent.computed;
var isMemberExpressionObject = isMemberExpression&& parent.object === token;
var isVariableInit = isVariableDeclarator && parent.init === token;
var isLeftOfAssignment = isAssignmentExpression && parent.left === token && parent.operator !== '=';
var isRightOfAssignment = isAssignmentExpression && parent.right === token;
// is it for..in variable? (mark its name as used)
var isVarInForIn = false;
if (isVariableDeclarator)
{
var declarationParent = this.top(3);
isVarInForIn = declarationParent && declarationParent.type === 'ForInStatement';
}
if (isFunctionName || isLabel ||
!isObjectKey && !isObjectValue &&
!isMemberExpressionProperty && !isMemberExpressionObject &&
!isLeftOfAssignment && !isRightOfAssignment &&
!isVariableInit && !isVarInForIn)
return;
}
this.names.push({
scope: scope,
token: token
});
}
}, {
names: ast.names,
scopes: ast.scopes
});
}
//
// Scope class
//
function Scope(type, parentScope, thisObject){
this.type = type || 'unknown';
this.thisObject = thisObject || utils.createIdentifier('undefined');
this.subscopes = [];
this.names = {};
this.declarations = [];
if (parentScope)
{
this.parent = parentScope;
this.root = parentScope.root;
this.strict = parentScope.strict;
parentScope.subscopes.push(this);
}
else
{
this.root = this;
}
this.put('this', {
type: 'readonly',
token: this.thisObject
});
}
Scope.prototype = {
root: null,
parent: null,
subscopes: null,
thisObject: null,
names: null,
strict: false,
getOwnNames: function(){
return Object.keys(this.names);
},
scopeByName: function(name){
var cursor = this;
while (cursor)
{
if (hasOwnProperty.call(cursor.names, name))
return cursor;
cursor = cursor.parent;
}
return null;
},
has: function(name){
return this.scopeByName(name) != null;
},
hasOwn: function(name){
return hasOwnProperty.call(this.names, name);
},
get: function(name){
var ownerScope = this.scopeByName(name);
if (ownerScope)
return ownerScope.names[name];
},
getOwn: function(name){
return this.names[name] || null;
},
token: function(name){
var ref = this.get(name);
return ref && ref.token;
},
put: function(name, info){
this.names[name] = info;
},
set: function(name, token){
var scope = this.scopeByName(name);
if (scope)
{
var ref = scope.names[name];
ref.token = token;
}
},
resolve: function(token){
var path = [];
var cursor = token;
if (cursor.obj && cursor.ref_)
return cursor.ref_;
cycle:
while (cursor && !cursor.obj)
{
switch (cursor.type)
{
case 'ThisExpression':
// FIXME: it seems a hack, remove cursor.scope - thisObject always must be of current scope
cursor = (cursor.scope || this).thisObject;
break cycle;
case 'Identifier':
var nameScope = cursor.scope || this.scopeByName(cursor.name);
if (!nameScope)
return;
if (nameScope === this)
{
cursor = this.names[cursor.name];
if (cursor)
cursor = cursor.token;
}
else
cursor = nameScope.resolve(cursor);
break cycle;
case 'MemberExpression':
var property = cursor.property;
if (cursor.property.computed)
property = this.resolve(cursor.property);
if (!property)
return;
if (property.type === 'Literal')
path.unshift(property.value);
else
path.unshift(property.name);
cursor = cursor.object;
break;
case 'Literal':
case 'FunctionDeclaration':
case 'FunctionExpression':
case 'ArrowFunctionExpression':
case 'ObjectExpression':
case 'ArrayExpression':
break cycle;
case 'CallExpression':
cursor = cursor.ref_;
break;
default:
return;
}
}
if (cursor && path.length)
{
if (cursor.ref_)
cursor = cursor.ref_;
if (!cursor.obj)
return;
for (var i = 0, key; key = path[i]; i++)
if (cursor.obj && key in cursor.obj && cursor.obj[key])
{
cursor = cursor.obj[key];
if (cursor.ref_)
cursor = cursor.ref_;
}
else
return;
}
return cursor;
},
deepResolve: function(token){
var prev;
do
{
prev = token;
token = this.resolve(token);
}
while (token && token !== prev);
return token;
},
simpleExpression: function(token){
switch (token.type)
{
case 'BinaryExpression':
if (token.operator === '+')
{
var left = this.simpleExpression(token.left);
var right = this.simpleExpression(token.right);
if (left.type === 'Literal' && right.type === 'Literal')
return utils.createLiteral(left.value + right.value);
}
break;
case 'ArrayExpression':
return { type: 'ArrayExpression', elements: token.elements.map(this.simpleExpression, this) };
default:
return this.deepResolve(token);
}
},
isLocal: function(name){
return hasOwnProperty.call(this.names, name);
},
isGlobal: function(name){
var scope = this.scopeByName(name);
return scope ? scope.root === scope : true;
}
};
//
// export
//
module.exports = {
Scope: Scope,
process: process
};