# scope-analyzer

> simple scope analysis for javascript ASTs

Latest version **2.1.2** (published 2021-10-05) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install scope-analyzer
pnpm add scope-analyzer
yarn add scope-analyzer
bun add scope-analyzer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2021-10-05 |
| First published | 2017-11-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 30.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Renée Kooi |
| Maintainers | goto-bus-stop |
| Keywords | analysis, ast, javascript, nodes, refactor, rename, scope |

## Links

- npm: https://www.npmjs.com/package/scope-analyzer
- Repository: https://github.com/goto-bus-stop/scope-analyzer
- Issues: https://github.com/goto-bus-stop/scope-analyzer/issues
- npm.io page: https://npm.io/package/scope-analyzer

## Dependencies (7)

- [es6-map](https://npm.io/package/es6-map.md) ^0.1.5
- [es6-set](https://npm.io/package/es6-set.md) ^0.1.5
- [dash-ast](https://npm.io/package/dash-ast.md) ^2.0.1
- [array-from](https://npm.io/package/array-from.md) ^2.1.1
- [es6-symbol](https://npm.io/package/es6-symbol.md) ^3.1.1
- [estree-is-function](https://npm.io/package/estree-is-function.md) ^1.0.0
- [get-assigned-identifiers](https://npm.io/package/get-assigned-identifiers.md) ^1.1.0

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 2.1.2 (latest) — 2021-10-05
- 2.1.1 — 2020-03-06
- 2.1.0 — 2020-03-06
- 2.0.6 — 2020-03-05
- 2.0.5 — 2018-06-25
- 2.0.4 — 2018-05-22
- 2.0.3 — 2018-04-20
- 2.0.2 — 2018-04-20
- 2.0.1 — 2018-03-30
- 2.0.0 — 2018-03-08
- 1.3.0 — 2018-01-13
- 1.2.0 — 2018-01-02
- 1.1.1 — 2017-12-26
- 1.1.0 — 2017-12-26
- 1.0.0 — 2017-11-15

## 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][stability-image]][stability-url]
[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url]

[stability-image]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square
[stability-url]: https://nodejs.org/api/documentation.html#documentation_stability_index
[npm-image]: https://img.shields.io/npm/v/scope-analyzer.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/scope-analyzer
[travis-image]: https://img.shields.io/travis/goto-bus-stop/scope-analyzer.svg?style=flat-square
[travis-url]: https://travis-ci.org/goto-bus-stop/scope-analyzer
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/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](https://github.com/substack/node-falafel) or [transform-ast](https://github.com/goto-bus-stop/transform-ast) have a `.parent` property, but others may not. You can use [estree-assign-parent](https://github.com/goto-bus-stop/estree-assign-parent) to quickly assign a parent property to all nodes in an AST.

```js
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:

```js
var ast = parse('xyz')
scopeAnalyzer.createScope(ast, ['HTMLElement', 'Notification', ...])
```

### `deleteScope(node)`

Delete the scope initialised by node.

### `scope(node)`

Get the [Scope](#scope) initialised by the given node.

### `getBinding(node)`

Get the [Binding](#binding) referenced by the `Identifier` `node`.

### Scope

#### `scope.has(name)`

Check if this scope defines `name`.

#### `scope.getBinding(name)`

Get the [Binding](#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](LICENSE.md)

---
_Source: https://npm.io/package/scope-analyzer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
