-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello-escope.js
40 lines (34 loc) · 1.02 KB
/
hello-escope.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
const inspect = require('util').inspect;
var escope = require('escope');
var esprima = require('esprima');
var estraverse = require('estraverse');
const code = `
function tutu() {
var a = 0;
function tutu1() {
var a = 0;
function tutu2() {
var a = 0;
}
}
}
`;
var ast = esprima.parse(code);
var scopeManager = escope.analyze(ast);
var currentScope = scopeManager.acquire(ast); // global scope
estraverse.traverse(ast, {
enter: function(node, parent) {
// do stuff
if (/Function/.test(node.type)) {
currentScope = scopeManager.acquire(node); // get current function scope
console.log("Entering " + inspect(currentScope.set.keys(), { depth: null }));
}
},
leave: function(node, parent) {
if (/Function/.test(node.type)) {
console.log("Leaving " + inspect(currentScope.set.keys(), { depth: null }));
currentScope = currentScope.upper; // set to parent scope
}
// do stuff
}
});