From 8c116485c3466ba83a5758098124f43ced7c86f5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 17 Sep 2021 08:53:23 +0200 Subject: [PATCH] feat: support eslint v8 --- .github/workflows/nodejs.yml | 6 +++++- integrationTests/custom-parser.test.js | 7 ++----- package.json | 4 ++-- src/runner/__tests__/runESLint.test.js | 16 ++++++++-------- src/runner/runESLint.js | 26 +++++++++++++++++--------- src/utils/eslintVersion.js | 6 ++++++ yarn.lock | 2 +- 7 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 src/utils/eslintVersion.js diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index adde224..5d02419 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -19,13 +19,17 @@ jobs: fail-fast: false matrix: node-version: [10.x, 12.x, 14.x, 16.x] - eslint-version: [6, 7] + eslint-version: [6, 7, '^8.0.0-0'] jest-version: [25, 26, 27] include: # eslint@7 and jest@26 doesn't support node@8 - node-version: 8.x eslint-version: 6 jest-version: 25 + exclude: + # eslint@8 doesn't support node@10 + - node-version: 10.x + eslint-version: '^8.0.0-0' runs-on: ubuntu-latest steps: diff --git a/integrationTests/custom-parser.test.js b/integrationTests/custom-parser.test.js index bdb38b1..ba1b5db 100644 --- a/integrationTests/custom-parser.test.js +++ b/integrationTests/custom-parser.test.js @@ -1,14 +1,11 @@ -const eslint = require('eslint'); const runJest = require('./runJest'); +const { version } = require('../src/utils/eslintVersion'); // Note: ESLint versions <6 have a different error message for this test. The // snapshot file contains both messages so we can test across both versions. // Without the skipped tests for the "other" version, the tests will always fail // with `1 snapshot obsolete`. -if ( - eslint.CLIEngine.version.startsWith('4') || - eslint.CLIEngine.version.startsWith('5') -) { +if (version.startsWith('4') || version.startsWith('5')) { it.skip("Doesn't override parser when not set", () => {}); it("Doesn't override parser when not set [ESLint<6]", () => { return expect(runJest('custom-parser')).resolves.toMatchSnapshot(); diff --git a/package.json b/package.json index a3230b3..7e26672 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "@babel/core": "^7.10.4", "@babel/preset-env": "^7.10.4", "babel-jest": "^25.1 || ^26 || ^27", - "eslint": "^6 || ^7", + "eslint": "^6 || ^7 || ^8.0.0-0", "eslint-config-airbnb-base": "^14.2.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-import": "^2.22.0", @@ -47,7 +47,7 @@ "rimraf": "^3.0.2" }, "peerDependencies": { - "eslint": "^6 || ^7", + "eslint": "^6 || ^7 || ^8.0.0-0", "jest": "^25.1 || ^26 || ^27" }, "prettier": { diff --git a/src/runner/__tests__/runESLint.test.js b/src/runner/__tests__/runESLint.test.js index 50e644f..d0f5cc4 100644 --- a/src/runner/__tests__/runESLint.test.js +++ b/src/runner/__tests__/runESLint.test.js @@ -25,7 +25,7 @@ const runESLintRunnerWithMockedEngine = options => { return runESLint({ extraOptions: {}, ...options.runESLint }); }; -it('Requires the config setupTestFrameworkScriptFile when specified', () => { +it('Requires the config setupTestFrameworkScriptFile when specified', async () => { const setupFile = path.join(__dirname, './path/to/setupFile.js'); let setupFileWasLoaded = false; @@ -37,7 +37,7 @@ it('Requires the config setupTestFrameworkScriptFile when specified', () => { { virtual: true }, ); - runESLintRunnerWithMockedEngine({ + await runESLintRunnerWithMockedEngine({ cliEngine: { ignoredFiles: ['/path/to/file.test.js'], errorCount: 0, @@ -53,8 +53,8 @@ it('Requires the config setupTestFrameworkScriptFile when specified', () => { expect(setupFileWasLoaded).toBeTruthy(); }); -it('Returns "skipped" when the test path is ignored', () => { - const result = runESLintRunnerWithMockedEngine({ +it('Returns "skipped" when the test path is ignored', async () => { + const result = await runESLintRunnerWithMockedEngine({ cliEngine: { ignoredFiles: ['/path/to/file.test.js'], errorCount: 0, @@ -73,8 +73,8 @@ it('Returns "skipped" when the test path is ignored', () => { }); }); -it('Returns "passed" when the test passed', () => { - const result = runESLintRunnerWithMockedEngine({ +it('Returns "passed" when the test passed', async () => { + const result = await runESLintRunnerWithMockedEngine({ cliEngine: { ignoredFiles: [], errorCount: 0, @@ -92,8 +92,8 @@ it('Returns "passed" when the test passed', () => { }); }); -it('Returns "fail" when the test failed', () => { - const result = runESLintRunnerWithMockedEngine({ +it('Returns "fail" when the test failed', async () => { + const result = await runESLintRunnerWithMockedEngine({ cliEngine: { ignoredFiles: [], errorCount: 1, diff --git a/src/runner/runESLint.js b/src/runner/runESLint.js index 374adee..6378d5e 100644 --- a/src/runner/runESLint.js +++ b/src/runner/runESLint.js @@ -1,5 +1,5 @@ const { pass, fail, skip } = require('create-jest-runner'); -const { CLIEngine } = require('eslint'); +const { CLIEngine, ESLint } = require('eslint'); const getESLintOptions = require('../utils/getESLintOptions'); const getComputedFixValue = ({ fix, quiet, fixDryRun }) => { @@ -9,25 +9,33 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => { return undefined; }; +const ESLintEngine = ESLint || CLIEngine + let cachedValues; const getCachedValues = (config, extraOptions) => { if (!cachedValues) { + const useEngine = ESLint == null; const { cliOptions: baseCliOptions } = getESLintOptions(config); const cliOptions = { ...baseCliOptions, fix: getComputedFixValue(baseCliOptions), ...extraOptions, }; - const cli = new CLIEngine(cliOptions); + const cli = useEngine ? new CLIEngine(cliOptions) : new ESLint(cliOptions); const formatter = cli.getFormatter(cliOptions.format); - cachedValues = { cli, formatter, cliOptions }; + cachedValues = { + isPathIgnored: cli.isPathIgnored, + lintFiles: cli.lintFiles || cli.executeOnFiles, + formatter, + cliOptions, + }; } return cachedValues; }; -const runESLint = ({ testPath, config, extraOptions }) => { +const runESLint = async ({ testPath, config, extraOptions }) => { const start = Date.now(); if (config.setupTestFrameworkScriptFile) { @@ -35,24 +43,24 @@ const runESLint = ({ testPath, config, extraOptions }) => { require(config.setupTestFrameworkScriptFile); } - const { cli, formatter, cliOptions } = getCachedValues(config, extraOptions); + const { isPathIgnored, lintFiles, formatter, cliOptions } = getCachedValues(config, extraOptions); - if (cli.isPathIgnored(testPath)) { + if (isPathIgnored(testPath)) { const end = Date.now(); return skip({ start, end, test: { path: testPath, title: 'ESLint' } }); } - const report = cli.executeOnFiles([testPath]); + const report = await lintFiles([testPath]); if (cliOptions.fix && !cliOptions.fixDryRun) { - CLIEngine.outputFixes(report); + await ESLintEngine.outputFixes(report); } const end = Date.now(); const message = formatter( cliOptions.quiet - ? CLIEngine.getErrorResults(report.results) + ? ESLintEngine.getErrorResults(report.results) : report.results, ); diff --git a/src/utils/eslintVersion.js b/src/utils/eslintVersion.js new file mode 100644 index 0000000..6add208 --- /dev/null +++ b/src/utils/eslintVersion.js @@ -0,0 +1,6 @@ +'use strict'; + +const version = require('eslint/package.json').version + +module.exports.version = version; +module.exports.isV8 = version.startsWith('8') diff --git a/yarn.lock b/yarn.lock index 0c39016..37ef7aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2213,7 +2213,7 @@ eslint-visitor-keys@^2.0.0: resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz#f65328259305927392c938ed44eb0a5c9b2bd303" integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw== -"eslint@^6 || ^7": +"eslint@^6 || ^7 || ^8.0.0-0": version "7.28.0" resolved "https://registry.yarnpkg.com/eslint/-/eslint-7.28.0.tgz#435aa17a0b82c13bb2be9d51408b617e49c1e820" integrity sha512-UMfH0VSjP0G4p3EWirscJEQ/cHqnT/iuH6oNZOB94nBjWbMnhGEPxsZm1eyIW0C/9jLI0Fow4W5DXLjEI7mn1g==