Skip to content

Commit acc0e47

Browse files
evanplaiceKai Cataldo
authored andcommitted
Update: support .eslintrc.cjs (refs eslint/rfcs#43) (#12321)
* Fix: ES module compatibility (fixes #12319) In ES module packages w/ "type": "module" defined treat all .js files as ES modules. CommonJS files contained in an ES module package should use the .cjs extension. * Fix Documentation * Add Tests * Fix Lint Error
1 parent 49c1658 commit acc0e47

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

docs/user-guide/configuring.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ In each case, the settings in the configuration file override default settings.
664664
ESLint supports configuration files in several formats:
665665

666666
* **JavaScript** - use `.eslintrc.js` and export an object containing your configuration.
667+
* **JavaScript (ESM)** - use `.eslintrc.cjs` when running ESLint in JavaScript packages that specify `"type":"module"` in their `package.json`. Note that ESLint does not support ESM configuration at this time.
667668
* **YAML** - use `.eslintrc.yaml` or `.eslintrc.yml` to define the configuration structure.
668669
* **JSON** - use `.eslintrc.json` to define the configuration structure. ESLint's JSON files also allow JavaScript-style comments.
669670
* **Deprecated** - use `.eslintrc`, which can be either JSON or YAML.
@@ -672,13 +673,13 @@ ESLint supports configuration files in several formats:
672673
If there are multiple configuration files in the same directory, ESLint will only use one. The priority order is:
673674

674675
1. `.eslintrc.js`
676+
1. `.eslintrc.cjs`
675677
1. `.eslintrc.yaml`
676678
1. `.eslintrc.yml`
677679
1. `.eslintrc.json`
678680
1. `.eslintrc`
679681
1. `package.json`
680682

681-
682683
## Configuration Cascading and Hierarchy
683684

684685
When using `.eslintrc.*` and `package.json` files for configuration, you can take advantage of configuration cascading. For instance, suppose you have the following structure:

lib/cli-engine/config-array-factory.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const eslintRecommendedPath = path.resolve(__dirname, "../../conf/eslint-recomme
6262
const eslintAllPath = path.resolve(__dirname, "../../conf/eslint-all.js");
6363
const configFilenames = [
6464
".eslintrc.js",
65+
".eslintrc.cjs",
6566
".eslintrc.yaml",
6667
".eslintrc.yml",
6768
".eslintrc.json",
@@ -279,6 +280,7 @@ function configMissingError(configName, importerName) {
279280
function loadConfigFile(filePath) {
280281
switch (path.extname(filePath)) {
281282
case ".js":
283+
case ".cjs":
282284
return loadJSConfigFile(filePath);
283285

284286
case ".json":
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
rules: {
3+
semi: [2, "always"]
4+
}
5+
};

tests/lib/cli-engine/config-array-factory.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ describe("ConfigArrayFactory", () => {
171171
describe("'loadFile(filePath, options)' method should load a config file.", () => {
172172
const basicFiles = {
173173
"js/.eslintrc.js": "exports.settings = { name: 'js/.eslintrc.js' }",
174+
"cjs/.eslintrc.cjs": "exports.settings = { name: 'cjs/.eslintrc.cjs' }",
174175
"json/.eslintrc.json": "{ \"settings\": { \"name\": \"json/.eslintrc.json\" } }",
175176
"legacy-json/.eslintrc": "{ \"settings\": { \"name\": \"legacy-json/.eslintrc\" } }",
176177
"legacy-yml/.eslintrc": "settings:\n name: legacy-yml/.eslintrc",
@@ -288,6 +289,7 @@ describe("ConfigArrayFactory", () => {
288289
describe("'loadInDirectory(directoryPath, options)' method should load the config file of a directory.", () => {
289290
const basicFiles = {
290291
"js/.eslintrc.js": "exports.settings = { name: 'js/.eslintrc.js' }",
292+
"cjs/.eslintrc.cjs": "exports.settings = { name: 'cjs/.eslintrc.cjs' }",
291293
"json/.eslintrc.json": "{ \"settings\": { \"name\": \"json/.eslintrc.json\" } }",
292294
"legacy-json/.eslintrc": "{ \"settings\": { \"name\": \"legacy-json/.eslintrc\" } }",
293295
"legacy-yml/.eslintrc": "settings:\n name: legacy-yml/.eslintrc",
@@ -1497,6 +1499,7 @@ describe("ConfigArrayFactory", () => {
14971499
"node_modules/eslint-plugin-invalid-parser/index.js": "exports.configs = { foo: { parser: 'nonexistent-parser' } }",
14981500
"node_modules/eslint-plugin-invalid-config/index.js": "exports.configs = { foo: {} }",
14991501
"js/.eslintrc.js": "module.exports = { rules: { semi: [2, 'always'] } };",
1502+
"cjs/.eslintrc.cjs": "module.exports = { rules: { semi: [2, 'always'] } };",
15001503
"json/.eslintrc.json": "{ \"rules\": { \"quotes\": [2, \"double\"] } }",
15011504
"package-json/package.json": "{ \"eslintConfig\": { \"env\": { \"es6\": true } } }",
15021505
"yaml/.eslintrc.yaml": "env:\n browser: true"
@@ -1747,6 +1750,22 @@ describe("ConfigArrayFactory", () => {
17471750
});
17481751
});
17491752

1753+
it("should load information from a JavaScript file with a .cjs extension", () => {
1754+
const { ConfigArrayFactory } = defineConfigArrayFactoryWithInMemoryFileSystem({
1755+
files: {
1756+
"cjs/.eslintrc.cjs": "module.exports = { rules: { semi: [2, 'always'] } };"
1757+
}
1758+
});
1759+
const factory = new ConfigArrayFactory();
1760+
const config = load(factory, "cjs/.eslintrc.cjs");
1761+
1762+
assertConfig(config, {
1763+
rules: {
1764+
semi: [2, "always"]
1765+
}
1766+
});
1767+
});
1768+
17501769
it("should throw error when loading invalid JavaScript file", () => {
17511770
const { ConfigArrayFactory } = defineConfigArrayFactoryWithInMemoryFileSystem({
17521771
files: {

0 commit comments

Comments
 (0)