Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

scope-analyzer

goto-bus-stop1.7mApache-2.02.1.2

simple scope analysis for javascript ASTs

analysis, ast, javascript, nodes, refactor, rename, scope

readme

scope-analyzer

simple scope analysis for javascript ASTs. tracks scopes and collects references to variables.

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!

stability npm travis standard

Install

npm install scope-analyzer

Usage

Note: AST nodes passed to scope-analyzer functions are expected to reference the parent node on a node.parent property. 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 = require('scope-analyzer')

var ast = parse('...')
// Initialize node module variables
scan.createScope(ast, ['module', 'exports', '__dirname', '__filename'])
scan.crawl(ast)

var binding = scan.getBinding(ast, 'exports')
binding.getReferences().forEach(function (reference) {
  // Assume for the sake of the example that all references to `exports` are assignments like
  // `exports.xyz = abc`
  console.log('found export:', reference.parent.property.name)
})

API

crawl(ast)

Walk the ast and analyze all scopes. This will immediately allow you to use the get* methods on any node in the tree.

clear(ast)

Clear scope information in all nodes of the AST.

visitScope(node)

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.

visitBinding(node)

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.

createScope(node, bindings)

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 = parse('xyz')
scopeAnalyzer.createScope(ast, ['HTMLElement', 'Notification', ...])

deleteScope(node)

Delete the scope initialised by node.

scope(node)

Get the Scope initialised by the given node.

getBinding(node)

Get the Binding referenced by the Identifier node.

Scope

scope.has(name)

Check if this scope defines name.

scope.getBinding(name)

Get the Binding named name that is declared by this scope.

scope.getReferences(name)

Get a list of all nodes referencing the name binding that is declared by this scope.

scope.getUndeclaredNames()

Get a list of all names that were used in this scope, but not defined anywhere in the AST.

scope.forEach(cb(binding, name))

Loop over all bindings declared by this scope.

scope.forEachAvailable(cb(binding, name))

Loop over all bindings available to this scope, declared in this scope or any parent scope.

Binding

binding.definition

The node that defined this binding. If this binding was not declared in the AST, binding.definition will be undefined.

binding.getReferences()

Return an array of nodes that reference this binding.

binding.isReferenced()

Check if the binding is referenced, i.e., if there are any identifier Nodes (other than binding.definition) referencing this binding.

binding.remove(node)

Remove a reference to this binding. Use this when you are replacing the node referencing the binding with something else.

License

Apache-2.0

changelog

scope-analyzer change log

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning.

2.1.2 / 2021-10-05

  • make .parent and [kScope] properties non-enumerable, fixing compatibility with recast. anecdotally a 20-30% performance regression.

    you can pin to 2.1.1 if you need the 20% and don't need safe traversal of node properties.

2.1.1 / 2020-03-06

  • use dash-ast for faster tree walking. anecdotally results in a 10-20% analysis speedup.

2.1.0 / 2020-03-06

  • add deleteScope() and clear() methods to delete scope information from a single node or an entire tree. Thanks @fabiosantoscode!

2.0.6 / 2020-03-05

  • detect the .value part of a shorthand property as the variable reference, instead of the .key part.

This is a bugfix only for ASTs that were already modified prior to being crawled with scope-analyzer. Thanks @fabiosantoscode!

2.0.5 / 2018-06-25

  • detect catch(param){} bindings

2.0.4 / 2018-05-22

  • fix class method definition names being counted as references to outer variables.

2.0.3 / 2018-04-20

  • revert to custom walker, acorn.walk behaviour is different and not faster

2.0.2 / 2018-04-20

  • fix valid references that occur above a value is declared in source code

2.0.1 / 2018-03-30

  • always initialise scope on the root node, so that undeclared names can be attached

2.0.0 / 2018-03-08

  • add support for ES5 environments (Node 0.10+)
  • scan.getBinding() now also returns bindings for undeclared identifiers. binding.definition will be undefined for undeclared identifiers.

1.3.0 / 2018-01-13

  • add binding.remove(node) to remove a reference to a binding from the list of references. use binding.isReferenced() to check if there are any references left.

1.2.0 / 2018-01-02

  • track uses of undeclared variable names. use getUndeclaredNames() on the root scope to get a list of undeclared names used in the AST.

1.1.1 / 2018-01-02

  • fix import { a as b } being counted as a reference to a

1.1.0 / 2017-12-26

  • account for import declarations
  • rename analyze to crawl (analyze is still available as alias)
  • some tests

1.0.0 / 2017-11-15

  • initial release