Caveats and/or todos:
- May be missing edge cases.
- Things like
label:s are not considered at all, but ideally in the future they will!
npm install scope-analyzer
Note: AST nodes passed to
scope-analyzer functions are expected to reference the parent node on a
Nodes from falafel or transform-ast have a
.parent property, but others may not. You can use estree-assign-parent to quickly assign a parent property to all nodes in an AST.
var scan =var ast =// Initialize node module variablesscanscanvar binding = scanbinding
Walk the ast and analyze all scopes. This will immediately allow you to use the
get* methods on any node in the tree.
Clear scope information in all nodes of the AST.
Visit a node to check if it initialises any scopes. For example, a function declaration will initialise a new scope to hold bindings for its parameters. Use this if you are already walking the AST manually, and if you don't need the scope information during this walk.
Visit a node to check if it is a reference to an existing binding. If it is, the reference is added to the parent scope. Use this if you are already walking the AST manually.
Initialise a new scope at the given node.
bindings is an array of variable names.
This can be useful to make the scope analyzer aware of preexisting global variables.
In that case, call
createScope on the root node with the names of globals:
var ast =scopeAnalyzer
Delete the scope initialised by node.
Get the Scope initialised by the given node.
Get the Binding referenced by the
Check if this scope defines
Get the Binding named
name that is declared by this scope.
Get a list of all nodes referencing the
name binding that is declared by this scope.
Get a list of all names that were used in this scope, but not defined anywhere in the AST.
Loop over all bindings declared by this scope.
Loop over all bindings available to this scope, declared in this scope or any parent scope.
The node that defined this binding. If this binding was not declared in the AST,
binding.definition will be undefined.
Return an array of nodes that reference this binding.
Check if the binding is referenced, i.e., if there are any identifier Nodes (other than
binding.definition) referencing this binding.
Remove a reference to this binding. Use this when you are replacing the node referencing the binding with something else.