Skip to content

Commit

Permalink
Improve logic for detecting when to use flat config format
Browse files Browse the repository at this point in the history
- Don't try to use flat config format on eslint versions that don't support it
- Respect `process.env.ESLINT_USE_FLAT_CONFIG`
  • Loading branch information
cprussin committed Apr 27, 2023
1 parent 324bfd1 commit c04ee9f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const a = 1;

console.log('a', a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
'no-console': 'error',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
config: './eslint.config.js',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
runner: '../../../',
testMatch: ['**/__eslint__/**/*.js'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Does not try to use flat config on eslint versions that don't support it 1`] = `
"PASS __eslint__/file.js
● Console
console.warn
/mocked-path-to-jest-runner-mocha/integrationTests/__fixtures__/legacy-config-at-eslint.config.js/__eslint__/file.js
3:1 warning Unexpected console statement no-console
✖ 1 problem (0 errors, 1 warning)
✓ ESLint
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time:
Ran all test suites.
"
`;
12 changes: 12 additions & 0 deletions integrationTests/legacy-config-at-eslint.config.js.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { version } = require('eslint/package.json');
const semver = require('semver');
const runJest = require('./runJest');

(semver.satisfies(version, '<8') ? it : it.skip)(
"Does not try to use flat config on eslint versions that don't support it",
async () => {
expect(
await runJest('legacy-config-at-eslint.config.js'),
).toMatchSnapshot();
},
);
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"cosmiconfig": "^7.0.0",
"create-jest-runner": "^0.11.2",
"dot-prop": "^6.0.1",
"find-up": "^5.0.0"
"find-up": "^5.0.0",
"semver": "^7.3.8"
},
"devDependencies": {
"@babel/cli": "^7.10.4",
Expand All @@ -47,8 +48,7 @@
"jest-watch-select-projects": "^2.0.0",
"jest-watch-typeahead": "^1.1.0 || ^2.1.1",
"prettier": "^2.8.4",
"rimraf": "^3.0.2",
"semver": "^7.3.8"
"rimraf": "^3.0.2"
},
"peerDependencies": {
"eslint": "^7 || ^8",
Expand Down
18 changes: 15 additions & 3 deletions src/runner/runESLint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { ESLint } = require('eslint');
const { version } = require('eslint/package.json');
const semver = require('semver');
const findUp = require('find-up');

const getESLintOptions = require('../utils/getESLintOptions');
Expand Down Expand Up @@ -98,9 +100,19 @@ const getComputedFixValue = ({ fix, quiet, fixDryRun }) => {
return undefined;
};

const useFlatConfig = async () =>
!!(await findUp('eslint.config.js', { cwd: process.cwd() }));

const useFlatConfig = async () => {
if (semver.satisfies(version, '>=8')) {
switch (process.env.ESLINT_USE_FLAT_CONFIG) {
case 'true':
return true;
case 'false':
return false;
default:
return !!(await findUp('eslint.config.js', { cwd: process.cwd() }));
}
}
return false;
};
const getESLintConstructor = async () => {
if (await useFlatConfig()) {
// Use a dynamic require here rather than a global require because this
Expand Down

0 comments on commit c04ee9f

Please sign in to comment.