Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow flat config files to export a Promise #17301

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion docs/src/use/configure/configuration-files-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,38 @@ export default [
"prefer-const": "error"
}
}
]
];
```

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.

If your project does not specify `"type":"module"` in its `package.json` file, then `eslint.config.js` must be in CommonJS format, such as:

```js
module.exports = [
{
rules: {
semi: "error",
"prefer-const": "error"
}
}
];
```

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:

```js
module.exports = (async () => {

const someDependency = await import("some-esm-dependency");

return [
// ... use `someDependency` here
];

})();
```

## Configuration Objects

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:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/promise-config/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = "bar";
5 changes: 5 additions & 0 deletions tests/fixtures/promise-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = Promise.resolve([{
rules: {
quotes: ["error", "single"]
}
}]);
25 changes: 25 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,18 @@ describe("FlatESLint", () => {
eslint = new FlatESLint();
await assert.rejects(() => eslint.lintText("var a = 0", { warnIgnored: "" }), /'options.warnIgnored' must be a boolean or undefined/u);
});

it("should work with config file that exports a promise", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("promise-config")
});
const results = await eslint.lintText('var foo = "bar";');

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].messages.length, 1);
assert.strictEqual(results[0].messages[0].severity, 2);
assert.strictEqual(results[0].messages[0].ruleId, "quotes");
});
});

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

it("should work with config file that exports a promise", async () => {
eslint = new FlatESLint({
cwd: getFixturePath("promise-config")
});
const results = await eslint.lintFiles(["a*.js"]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, getFixturePath("promise-config", "a.js"));
assert.strictEqual(results[0].messages.length, 1);
assert.strictEqual(results[0].messages[0].severity, 2);
assert.strictEqual(results[0].messages[0].ruleId, "quotes");
});

// https://github.com/eslint/eslint/issues/16265
describe("Dot files in searches", () => {

Expand Down