Skip to content

Commit e1ac05e

Browse files
authored
refactor: mark ESLint.findConfigFile() as async, add missing docs (#20157)
docs: add missing docs for `ESLint.findConfigFile()`
1 parent 90a71bf commit e1ac05e

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

docs/src/integrate/nodejs-api.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,24 @@ This method calculates the configuration for a given file, which can be useful f
259259
- (`Promise<Object>`)<br>
260260
The promise that will be fulfilled with a configuration object.
261261

262+
### ◆ eslint.findConfigFile(filePath)
263+
264+
```js
265+
const configFilePath = await eslint.findConfigFile(filePath);
266+
```
267+
268+
This method finds the configuration file that this `ESLint` instance would use based on the options passed to the constructor.
269+
270+
#### Parameters
271+
272+
- `filePath` (`string`)<br>
273+
Optional. The path of a file for which to find the associated config file. If omitted, ESLint determines the config file based on the current working directory of this instance.
274+
275+
#### Return Value
276+
277+
- (`Promise<string | undefined>`)<br>
278+
The promise that will be fulfilled with the absolute path to the config file being used, or `undefined` when no config file is used (for example, when `overrideConfigFile: true` is set).
279+
262280
### ◆ eslint.isPathIgnored(filePath)
263281

264282
```js

lib/eslint/eslint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ class ESLint {
12991299
* @returns {Promise<string|undefined>} The path to the config file being used or
13001300
* `undefined` if no config file is being used.
13011301
*/
1302-
findConfigFile(filePath) {
1302+
async findConfigFile(filePath) {
13031303
const options = privateMembers.get(this).options;
13041304

13051305
/*

tests/lib/eslint/eslint.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10862,6 +10862,60 @@ describe("ESLint", () => {
1086210862
configFilePath,
1086310863
);
1086410864
});
10865+
10866+
it("should return undefined when overrideConfigFile is true even when filePath is provided", async () => {
10867+
const engine = new ESLint({
10868+
flags,
10869+
overrideConfigFile: true,
10870+
cwd: getFixturePath("lookup-from-file"),
10871+
});
10872+
10873+
const filePath = path.join("subdir", "code.js");
10874+
assert.strictEqual(
10875+
await engine.findConfigFile(filePath),
10876+
void 0,
10877+
);
10878+
});
10879+
10880+
it("should return custom config file path when overrideConfigFile is a nonempty string even when filePath is provided", async () => {
10881+
const engine = new ESLint({
10882+
flags,
10883+
overrideConfigFile: "my-config.js",
10884+
});
10885+
10886+
const configFilePath = path.resolve(
10887+
__dirname,
10888+
"../../../my-config.js",
10889+
);
10890+
10891+
assert.strictEqual(
10892+
await engine.findConfigFile("some/file.js"),
10893+
configFilePath,
10894+
);
10895+
});
10896+
10897+
it("should return the config file relative to the provided filePath when specified", async () => {
10898+
const engine = new ESLint({
10899+
flags,
10900+
cwd: getFixturePath("lookup-from-file"),
10901+
});
10902+
10903+
const foundConfig = await engine.findConfigFile(
10904+
path.join("subdir", "code.js"),
10905+
);
10906+
10907+
const expectedConfig = flags.includes(
10908+
"v10_config_lookup_from_file",
10909+
)
10910+
? getFixturePath(
10911+
"lookup-from-file",
10912+
"subdir",
10913+
"eslint.config.js",
10914+
)
10915+
: getFixturePath("lookup-from-file", "eslint.config.js");
10916+
10917+
assert.strictEqual(foundConfig, expectedConfig);
10918+
});
1086510919
});
1086610920

1086710921
describe("Use stats option", () => {

0 commit comments

Comments
 (0)