1 | [![npm version](https://img.shields.io/npm/v/eslint-scope.svg)](https://www.npmjs.com/package/eslint-scope)
|
---|
2 | [![Downloads](https://img.shields.io/npm/dm/eslint-scope.svg)](https://www.npmjs.com/package/eslint-scope)
|
---|
3 | [![Build Status](https://github.com/eslint/eslint-scope/workflows/CI/badge.svg)](https://github.com/eslint/eslint-scope/actions)
|
---|
4 |
|
---|
5 | # ESLint Scope
|
---|
6 |
|
---|
7 | 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).
|
---|
8 |
|
---|
9 | ## Install
|
---|
10 |
|
---|
11 | ```
|
---|
12 | npm i eslint-scope --save
|
---|
13 | ```
|
---|
14 |
|
---|
15 | ## 📖 Usage
|
---|
16 |
|
---|
17 | To use in an ESM file:
|
---|
18 |
|
---|
19 | ```js
|
---|
20 | import * as eslintScope from 'eslint-scope';
|
---|
21 | ```
|
---|
22 |
|
---|
23 | To use in a CommonJS file:
|
---|
24 |
|
---|
25 | ```js
|
---|
26 | const eslintScope = require('eslint-scope');
|
---|
27 | ```
|
---|
28 |
|
---|
29 | Example:
|
---|
30 |
|
---|
31 | ```js
|
---|
32 | import * as eslintScope from 'eslint-scope';
|
---|
33 | import * as espree from 'espree';
|
---|
34 | import estraverse from 'estraverse';
|
---|
35 |
|
---|
36 | const ast = espree.parse(code, { range: true });
|
---|
37 | const scopeManager = eslintScope.analyze(ast);
|
---|
38 |
|
---|
39 | const currentScope = scopeManager.acquire(ast); // global scope
|
---|
40 |
|
---|
41 | estraverse.traverse(ast, {
|
---|
42 | enter (node, parent) {
|
---|
43 | // do stuff
|
---|
44 |
|
---|
45 | if (/Function/.test(node.type)) {
|
---|
46 | currentScope = scopeManager.acquire(node); // get current function scope
|
---|
47 | }
|
---|
48 | },
|
---|
49 | leave(node, parent) {
|
---|
50 | if (/Function/.test(node.type)) {
|
---|
51 | currentScope = currentScope.upper; // set to parent scope
|
---|
52 | }
|
---|
53 |
|
---|
54 | // do stuff
|
---|
55 | }
|
---|
56 | });
|
---|
57 | ```
|
---|
58 |
|
---|
59 | ## Contributing
|
---|
60 |
|
---|
61 | 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).
|
---|
62 |
|
---|
63 | ## Build Commands
|
---|
64 |
|
---|
65 | * `npm test` - run all linting and tests
|
---|
66 | * `npm run lint` - run all linting
|
---|
67 |
|
---|
68 | ## License
|
---|
69 |
|
---|
70 | ESLint Scope is licensed under a permissive BSD 2-clause license.
|
---|