Skip to content

Commit e940be7

Browse files
feat: Use ESLINT_USE_FLAT_CONFIG environment variable for flat config (#16356)
* feat: take flag config env into account * test: take flag config env into account * style: fix comments * docs: document ESLINT_USE_FLAT_CONFIG * fix: keep original process.env * fix: ESLINT_USE_FLAT_CONFIG=true test * fix: typo in docs Co-authored-by: Milos Djermanovic <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]>
1 parent d336cfc commit e940be7

3 files changed

Lines changed: 73 additions & 4 deletions

File tree

docs/src/user-guide/configuring/configuration-files-new.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ eleventyNavigation:
1010
---
1111

1212
::: warning
13-
This is an experimental feature. To opt-in, place a `eslint.config.js` file in the root of your project. If you are using the API, you can use the configuration system described on this page by using the `FlatESLint` class, the `FlatRuleTester` class, or by setting `configType: "flat"` in the `Linter` class.
13+
This is an experimental feature. To opt-in, place a `eslint.config.js` file in the root of your project or set the `ESLINT_USE_FLAT_CONFIG` environment variable to `true`. To opt-out, even in the presence of a `eslint.config.js` file, set the environment variable to `false`. If you are using the API, you can use the configuration system described on this page by using the `FlatESLint` class, the `FlatRuleTester` class, or by setting `configType: "flat"` in the `Linter` class.
1414
:::
1515

1616
## Configuration File
@@ -585,10 +585,10 @@ Here, the `eslint:recommended` predefined configuration is applied first and the
585585

586586
When ESLint is run on the command line, it first checks the current working directory for `eslint.config.js`, and if not found, will look to the next parent directory for the file. This search continues until either the file is found or the root directory is reached.
587587

588-
You can prevent this search for `eslint.config.js` by using the `-c` or `--config--file` option on the command line to specify an alternate configuration file, such as:
588+
You can prevent this search for `eslint.config.js` by setting the `ESLINT_USE_FLAT_CONFIG` environment variable to `true` and using the `-c` or `--config` option on the command line to specify an alternate configuration file, such as:
589589

590590
```shell
591-
npx eslint -c some-other-file.js **/*.js
591+
ESLINT_USE_FLAT_CONFIG=true npx eslint -c some-other-file.js **/*.js
592592
```
593593

594594
In this case, ESLint will not search for `eslint.config.js` and will instead use `some-other-file.js`.

lib/cli.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,31 @@ async function printResults(engine, results, format, outputFile, resultsMeta) {
279279
return true;
280280
}
281281

282+
/**
283+
* Returns whether flat config should be used.
284+
* @param {boolean} [allowFlatConfig] Whether or not to allow flat config.
285+
* @returns {Promise<boolean>} Where flat config should be used.
286+
*/
287+
async function shouldUseFlatConfig(allowFlatConfig) {
288+
if (!allowFlatConfig) {
289+
return false;
290+
}
291+
292+
switch (process.env.ESLINT_USE_FLAT_CONFIG) {
293+
case "true":
294+
return true;
295+
case "false":
296+
return false;
297+
default:
298+
299+
/*
300+
* If neither explicitly enabled nor disabled, then use the presence
301+
* of a flat config file to determine enablement.
302+
*/
303+
return !!(await findFlatConfigFile(process.cwd()));
304+
}
305+
}
306+
282307
//------------------------------------------------------------------------------
283308
// Public Interface
284309
//------------------------------------------------------------------------------
@@ -308,7 +333,7 @@ const cli = {
308333
* switch to flat config we can remove this logic.
309334
*/
310335

311-
const usingFlatConfig = allowFlatConfig && !!(await findFlatConfigFile(process.cwd()));
336+
const usingFlatConfig = await shouldUseFlatConfig(allowFlatConfig);
312337

313338
debug("Using flat config?", usingFlatConfig);
314339

tests/lib/cli.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,50 @@ describe("cli", () => {
137137

138138
});
139139

140+
describe("flat config", () => {
141+
const originalEnv = process.env;
142+
const originalCwd = process.cwd;
143+
144+
beforeEach(() => {
145+
process.env = { ...originalEnv };
146+
});
147+
148+
afterEach(() => {
149+
process.env = originalEnv;
150+
process.cwd = originalCwd;
151+
});
152+
153+
it(`should use it when an eslint.config.js is present and useFlatConfig is true:${configType}`, async () => {
154+
process.cwd = getFixturePath;
155+
156+
const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig);
157+
158+
// When flat config is used, we get an exit code of 2 because the --ext option is unrecognized.
159+
assert.strictEqual(exitCode, useFlatConfig ? 2 : 0);
160+
});
161+
162+
it(`should not use it when ESLINT_USE_FLAT_CONFIG=false even if an eslint.config.js is present:${configType}`, async () => {
163+
process.env.ESLINT_USE_FLAT_CONFIG = "false";
164+
process.cwd = getFixturePath;
165+
166+
const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig);
167+
168+
assert.strictEqual(exitCode, 0);
169+
});
170+
171+
it(`should use it when ESLINT_USE_FLAT_CONFIG=true and useFlatConfig is true even if an eslint.config.js is not present:${configType}`, async () => {
172+
process.env.ESLINT_USE_FLAT_CONFIG = "true";
173+
174+
// Set the CWD to outside the fixtures/ directory so that no eslint.config.js is found
175+
process.cwd = () => getFixturePath("..");
176+
177+
const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig);
178+
179+
// When flat config is used, we get an exit code of 2 because the --ext option is unrecognized.
180+
assert.strictEqual(exitCode, useFlatConfig ? 2 : 0);
181+
});
182+
});
183+
140184
describe("when given a config with rules with options and severity level set to error", () => {
141185

142186
const originalCwd = process.cwd;

0 commit comments

Comments
 (0)