-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-scopes.js
51 lines (40 loc) · 915 Bytes
/
extract-scopes.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
var estraverse = require('estraverse');
module.exports = extractScopes;
function contained(node, array) {
var w = node;
while (w) {
if (array.indexOf(w) > -1) {
return true;
}
w = w.parent;
}
return false;
}
function extractScopes(ast) {
var scopes = [];
var path = [];
var lastNode = null;
estraverse.traverse(ast, {
enter: function(node, parent) {
// ensure we have access to parents
node.parent = parent;
if (node.type === 'ExpressionStatement' && node.expression.value === 'use rat') {
if (contained(parent, scopes)) {
return;
}
scopes.push(parent);
var p = parent;
if (p.body) {
p = p.body;
}
for (var i = 0; i<p.length; i++) {
if (p[i] === node) {
p.splice(i, 1);
break;
}
}
}
}
});
return scopes;
}