Skip to content

Commit 1866e1d

Browse files
mdjermanovicnzakas
andauthoredJun 22, 2023
feat: allow flat config files to export a Promise (#17301)
* feat: allow flat config files to export a Promise Fixes #16864, #16580 * Update docs/src/use/configure/configuration-files-new.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * Update docs/src/use/configure/configuration-files-new.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> --------- Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
1 parent f82e56e commit 1866e1d

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
 

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

+28-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,38 @@ export default [
2626
"prefer-const": "error"
2727
}
2828
}
29-
]
29+
];
3030
```
3131

3232
In this example, the configuration array contains just one configuration object. The configuration object enables two rules: `semi` and `prefer-const`. These rules are applied to all of the files ESLint processes using this config file.
3333

34+
If your project does not specify `"type":"module"` in its `package.json` file, then `eslint.config.js` must be in CommonJS format, such as:
35+
36+
```js
37+
module.exports = [
38+
{
39+
rules: {
40+
semi: "error",
41+
"prefer-const": "error"
42+
}
43+
}
44+
];
45+
```
46+
47+
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:
48+
49+
```js
50+
module.exports = (async () => {
51+
52+
const someDependency = await import("some-esm-dependency");
53+
54+
return [
55+
// ... use `someDependency` here
56+
];
57+
58+
})();
59+
```
60+
3461
## Configuration Objects
3562

3663
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:

‎tests/fixtures/promise-config/a.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = "bar";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = Promise.resolve([{
2+
rules: {
3+
quotes: ["error", "single"]
4+
}
5+
}]);

‎tests/lib/eslint/flat-eslint.js

+25
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,18 @@ describe("FlatESLint", () => {
761761
eslint = new FlatESLint();
762762
await assert.rejects(() => eslint.lintText("var a = 0", { warnIgnored: "" }), /'options.warnIgnored' must be a boolean or undefined/u);
763763
});
764+
765+
it("should work with config file that exports a promise", async () => {
766+
eslint = new FlatESLint({
767+
cwd: getFixturePath("promise-config")
768+
});
769+
const results = await eslint.lintText('var foo = "bar";');
770+
771+
assert.strictEqual(results.length, 1);
772+
assert.strictEqual(results[0].messages.length, 1);
773+
assert.strictEqual(results[0].messages[0].severity, 2);
774+
assert.strictEqual(results[0].messages[0].ruleId, "quotes");
775+
});
764776
});
765777

766778
describe("lintFiles()", () => {
@@ -991,6 +1003,19 @@ describe("FlatESLint", () => {
9911003
assert.strictEqual(results[0].suppressedMessages.length, 0);
9921004
});
9931005

1006+
it("should work with config file that exports a promise", async () => {
1007+
eslint = new FlatESLint({
1008+
cwd: getFixturePath("promise-config")
1009+
});
1010+
const results = await eslint.lintFiles(["a*.js"]);
1011+
1012+
assert.strictEqual(results.length, 1);
1013+
assert.strictEqual(results[0].filePath, getFixturePath("promise-config", "a.js"));
1014+
assert.strictEqual(results[0].messages.length, 1);
1015+
assert.strictEqual(results[0].messages[0].severity, 2);
1016+
assert.strictEqual(results[0].messages[0].ruleId, "quotes");
1017+
});
1018+
9941019
// https://github.com/eslint/eslint/issues/16265
9951020
describe("Dot files in searches", () => {
9961021

0 commit comments

Comments
 (0)
Please sign in to comment.