Skip to content

Commit 5202a5d

Browse files
committed
feat: support eslint v8
1 parent 3e5e611 commit 5202a5d

File tree

7 files changed

+44
-26
lines changed

7 files changed

+44
-26
lines changed

.github/workflows/nodejs.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
node-version: [10.x, 12.x, 14.x, 16.x]
22-
eslint-version: [6, 7]
22+
eslint-version: [6, 7, '^8.0.0-0']
2323
jest-version: [25, 26, 27]
2424
include:
2525
# eslint@7 and jest@26 doesn't support node@8
2626
- node-version: 8.x
2727
eslint-version: 6
2828
jest-version: 25
29+
exclude:
30+
# eslint@8 doesn't support node@10
31+
- node-version: 10.x
32+
eslint-version: '^8.0.0-0'
2933
runs-on: ubuntu-latest
3034

3135
steps:

integrationTests/custom-parser.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
const eslint = require('eslint');
21
const runJest = require('./runJest');
2+
const { version } = require('../src/utils/eslintVersion');
33

44
// Note: ESLint versions <6 have a different error message for this test. The
55
// snapshot file contains both messages so we can test across both versions.
66
// Without the skipped tests for the "other" version, the tests will always fail
77
// with `1 snapshot obsolete`.
8-
if (
9-
eslint.CLIEngine.version.startsWith('4') ||
10-
eslint.CLIEngine.version.startsWith('5')
11-
) {
8+
if (version.startsWith('4') || version.startsWith('5')) {
129
it.skip("Doesn't override parser when not set", () => {});
1310
it("Doesn't override parser when not set [ESLint<6]", () => {
1411
return expect(runJest('custom-parser')).resolves.toMatchSnapshot();

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"@babel/core": "^7.10.4",
3434
"@babel/preset-env": "^7.10.4",
3535
"babel-jest": "^25.1 || ^26 || ^27",
36-
"eslint": "^6 || ^7",
36+
"eslint": "^6 || ^7 || ^8.0.0-0",
3737
"eslint-config-airbnb-base": "^14.2.0",
3838
"eslint-config-prettier": "^6.11.0",
3939
"eslint-plugin-import": "^2.22.0",
@@ -47,7 +47,7 @@
4747
"rimraf": "^3.0.2"
4848
},
4949
"peerDependencies": {
50-
"eslint": "^6 || ^7",
50+
"eslint": "^6 || ^7 || ^8.0.0-0",
5151
"jest": "^25.1 || ^26 || ^27"
5252
},
5353
"prettier": {

src/runner/__tests__/runESLint.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const runESLintRunnerWithMockedEngine = options => {
2525
return runESLint({ extraOptions: {}, ...options.runESLint });
2626
};
2727

28-
it('Requires the config setupTestFrameworkScriptFile when specified', () => {
28+
it('Requires the config setupTestFrameworkScriptFile when specified', async () => {
2929
const setupFile = path.join(__dirname, './path/to/setupFile.js');
3030

3131
let setupFileWasLoaded = false;
@@ -37,7 +37,7 @@ it('Requires the config setupTestFrameworkScriptFile when specified', () => {
3737
{ virtual: true },
3838
);
3939

40-
runESLintRunnerWithMockedEngine({
40+
await runESLintRunnerWithMockedEngine({
4141
cliEngine: {
4242
ignoredFiles: ['/path/to/file.test.js'],
4343
errorCount: 0,
@@ -53,8 +53,8 @@ it('Requires the config setupTestFrameworkScriptFile when specified', () => {
5353
expect(setupFileWasLoaded).toBeTruthy();
5454
});
5555

56-
it('Returns "skipped" when the test path is ignored', () => {
57-
const result = runESLintRunnerWithMockedEngine({
56+
it('Returns "skipped" when the test path is ignored', async () => {
57+
const result = await runESLintRunnerWithMockedEngine({
5858
cliEngine: {
5959
ignoredFiles: ['/path/to/file.test.js'],
6060
errorCount: 0,
@@ -73,8 +73,8 @@ it('Returns "skipped" when the test path is ignored', () => {
7373
});
7474
});
7575

76-
it('Returns "passed" when the test passed', () => {
77-
const result = runESLintRunnerWithMockedEngine({
76+
it('Returns "passed" when the test passed', async () => {
77+
const result = await runESLintRunnerWithMockedEngine({
7878
cliEngine: {
7979
ignoredFiles: [],
8080
errorCount: 0,
@@ -92,8 +92,8 @@ it('Returns "passed" when the test passed', () => {
9292
});
9393
});
9494

95-
it('Returns "fail" when the test failed', () => {
96-
const result = runESLintRunnerWithMockedEngine({
95+
it('Returns "fail" when the test failed', async () => {
96+
const result = await runESLintRunnerWithMockedEngine({
9797
cliEngine: {
9898
ignoredFiles: [],
9999
errorCount: 1,

src/runner/runESLint.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { pass, fail, skip } = require('create-jest-runner');
2-
const { CLIEngine } = require('eslint');
2+
const { CLIEngine, ESLint } = require('eslint');
33
const getESLintOptions = require('../utils/getESLintOptions');
44

55
const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
@@ -9,50 +9,61 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
99
return undefined;
1010
};
1111

12+
const ESLintEngine = ESLint || CLIEngine;
13+
1214
let cachedValues;
1315
const getCachedValues = (config, extraOptions) => {
1416
if (!cachedValues) {
17+
const useEngine = ESLint == null;
1518
const { cliOptions: baseCliOptions } = getESLintOptions(config);
1619
const cliOptions = {
1720
...baseCliOptions,
1821
fix: getComputedFixValue(baseCliOptions),
1922
...extraOptions,
2023
};
21-
const cli = new CLIEngine(cliOptions);
24+
const cli = useEngine ? new CLIEngine(cliOptions) : new ESLint(cliOptions);
2225
const formatter = cli.getFormatter(cliOptions.format);
2326

24-
cachedValues = { cli, formatter, cliOptions };
27+
cachedValues = {
28+
isPathIgnored: cli.isPathIgnored,
29+
lintFiles: cli.lintFiles || cli.executeOnFiles,
30+
formatter,
31+
cliOptions,
32+
};
2533
}
2634

2735
return cachedValues;
2836
};
2937

30-
const runESLint = ({ testPath, config, extraOptions }) => {
38+
const runESLint = async ({ testPath, config, extraOptions }) => {
3139
const start = Date.now();
3240

3341
if (config.setupTestFrameworkScriptFile) {
3442
// eslint-disable-next-line import/no-dynamic-require,global-require
3543
require(config.setupTestFrameworkScriptFile);
3644
}
3745

38-
const { cli, formatter, cliOptions } = getCachedValues(config, extraOptions);
46+
const { isPathIgnored, lintFiles, formatter, cliOptions } = getCachedValues(
47+
config,
48+
extraOptions,
49+
);
3950

40-
if (cli.isPathIgnored(testPath)) {
51+
if (isPathIgnored(testPath)) {
4152
const end = Date.now();
4253
return skip({ start, end, test: { path: testPath, title: 'ESLint' } });
4354
}
4455

45-
const report = cli.executeOnFiles([testPath]);
56+
const report = await lintFiles([testPath]);
4657

4758
if (cliOptions.fix && !cliOptions.fixDryRun) {
48-
CLIEngine.outputFixes(report);
59+
await ESLintEngine.outputFixes(report);
4960
}
5061

5162
const end = Date.now();
5263

5364
const message = formatter(
5465
cliOptions.quiet
55-
? CLIEngine.getErrorResults(report.results)
66+
? ESLintEngine.getErrorResults(report.results)
5667
: report.results,
5768
);
5869

src/utils/eslintVersion.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
const version = require('eslint/package.json').version
4+
5+
module.exports.version = version;
6+
module.exports.isV8 = version.startsWith('8')

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ eslint-visitor-keys@^2.0.0:
22132213
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz#f65328259305927392c938ed44eb0a5c9b2bd303"
22142214
integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw==
22152215

2216-
"eslint@^6 || ^7":
2216+
"eslint@^6 || ^7 || ^8.0.0-0":
22172217
version "7.28.0"
22182218
resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.28.0.tgz#435aa17a0b82c13bb2be9d51408b617e49c1e820"
22192219
integrity sha512-UMfH0VSjP0G4p3EWirscJEQ/cHqnT/iuH6oNZOB94nBjWbMnhGEPxsZm1eyIW0C/9jLI0Fow4W5DXLjEI7mn1g==

0 commit comments

Comments
 (0)