Skip to content

Commit

Permalink
feat: Implement alternate config lookup (#18742)
Browse files Browse the repository at this point in the history
* feat: Look up config file from file being linted

fixes #18385

* Fix ESLint class flag lookup

* Fix up tests for config lookup

* Add documentation for new config lookup flag

* Clean up directory searching behavior

* Read config files from starting from parent

* Cover edge cases

* Fix failing test

* Fix test and linting errors

* Remove .only from tests

* Fix failing test

* Incorporate TypeScript config files

* Fix lint errors

* Fix docs link

* Fix broken link

* Fix conflicts

* Update lib/config/config-loader.js

Co-authored-by: 唯然 <[email protected]>

* Update lib/config/config-loader.js

Co-authored-by: 唯然 <[email protected]>

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

Co-authored-by: Francesco Trotta <[email protected]>

* Add comment about CLI overrideConfig

* Remove duplicate code

* Fix error

* add test with a deep subdir

* Remove immediate child check

* Fix edge cases

* Clean up logic

* Ensure foo*/*.js doesn't traverse directory foo when ignored

* Ensure order of glob patterns doesn't matter

* Fix edge cases

* Don't normalize absolute path to posix

* add tests with overrideConfig and no config file

* Fix config override behavior

* Fix lint issues

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

Co-authored-by: Milos Djermanovic <[email protected]>

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

Co-authored-by: Milos Djermanovic <[email protected]>

* Update lib/config/config-loader.js

Co-authored-by: Milos Djermanovic <[email protected]>

* Update lib/config/config-loader.js

Co-authored-by: Milos Djermanovic <[email protected]>

* Update lib/config/config-loader.js

Co-authored-by: Milos Djermanovic <[email protected]>

* Update lib/config/config-loader.js

Co-authored-by: Milos Djermanovic <[email protected]>

* Update directory search to group by glob parent

* Simplify lookup

* Remove unused package

---------

Co-authored-by: 唯然 <[email protected]>
Co-authored-by: Francesco Trotta <[email protected]>
Co-authored-by: Milos Djermanovic <[email protected]>
  • Loading branch information
4 people authored Sep 25, 2024
1 parent 2d17453 commit 2ff0e51
Show file tree
Hide file tree
Showing 25 changed files with 8,825 additions and 7,453 deletions.
20 changes: 19 additions & 1 deletion docs/src/use/configure/configuration-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,22 @@ npx eslint --config some-other-file.js **/*.js

In this case, ESLint does not search for `eslint.config.js` and instead uses `some-other-file.js`.

### Experimental Configuration File Resolution

::: warning
This feature is experimental and its details may change before being finalized. This behavior will be the new lookup behavior starting in v10.0.0, but you can try it today using a feature flag.
:::

You can use the `unstable_config_lookup_from_file` flag to change the way ESLint searches for configuration files. Instead of searching from the current working directory, ESLint will search for a configuration file by first starting in the directory of the file being linted and then searching up its ancestor directories until it finds a `eslint.config.js` file (or any other extension of configuration file). This behavior is better for monorepos, where each subdirectory may have its own configuration file.

To use this feature on the command line, use the `--flag` flag:

```shell
npx eslint --flag unstable_config_lookup_from_file .
```

For more information about using feature flags, see [Feature Flags](../../flags/).

## TypeScript Configuration Files

::: warning
Expand All @@ -511,6 +527,8 @@ You need to enable this feature through the `unstable_ts_config` feature flag:
npx eslint --flag unstable_ts_config
```

For more information about using feature flags, see [Feature Flags](../../flags/).

For Deno and Bun, TypeScript configuration files are natively supported; for Node.js, you must install the optional dev dependency [`jiti`](https://github.com/unjs/jiti) in your project (this dependency is not automatically installed by ESLint):

```bash
Expand Down Expand Up @@ -560,7 +578,7 @@ ESLint does not perform type checking on your configuration file and does not ap
:::

::: warning
As of now, [`jiti`](https://github.com/unjs/jiti) does not support [Top-level `await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await)
As of now, [`jiti`](https://github.com/unjs/jiti) does not support [top-level `await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#top_level_await)
:::

### Configuration File Precedence
Expand Down
7 changes: 1 addition & 6 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,9 @@ const cli = {
// find the config file
const {
configFilePath,
basePath,
error
basePath
} = await locateConfigFileToUse({ cwd: process.cwd(), configFile }, hasUnstableTSConfigFlag);

if (error) {
throw error;
}

return ["--config", configFilePath, "--basePath", basePath];
},

Expand Down
Loading

0 comments on commit 2ff0e51

Please sign in to comment.