Skip to content

Commit

Permalink
feat: support eslint v8
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Sep 17, 2021
1 parent 3e5e611 commit 8c11648
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 26 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions 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();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down
16 changes: 8 additions & 8 deletions src/runner/__tests__/runESLint.test.js
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
26 changes: 17 additions & 9 deletions 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 }) => {
Expand All @@ -9,50 +9,58 @@ 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) {
// eslint-disable-next-line import/no-dynamic-require,global-require
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,
);

Expand Down
6 changes: 6 additions & 0 deletions 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')
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -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==
Expand Down

0 comments on commit 8c11648

Please sign in to comment.