pissang / dfatool

JavaScript Data Flow Analyze Tool
47 stars 7 forks source link

How to traverse scope? #3

Open CMatias opened 8 years ago

CMatias commented 8 years ago

There's a reference in the readme to "get a variable on a specific scope". However, in the code example there's not an example on how to traverse the scope, only a reference to function calls.

jzy2000 commented 7 years ago

You may consider traverse all scopes recursively, starting from globalScope:

var ast = esprima.parse(code, {
    loc : true
});

var globalScope = dfatool.newGlobalScope();
dfatool.buildScope(ast, globalScope);

globalScope.initialize();
globalScope.derivation()
... ...
function traverse(scope)
{
    //do something with 'scope'
    for(var i=0;i<scope.children.length;i++)
       traverse(scope.children[i]);
}
traverse(globalScope);