Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 82a7e6d

Browse files
brettz9nzakasmdjermanovicbtmills
authored
Breaking: Switch to ESM (fixes #70) (#71)
* Chore: Update devDeps. * Chore: Lint according to latest config * Breaking: Switch to ESM; also adds eslint-plugin-jsdoc for linter * Fix: Sync with eslint-visitor-keys, including fixing cjs linting * Update package.json Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Test: Drop Node 13, 10, 8 tests, and add 16, 12.22.0 * Docs: Add comment to explain shelljs command * Refactor: Apply new API and ESM changes * Chore: Update devDeps. * Docs: Update espree usage in README * Test: Add CJS file * Update README.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Fix: Avoid named imports of CJS modules * Fix: For browser compatibility, avoid package.json file-reading attempt * Refactor: Drop `fs` from Rollup now that not in use * Fix: Stop re-exporting Scope child classes and only export `Scope` * Fix: Re-export Reference, Definition, and PatternVisitor * Update Makefile.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Update tests/commonjs.cjs Co-authored-by: Brandon Mills <btmills@users.noreply.github.com> Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> Co-authored-by: Brandon Mills <btmills@users.noreply.github.com>
1 parent 0b4a5f1 commit 82a7e6d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+435
-312
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; EditorConfig file: https://EditorConfig.org
2+
; Install the "EditorConfig" plugin into your editor to use
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
14+
[package.json]
15+
indent_size = 2

.eslintrc.cjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
module.exports = {
4+
root: true,
5+
extends: [
6+
"eslint",
7+
"plugin:node/recommended-module"
8+
],
9+
parserOptions: {
10+
sourceType: "module",
11+
ecmaVersion: "2020"
12+
},
13+
overrides: [
14+
{
15+
files: ["tests/**/*"],
16+
env: {
17+
mocha: true
18+
}
19+
},
20+
{
21+
files: ["*.cjs"],
22+
extends: ["eslint", "plugin:node/recommended-script"],
23+
parserOptions: {
24+
sourceType: "script",
25+
ecmaVersion: "2020"
26+
}
27+
}
28+
],
29+
ignorePatterns: ["/dist", "/coverage"]
30+
};

.eslintrc.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
strategy:
2626
matrix:
2727
os: [ubuntu-latest]
28-
node: [14.x, 13.x, 12.x, 10.x, 8.x]
28+
node: [16.x, 14.x, 12.x, "12.22.0"]
2929
runs-on: ${{ matrix.os }}
3030
steps:
3131
- uses: actions/checkout@v2

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ npm-debug.log
88
/.vscode
99
.sublimelinterrc
1010
.eslint-release-info.json
11+
dist

Makefile.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,51 @@
66
*/
77
/* global echo, exec, exit, set, target */
88

9-
"use strict";
10-
119
/* eslint no-console: 0*/
1210
//------------------------------------------------------------------------------
1311
// Requirements
1412
//------------------------------------------------------------------------------
1513

16-
require("shelljs/make");
17-
set("+e");
14+
import path from "path";
15+
import { fileURLToPath } from "url";
16+
17+
import "shelljs/make.js";
18+
import checker from "npm-license";
19+
20+
const dirname = path.dirname(fileURLToPath(import.meta.url));
1821

19-
const checker = require("npm-license");
22+
// `shelljs/make.js` global command to unset any `set('-e')` (to exit upon
23+
// first error)
24+
set("+e");
2025

2126
//------------------------------------------------------------------------------
2227
// Settings
2328
//------------------------------------------------------------------------------
2429

2530
const OPEN_SOURCE_LICENSES = [
26-
/MIT/, /BSD/, /Apache/, /ISC/, /WTF/, /Public Domain/
31+
/MIT/u, /BSD/u, /Apache/u, /ISC/u, /WTF/u, /Public Domain/u
2732
];
2833

2934
//------------------------------------------------------------------------------
3035
// Data
3136
//------------------------------------------------------------------------------
3237

3338
const NODE = "node",
34-
NODE_MODULES = "./node_modules/",
39+
NODE_MODULES = "./node_modules",
3540

3641
// Utilities - intentional extra space at the end of each string
37-
MOCHA = `${NODE_MODULES}mocha/bin/_mocha `,
42+
MOCHA = `${NODE_MODULES}/mocha/bin/_mocha `,
3843
ESLINT = `${NODE} ${NODE_MODULES}/eslint/bin/eslint `,
39-
ISTANBUL = `${NODE} ${NODE_MODULES}/istanbul/lib/cli.js `,
44+
45+
// If switching back to Istanbul when may be working with ESM
46+
// ISTANBUL = `${NODE} ${NODE_MODULES}/istanbul/lib/cli.js `,
47+
C8 = `${NODE} ${NODE_MODULES}/c8/bin/c8.js`,
4048

4149
// Files
4250
MAKEFILE = "./Makefile.js",
4351
JS_FILES = "lib/**/*.js",
44-
TEST_FILES = "tests/*.js";
52+
TEST_FILES = "tests/*.js",
53+
CJS_TEST_FILES = "tests/*.cjs";
4554

4655
//------------------------------------------------------------------------------
4756
// Tasks
@@ -73,14 +82,26 @@ target.lint = function() {
7382
errors++;
7483
}
7584

85+
echo("Validating CJS JavaScript test files");
86+
lastReturn = exec(ESLINT + CJS_TEST_FILES);
87+
if (lastReturn.code !== 0) {
88+
errors++;
89+
}
90+
7691
if (errors) {
7792
exit(1);
7893
}
7994
};
8095

8196
target.test = function() {
8297
let errors = 0;
83-
const lastReturn = exec(`${ISTANBUL} cover ${MOCHA} -- -R progress -c ${TEST_FILES}`);
98+
let lastReturn = exec(`${NODE} ${MOCHA} -- -R progress -c ${CJS_TEST_FILES}`);
99+
100+
if (lastReturn.code !== 0) {
101+
errors++;
102+
}
103+
104+
lastReturn = exec(`${C8} ${MOCHA} -- -R progress -c ${TEST_FILES}`);
84105

85106
if (lastReturn.code !== 0) {
86107
errors++;
@@ -116,7 +137,7 @@ target.checkLicenses = function() {
116137
echo("Validating licenses");
117138

118139
checker.init({
119-
start: __dirname
140+
start: dirname
120141
}, deps => {
121142
const impermissible = Object.keys(deps).map(dependency => ({
122143
name: dependency,

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,47 @@
22

33
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).
44

5-
## Usage
6-
7-
Install:
5+
## Install
86

97
```
108
npm i eslint-scope --save
119
```
1210

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+
1325
Example:
1426

1527
```js
16-
var eslintScope = require('eslint-scope');
17-
var espree = require('espree');
18-
var estraverse = require('estraverse');
28+
import * as eslintScope from 'eslint-scope';
29+
import * as espree from 'espree';
30+
import estraverse from 'estraverse';
1931

20-
var ast = espree.parse(code, { range: true });
21-
var scopeManager = eslintScope.analyze(ast);
32+
const ast = espree.parse(code, { range: true });
33+
const scopeManager = eslintScope.analyze(ast);
2234

23-
var currentScope = scopeManager.acquire(ast); // global scope
35+
const currentScope = scopeManager.acquire(ast); // global scope
2436

2537
estraverse.traverse(ast, {
26-
enter: function(node, parent) {
38+
enter (node, parent) {
2739
// do stuff
2840

2941
if (/Function/.test(node.type)) {
3042
currentScope = scopeManager.acquire(node); // get current function scope
3143
}
3244
},
33-
leave: function(node, parent) {
45+
leave(node, parent) {
3446
if (/Function/.test(node.type)) {
3547
currentScope = currentScope.upper; // set to parent scope
3648
}

lib/definition.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2222
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2323
*/
24-
"use strict";
2524

26-
const Variable = require("./variable");
25+
import Variable from "./variable.js";
2726

2827
/**
29-
* @class Definition
28+
* @constructor Definition
3029
*/
3130
class Definition {
3231
constructor(type, name, node, parent, index, kind) {
3332

3433
/**
35-
* @member {String} Definition#type - type of the occurrence (e.g. "Parameter", "Variable", ...).
34+
* @member {string} Definition#type - type of the occurrence (e.g. "Parameter", "Variable", ...).
3635
*/
3736
this.type = type;
3837

@@ -52,19 +51,19 @@ class Definition {
5251
this.parent = parent;
5352

5453
/**
55-
* @member {Number?} Definition#index - the index in the declaration statement.
54+
* @member {number?} Definition#index - the index in the declaration statement.
5655
*/
5756
this.index = index;
5857

5958
/**
60-
* @member {String?} Definition#kind - the kind of the declaration statement.
59+
* @member {string?} Definition#kind - the kind of the declaration statement.
6160
*/
6261
this.kind = kind;
6362
}
6463
}
6564

6665
/**
67-
* @class ParameterDefinition
66+
* @constructor ParameterDefinition
6867
*/
6968
class ParameterDefinition extends Definition {
7069
constructor(name, node, index, rest) {
@@ -78,7 +77,7 @@ class ParameterDefinition extends Definition {
7877
}
7978
}
8079

81-
module.exports = {
80+
export {
8281
ParameterDefinition,
8382
Definition
8483
};

0 commit comments

Comments
 (0)