According to the ESLint documentation, when ESLint is run, it searches for configuration files in the current directory and all parent directories up to and including the root directory (/), the home directory, or a directory where the root: true option is specified in an ESLint configuration file.
Is it possible to configure ESLint to check for ESLint configuration files in certain parent directories, but not others?
To make things more concrete, take the example of the Drupal 9 based project that I am working on currently. The project tree has this general shape:
.
├── .eslintrc.js
├── node_modules
├── package-lock.json
├── package.json
└── web
├── .eslintrc.json
└── modules
└── custom
└── some_module
├── .eslintrc.js
├── node_modules
├── package-lock.json
└── package.json
As you can see, the project has an ESLint configuration file at the root of the repository, as many projects do, but there is also an ESLint configuration file in the some_module directory for settings which are specific to that module.
So far so good. However, Drupal 9 also distributes its own, internal ESLint configuration file in the web directory. That configuration file would be helpful if I were working on Drupal core, but I'm not. Nonetheless, its presence between the two ESLint configuration files that I actually care about interferes with my desired ESLint configuration and actually causes ESLint invocations from the root of the repository to fail.
Is it possible to configure ESLint to read the some_module/.eslintrc.js configuration file and the .eslintrc.js configuration file at the root of the repository, but ignore the web/.eslintrc.json in the middle?
After some research and thought, I realized that the extends keyword can help solve this problem. I ended up adding the following to the some_module/.eslintrc.js configuration file:
module.exports = {
// Do not search for ESLint configuration files in parent directories because
// Drupal 9 puts one in the *web* directory which we do not want applied.
//
// However, do manually extend from the ESLint configuration at the root of
// this repository.
root: true,
extends: ["../../../../.eslintrc.js"],
// [other ESlint configuration]
};
These two lines tell ESLint that it should not search for ESLint configuration files in parent directories of some_module, but that it should manually read and apply the ESLint configuration at the root of the repository, in effect skipping the web/.eslintrc.json configuration file altogether.