npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scope-analyzer

v2.1.2

Published

simple scope analysis for javascript ASTs

Downloads

2,875,954

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