What is eslint-scope?
The eslint-scope npm package is used to analyze the scope of variables within JavaScript code. It is part of the ESLint family of tools and is used to determine where variables are declared and how they are used throughout the code. This can be particularly useful for identifying issues with variable shadowing, undeclared variables, or potential scope leaks.
What are eslint-scope's main functionalities?
Analyzing variable scope in a JavaScript file
This code sample demonstrates how to use eslint-scope to analyze the scope of variables in a JavaScript file. It reads the file, parses it into an AST (Abstract Syntax Tree) using espree, and then analyzes the scope with eslint-scope. The resulting globalScope object contains information about the variables and references in the global scope of the file.
const eslintScope = require('eslint-scope');
const espree = require('espree');
const fs = require('fs');
const code = fs.readFileSync('myfile.js', 'utf8');
const ast = espree.parse(code, { ecmaVersion: 6 });
const scopeManager = eslintScope.analyze(ast);
const globalScope = scopeManager.globalScope;
// Inspect the variables and references in the global scope
console.log(globalScope.variables);
console.log(globalScope.references);
Other packages similar to eslint-scope
escope
Escope is another scope analysis tool for JavaScript. It is similar to eslint-scope but is no longer actively maintained. Escope was the predecessor to eslint-scope and served a similar purpose in analyzing variable scope within JavaScript code.
jscodeshift
Jscodeshift is a toolkit for running codemods over multiple JavaScript or TypeScript files. It uses a different approach but can be used for similar scope analysis and transformation tasks. Jscodeshift provides a more extensive API for manipulating the AST and is often used for large-scale codebase refactors.
babel-traverse
Babel-traverse allows you to traverse and manipulate the AST generated by Babel. While its primary use is for Babel plugins and transformations, it can also be used to analyze scope and variable declarations in a manner similar to eslint-scope. Babel-traverse is part of the larger Babel ecosystem, which is widely used for JavaScript compilation and transformation.
ESLint Scope
ESLint Scope is the ECMAScript scope analyzer used in ESLint. It is a fork of escope.
Install
npm i eslint-scope --save
📖 Usage
To use in an ESM file:
import * as eslintScope from 'eslint-scope';
To use in a CommonJS file:
const eslintScope = require('eslint-scope');
Example:
import * as eslintScope from 'eslint-scope';
import * as espree from 'espree';
import estraverse from 'estraverse';
const ast = espree.parse(code, { range: true });
const scopeManager = eslintScope.analyze(ast);
const currentScope = scopeManager.acquire(ast);
estraverse.traverse(ast, {
enter (node, parent) {
if (/Function/.test(node.type)) {
currentScope = scopeManager.acquire(node);
}
},
leave(node, parent) {
if (/Function/.test(node.type)) {
currentScope = currentScope.upper;
}
}
});
Contributing
Issues and pull requests will be triaged and responded to as quickly as possible. We operate under the ESLint Contributor Guidelines, so please be sure to read them before contributing. If you're not sure where to dig in, check out the issues.
Build Commands
npm test
- run all linting and testsnpm run lint
- run all linting
License
ESLint Scope is licensed under a permissive BSD 2-clause license.
7.2.1 (2023-05-31)
Chores
v7.2.0 - April 13, 2023
70c8db1
feat: Add isGlobalReturn method on scopeManager. (#96) (Nicholas C. Zakas)3dbad80
chore: add triage action (#95) (Milos Djermanovic)34ffedc
ci: add Node v19 (#94) (Milos Djermanovic)4c00534
ci: update Github actions (#93) (Deepshika S)6c8ccf2
chore: add funding field (#92) (Deepshika S)a8811b8
build: add node v18 (#91) (唯然)25abacf
docs: add badges (#89) (Milos Djermanovic)
v7.1.1 - February 11, 2022
8938109
chore: upgrade espree@9.3.1 eslint-visitor-keys@3.3.0 (#88) (Milos Djermanovic)4e1d24c
fix: ignore "use strict"
directives in ES3 (#87) (Milos Djermanovic)ceb8bdd
ci: use node v16 (#84) (Nitin Kumar)62e147b
test: add tests with year-based ecmaVersion
(#83) (Milos Djermanovic)
v7.1.0 - November 20, 2021
d756f1e
feat: Add sourceType:commonjs support (#81) (Nicholas C. Zakas)
v7.0.0 - November 16, 2021
22a55c0
feat!: support class static blocks (#80) (Milos Djermanovic)4aafb61
build: upgrade eslint-release to v3.2.0 to support conventional commits (#79) (Milos Djermanovic)263c762
Build: add node v17 (#76) (唯然)
v6.0.0 - July 23, 2021
4ee1d80
Fix: Ensure correct version in package (#73) (Nicholas C. Zakas)82a7e6d
Breaking: Switch to ESM (fixes #70) (#71) (Brett Zamir)0b4a5f1
Update: support class fields (refs eslint/eslint#14343) (#69) (Toru Nagashima)39f8cfc
Chore: upgrade estraverse to version 5 (#68) (Rouven Weßling)ae27ff3
Docs: Add range to espree options in README (fixes #66) (#67) (Alan Liang)
v5.1.1 - September 12, 2020
9b528d7
Upgrade: esrecurse version to ^4.3.0 (#64) (Timofey Kachalov)f758bbc
Chore: fix definiton -> definition typo in comments (#63) (Kevin Kirsche)7513734
Chore: move to GitHub Actions (#62) (Kai Cataldo)
v5.1.0 - June 4, 2020
d4a3764
Update: support new export syntax (#56) (Toru Nagashima)
v5.0.0 - July 20, 2019
e9fa22e
Upgrade: update dependencies after dropping support for Node <8 (#53) (Kai Cataldo)ee9f7c1
Breaking: drop support for Node v6 (#54) (Kai Cataldo)
v4.0.3 - March 15, 2019
299df64
Fix: arrow function scope strictness (take 2) (#52) (futpib)
v4.0.2 - March 1, 2019
c925600
Revert "Fix: Arrow function scope strictness (fixes #49) (#50)" (#51) (Teddy Katz)
v4.0.1 - March 1, 2019
2533966
Fix: Arrow function scope strictness (fixes #49) (#50) (futpib)0cbeea5
Chore: add supported Node.js versions to CI (#47) (Kai Cataldo)b423057
Upgrade: eslint-release@1.0.0 (#46) (Teddy Katz)
v4.0.0 - June 21, 2018
v4.0.0-rc.0 - June 9, 2018
- 3b919b8 Build: Adding rc release script to package.json (#38) (Kevin Partington)
- 137732a Chore: avoid creating package-lock.json files (#37) (Teddy Katz)
v4.0.0-alpha.0 - April 27, 2018
- 7cc3769 Upgrade: eslint-release ^0.11.1 (#36) (Teddy Katz)
- c9f6967 Breaking: remove TDZScope (refs eslint/eslint#10245) (#35) (Toru Nagashima)
- 982a71f Fix: wrong resolution about default parameters (#33) (Toru Nagashima)
- 57889f1 Docs: Remove extra header line from LICENSE (#32) (Gyandeep Singh)
v3.7.1 - April 12, 2017
- ced6262 Fix: restore previous Scope API exports from escope (#31) (Vitor Balocco)
- 5c3d966 Fix: Remove and Modify tests that contain invalid ES6 syntax (#29) (Reyad Attiyat)
v3.7.0 - March 17, 2017
- 9e27835 Chore: Add files section to package.json (#24) (Ilya Volodin)
- 3e4d123 Upgrade: eslint-config-eslint to 4.0.0 (#21) (Teddy Katz)
- 38c50fb Chore: Rename src to lib and test to tests (#20) (Corbin Uselton)
- f4cd920 Chore: Remove esprima (#19) (Corbin Uselton)
- f81fad5 Revert "Chore: Remove esprima" (#18) (James Henry)
- 31b0085 Chore: Remove es6-map and es6-weakmap as they are included in node4 (#10) (#13) (Corbin Uselton)
- 12a1ca1 Add Makefile.js and eslint (#15) (Reyad Attiyat)
- 7d23f8e Chore: Remove es6-map and es6-weakmap as they are included in node4 (#10) (Corbin Uselton)
- 019441e Chore: Convert to ES6 that is supported on Node 4, commonjs modules and remove Babel (#14) (Corbin Uselton)
- c647f65 Update: Add check for node.body in referencer (#2) (Corbin Uselton)
- eb5c9db Remove browserify and jsdoc (#12) (Corbin Uselton)
- cf38df0 Chore: Update README.md (#3) (James Henry)
- 8a142ca Chore: Add eslint-release scripts (#6) (James Henry)
- e60d8cb Chore: Remove unused bower.json (#5) (James Henry)
- 049c545 Chore: Fix tests for eslint-scope (#4) (James Henry)
- f026aab Chore: Update package.json for eslint fork (#1) (James Henry)
- a94d281 Chore: Update license with JSF copyright (Nicholas C. Zakas)