1 | # ESLint Scope
|
2 |
|
3 | ESLint 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 | ```
|
8 | npm i eslint-scope --save
|
9 | ```
|
10 |
|
11 | ## 📖 Usage
|
12 |
|
13 | To use in an ESM file:
|
14 |
|
15 | ```js
|
16 | import * as eslintScope from 'eslint-scope';
|
17 | ```
|
18 |
|
19 | To use in a CommonJS file:
|
20 |
|
21 | ```js
|
22 | const eslintScope = require('eslint-scope');
|
23 | ```
|
24 |
|
25 | Example:
|
26 |
|
27 | ```js
|
28 | import * as eslintScope from 'eslint-scope';
|
29 | import * as espree from 'espree';
|
30 | import estraverse from 'estraverse';
|
31 |
|
32 | const ast = espree.parse(code, { range: true });
|
33 | const scopeManager = eslintScope.analyze(ast);
|
34 |
|
35 | const currentScope = scopeManager.acquire(ast); // global scope
|
36 |
|
37 | estraverse.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 |
|
57 | Issues 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 |
|
66 | ESLint Scope is licensed under a permissive BSD 2-clause license.
|