Skip to content

Commit 2d740b7

Browse files
committed
feat: support ESLint 8.x
1 parent 7e4c01b commit 2d740b7

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

.github/workflows/node.yml

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
matrix:
2626
node-version: ${{ fromJson(needs.matrix.outputs.majors) }}
2727
eslint:
28+
- 8
2829
- 7
2930
- 6
3031
- 5
@@ -52,6 +53,15 @@ jobs:
5253
eslint: 4
5354
- node-version: 4
5455
eslint: 3
56+
exclude:
57+
- node-version: 15
58+
eslint: 8
59+
- node-version: 13
60+
eslint: 8
61+
- node-version: 11
62+
eslint: 8
63+
- node-version: 10
64+
eslint: 8
5565

5666
steps:
5767
- uses: actions/checkout@v2
@@ -67,6 +77,8 @@ jobs:
6777
if: ${{ !matrix.ajv && matrix.eslint == 4 }}
6878
- run: npm install --no-save "ajv@6"
6979
if: ${{ !matrix.ajv && matrix.eslint == 5 }}
80+
- run: npm install --no-save @eslint/eslintrc@0
81+
if: ${{ matrix.eslint != 8 }}
7082
- run: npm prune > /dev/null
7183
- run: npm ls > /dev/null
7284
- run: npm uninstall --no-save @eslint/eslintrc

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"license": "MIT",
3333
"dependencies": {
34-
"@eslint/eslintrc": "^0.1.3",
34+
"@eslint/eslintrc": "^0.4.3 || ^1.0.2",
3535
"cliui": "^3.2.0",
3636
"eslint-rule-documentation": "^1.0.23",
3737
"glob": "^7.1.6",
@@ -50,8 +50,8 @@
5050
"codecov": "^2.3.1",
5151
"commitizen": "^2.10.1",
5252
"cz-conventional-changelog": "^2.1.0",
53-
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7",
54-
"eslint-plugin-json": "^2.1.2",
53+
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7 || ^8",
54+
"eslint-plugin-json": "^3.1.0",
5555
"ghooks": "^2.0.4",
5656
"in-publish": "^2.0.1",
5757
"mocha": "^3.5.3",
@@ -65,7 +65,7 @@
6565
"validate-commit-msg": "^2.14.0"
6666
},
6767
"peerDependencies": {
68-
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7"
68+
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7 || ^8"
6969
},
7070
"nyc": {
7171
"exclude": [

src/lib/rule-finder.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path');
22

3-
const eslint = require('eslint');
3+
const { CLIEngine, ESLint, Linter } = require('eslint');
44
const glob = require('glob');
55
const isAbsolute = require('path-is-absolute');
66
const difference = require('./array-diff');
@@ -19,15 +19,29 @@ function _getConfigFile(specifiedFile) {
1919
}
2020

2121
function _getConfigs(configFile, files) {
22-
const cliEngine = new eslint.CLIEngine({
22+
if (ESLint) { // ESLint 7+
23+
const cliEngine = new ESLint({
24+
// Ignore any config applicable depending on the location on the filesystem
25+
useEslintrc: false,
26+
// Point to the particular config
27+
overrideConfigFile: configFile
28+
});
29+
30+
return new Set(files
31+
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.calculateConfigForFile(filePath))
32+
.filter(Boolean));
33+
}
34+
35+
const cliEngine = new CLIEngine({
2336
// Ignore any config applicable depending on the location on the filesystem
2437
useEslintrc: false,
2538
// Point to the particular config
2639
configFile
2740
});
41+
2842
return new Set(files
29-
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.getConfigForFile(filePath))
30-
.filter(Boolean));
43+
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.getConfigForFile(filePath))
44+
.filter(Boolean));
3145
}
3246

3347
function _getConfig(configFile, files) {
@@ -67,7 +81,7 @@ function _getPluginRules(config) {
6781
}
6882

6983
function _getCoreRules() {
70-
return eslint.linter.getRules();
84+
return new Linter().getRules();
7185
}
7286

7387
function _filterRuleNames(ruleNames, rules, predicate) {

test/lib/rule-finder.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ const proxyquire = require('proxyquire');
44

55
let ModuleResolver;
66
try {
7-
// eslint 7.12+
8-
ModuleResolver = require('@eslint/eslintrc/lib/shared/relative-module-resolver');
7+
// ESLint 7.12+
8+
ModuleResolver = require('@eslint/eslintrc').Legacy.ModuleResolver;
99
} catch (err) {
1010
if (err.code !== 'MODULE_NOT_FOUND') {
1111
throw err;
1212
}
13+
1314
try {
14-
// eslint v6 - v7.11: load the actual module
15+
// ESLint v6 - v7.11: load the actual module
1516
ModuleResolver = require('eslint/lib/shared/relative-module-resolver');
1617
} catch (err) {
1718
if (err.code !== 'MODULE_NOT_FOUND') {
1819
throw err;
1920
}
20-
// eslint < 6: ModuleResolver is `undefined`, which is okay. The proxyquire
21+
22+
// ESLint < 6: ModuleResolver is `undefined`, which is okay. The proxyquire
2123
// override for ../shared/relative-module-resolver won't be used because
22-
// eslint < 6 does not have that module and so does not try to load it.
24+
// ESLint < 6 does not have that module and so does not try to load it.
2325
ModuleResolver = undefined;
2426
}
2527
}
@@ -54,27 +56,27 @@ const moduleResolver = {
5456

5557
const getRuleFinder = proxyquire('../../src/lib/rule-finder', {
5658
eslint: {
57-
linter: {
59+
Linter: () => ({
5860
getRules() {
5961
return new Map()
6062
.set('foo-rule', {})
6163
.set('old-rule', {meta: {deprecated: true}})
6264
.set('bar-rule', {})
6365
.set('baz-rule', {});
6466
}
65-
}
67+
})
6668
},
6769
//
68-
// This following module override is needed for eslint v6 and over. The module
69-
// path that we pass here is literally the one used in eslint (specifically in
70+
// This following module override is needed for ESLint v6 and over. The module
71+
// path that we pass here is literally the one used in ESLint (specifically in
7072
// eslint/lib/cli-engine/config-array-factory.js)
7173
//
7274
// The stock `resolve` method attempts to resolve to a file path the module
7375
// name passed in `name` relative to the path in `relative`. We have to
74-
// override that function, otherwise eslint fails to "load" our plugins.
76+
// override that function, otherwise ESLint fails to "load" our plugins.
7577
//
76-
'../shared/relative-module-resolver': moduleResolver, // in eslint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
77-
'./shared/relative-module-resolver': moduleResolver, // in eslint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
78+
'../shared/relative-module-resolver': moduleResolver, // in ESLint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
79+
'./shared/relative-module-resolver': moduleResolver, // in ESLint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
7880
'eslint-plugin-plugin': {
7981
rules: {
8082
'foo-rule': {},
@@ -156,19 +158,19 @@ const dedupeModuleResolver = {
156158
};
157159
const getRuleFinderForDedupeTests = proxyquire('../../src/lib/rule-finder', {
158160
eslint: {
159-
linter: {
161+
Linter: () => ({
160162
getRules() {
161163
return new Map()
162164
.set('foo-rule', {})
163165
.set('bar-rule', {})
164166
.set('plugin/duplicate-foo-rule', {})
165167
.set('plugin/duplicate-bar-rule', {});
166168
}
167-
}
169+
})
168170
},
169171
// See the long comment in `getRuleFinder` above to learn what the point of this override is.
170-
'../shared/relative-module-resolver': dedupeModuleResolver, // in eslint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
171-
'./shared/relative-module-resolver': dedupeModuleResolver, // in eslint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
172+
'../shared/relative-module-resolver': dedupeModuleResolver, // in ESLint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
173+
'./shared/relative-module-resolver': dedupeModuleResolver, // in ESLint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
172174
'eslint-plugin-plugin': {
173175
rules: {
174176
'duplicate-foo-rule': {},

0 commit comments

Comments
 (0)