Skip to content

Commit 8792464

Browse files
authored
feat: Enable eslint.config.mjs and eslint.config.cjs (#17909)
* feat: Enable eslint.config.mjs and eslint.config.cjs fixes #17863 * Update CLI description
1 parent 24ce927 commit 8792464

15 files changed

Lines changed: 190 additions & 24 deletions

File tree

docs/src/use/configure/configuration-files.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ You can put your ESLint project configuration in a configuration file. You can i
1616

1717
## Configuration File
1818

19-
The ESLint configuration file is named `eslint.config.js`. It should be placed in the root directory of your project and export an array of [configuration objects](#configuration-objects). Here's an example:
19+
The ESLint configuration file may be named any of the following:
20+
21+
* `eslint.config.js`
22+
* `eslint.config.mjs`
23+
* `eslint.config.cjs`
24+
25+
It should be placed in the root directory of your project and export an array of [configuration objects](#configuration-objects). Here's an example:
2026

2127
```js
2228
// eslint.config.js
@@ -46,25 +52,6 @@ module.exports = [
4652
];
4753
```
4854

49-
The configuration file can also export a promise that resolves to the configuration array. This can be useful for using ESM dependencies in CommonJS configuration files, as in this example:
50-
51-
```js
52-
// eslint.config.js
53-
module.exports = (async () => {
54-
55-
const someDependency = await import("some-esm-dependency");
56-
57-
return [
58-
// ... use `someDependency` here
59-
];
60-
61-
})();
62-
```
63-
64-
::: warning
65-
ESLint only automatically looks for a config file named `eslint.config.js` and does not look for `eslint.config.cjs` or `eslint.config.mjs`. If you'd like to specify a different config filename than the default, use the `--config` command line option.
66-
:::
67-
6855
## Configuration Objects
6956

7057
Each configuration object contains all of the information ESLint needs to execute on a set of files. Each configuration object is made up of these properties:
@@ -387,7 +374,7 @@ Please refer to the documentation for the shareable configuration package you're
387374

388375
## Configuration File Resolution
389376

390-
When ESLint is run on the command line, it first checks the current working directory for `eslint.config.js`. If the file is not found, it looks to the next parent directory for the file. This search continues until either the file is found or the root directory is reached.
377+
When ESLint is run on the command line, it first checks the current working directory for `eslint.config.js`. If that file is found, then the search stops, otherwise it checks for `eslint.config.mjs`. If that file is found, then the search stops, otherwise it checks for `eslint.config.cjs`. If none of the files are not found, it checks the parent directory for each file. This search continues until either a config file is found or the root directory is reached.
391378

392379
You can prevent this search for `eslint.config.js` by using the `-c` or `--config` option on the command line to specify an alternate configuration file, such as:
393380

lib/eslint/flat-eslint.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ const LintResultCache = require("../cli-engine/lint-result-cache");
9393
// Helpers
9494
//------------------------------------------------------------------------------
9595

96-
const FLAT_CONFIG_FILENAME = "eslint.config.js";
96+
const FLAT_CONFIG_FILENAMES = [
97+
"eslint.config.js",
98+
"eslint.config.mjs",
99+
"eslint.config.cjs"
100+
];
97101
const debug = require("debug")("eslint:flat-eslint");
98102
const privateMembers = new WeakMap();
99103
const importedConfigFileModificationTime = new Map();
@@ -260,7 +264,7 @@ function compareResultsByFilePath(a, b) {
260264
*/
261265
function findFlatConfigFile(cwd) {
262266
return findUp(
263-
FLAT_CONFIG_FILENAME,
267+
FLAT_CONFIG_FILENAMES,
264268
{ cwd }
265269
);
266270
}

lib/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ module.exports = function(usingFlatConfig) {
170170
alias: "c",
171171
type: "path::String",
172172
description: usingFlatConfig
173-
? "Use this configuration instead of eslint.config.js"
173+
? "Use this configuration instead of eslint.config.js, eslint.config.mjs, or eslint.config.cjs"
174174
: "Use this configuration, overriding .eslintrc.* config options if present"
175175
},
176176
envFlag,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
"no-undef": "warn"
4+
}
5+
};

tests/fixtures/cjs-config/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports= {
2+
rules: {
3+
"no-undef": "warn"
4+
}
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
"no-undef": "off"
4+
}
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
rules: {
3+
"no-undef": "error"
4+
}
5+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
"no-undef": "warn"
4+
}
5+
};

0 commit comments

Comments
 (0)