https://github.com/Rich-Harris/periscopic
Utility for analyzing scopes belonging to an ESTree-compliant AST
https://github.com/Rich-Harris/periscopic
Last synced: 6 months ago
JSON representation
Utility for analyzing scopes belonging to an ESTree-compliant AST
- Host: GitHub
- URL: https://github.com/Rich-Harris/periscopic
- Owner: Rich-Harris
- License: mit
- Created: 2019-09-08T19:33:55.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-08T11:28:58.000Z (over 1 year ago)
- Last Synced: 2025-03-03T14:44:12.971Z (7 months ago)
- Language: JavaScript
- Size: 1.9 MB
- Stars: 96
- Watchers: 5
- Forks: 8
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-nodejs - periscopic - Utility for analyzing scopes belonging to an ESTree-compliant AST.  (Repository / AST)
README
# periscopic
Utility for analyzing scopes belonging to an ESTree-compliant AST.
## API
```js
import { analyze } from 'periscopic';const ast = acorn.parse(`
const a = b;
console.log(a);
`);const { map, globals, scope } = analyze(ast);
```* `map` is a `WeakMap`, where the keys are the nodes of your AST that create a scope
* `globals` is a `Map` of all the identifiers that are referenced without being declared anywhere in the program (in this case, `b` and `console`)
* `scope` is the top-level `Scope` belonging to the program### Scope
Each `Scope` instance has the following properties:
* `scope.block` — true if the scope is created by a block statement (i.e. `let`, `const` and `class` are contained to it), false otherwise
* `scope.parent` — the parent scope object
* `scope.declarations` — a `Map` of all the variables declared in this scope, the node value referes to the declaration statement
* `scope.initialised_declarations` — a `Set` of all the variables declared and initialised in this scope
* `scope.references` — a `Set` of all the names referenced in this scope (or child scopes)It also has two methods:
* `scope.has(name)` — returns `true` if `name` is declared in this scope or an ancestor scope
* `scope.find_owner(name)` — returns the scope object in which `name` is declared (or `null` if it is not declared)### `extract_identifiers` and `extract_names`
This package also exposes utilities for extracting the identifiers contained in a declaration or a function parameter:
```js
import { extract_identifiers, extract_names } from 'periscopic';const ast = acorn.parse(`
const { a, b: [c, d] = e } = opts;
`);const lhs = ast.body[0].declarations[0].id;
extract_identifiers(lhs);
/*
[
{ type: 'Identifier', name: 'a', start: 9, end: 10 },
{ type: 'Identifier', name: 'c', start: 16, end: 17 },
{ type: 'Identifier', name: 'd', start: 19, end: 20 }
]
*/extract_names(lhs);
/*
['a', 'c', 'd']
*/
```## License
[MIT](LICENSE)