UNPKG

1.68 kBMarkdownView Raw
1# ESLint Scope
2
3ESLint Scope is the [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm) scope analyzer used in ESLint. It is a fork of [escope](http://github.com/estools/escope).
4
5## Install
6
7```
8npm i eslint-scope --save
9```
10
11## 📖 Usage
12
13To use in an ESM file:
14
15```js
16import * as eslintScope from 'eslint-scope';
17```
18
19To use in a CommonJS file:
20
21```js
22const eslintScope = require('eslint-scope');
23```
24
25Example:
26
27```js
28import * as eslintScope from 'eslint-scope';
29import * as espree from 'espree';
30import estraverse from 'estraverse';
31
32const ast = espree.parse(code, { range: true });
33const scopeManager = eslintScope.analyze(ast);
34
35const currentScope = scopeManager.acquire(ast); // global scope
36
37estraverse.traverse(ast, {
38 enter (node, parent) {
39 // do stuff
40
41 if (/Function/.test(node.type)) {
42 currentScope = scopeManager.acquire(node); // get current function scope
43 }
44 },
45 leave(node, parent) {
46 if (/Function/.test(node.type)) {
47 currentScope = currentScope.upper; // set to parent scope
48 }
49
50 // do stuff
51 }
52});
53```
54
55## Contributing
56
57Issues and pull requests will be triaged and responded to as quickly as possible. We operate under the [ESLint Contributor Guidelines](http://eslint.org/docs/developer-guide/contributing), so please be sure to read them before contributing. If you're not sure where to dig in, check out the [issues](https://github.com/eslint/eslint-scope/issues).
58
59## Build Commands
60
61* `npm test` - run all linting and tests
62* `npm run lint` - run all linting
63
64## License
65
66ESLint Scope is licensed under a permissive BSD 2-clause license.