From e3df83f45a88483c76cbc4540d904ba69d40bc84 Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Tue, 27 Sep 2022 21:45:05 -0400 Subject: [PATCH 1/7] feat: take flag config env into account --- lib/cli.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/cli.js b/lib/cli.js index 2fca65c1908..a6a2e93e127 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -278,6 +278,31 @@ async function printResults(engine, results, format, outputFile) { return true; } +/** + * Returns whether flat config should be used. + * @param {boolean} [allowFlatConfig] Whether or not to allow flat config. + * @returns {Promise} Where flat config should be used. + */ +async function shouldUseFlatConfig(allowFlatConfig) { + if (!allowFlatConfig) { + return false; + } + + switch (process.env.ESLINT_USE_FLAT_CONFIG) { + case "true": + return true; + case "false": + return false; + default: + + /* + * If neither explicitly enabled nor disabled, then use the presence + * of a flat config file to determine enablement. + */ + return !!(await findFlatConfigFile(process.cwd())); + } +} + //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ @@ -307,7 +332,7 @@ const cli = { * switch to flat config we can remove this logic. */ - const usingFlatConfig = allowFlatConfig && !!(await findFlatConfigFile(process.cwd())); + const usingFlatConfig = await shouldUseFlatConfig(allowFlatConfig); debug("Using flat config?", usingFlatConfig); From fce04b9c91f38756c65adfc56b96361c757e1bf4 Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Sun, 2 Oct 2022 14:32:07 -0400 Subject: [PATCH 2/7] test: take flag config env into account --- tests/lib/cli.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/lib/cli.js b/tests/lib/cli.js index a58a8c910fc..2a8fa3c57c9 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -137,6 +137,45 @@ describe("cli", () => { }); + describe("flat config", () => { + const originalCwd = process.cwd; + const originalEnv = { ...process.env }; + + afterEach(() => { + process.cwd = originalCwd; + process.env = { ...originalEnv }; + }); + + it(`should use it when an eslint.config.js is present and useFlatConfig is true:${configType}`, async () => { + process.cwd = getFixturePath; + + const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); + + /* + * When flat config is used, we get an exit code of 2 because + * the --ext option is unrecognized. + */ + assert.strictEqual(exitCode, useFlatConfig ? 2 : 0); + }); + + it(`should not use it when ESLINT_USE_FLAT_CONFIG=false even if an eslint.config.js is present:${configType}`, async () => { + process.cwd = getFixturePath; + process.env.ESLINT_USE_FLAT_CONFIG = "false"; + + const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); + + assert.strictEqual(exitCode, 0); + }); + + 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 () => { + process.env.ESLINT_USE_FLAT_CONFIG = "true"; + + const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); + + assert.strictEqual(exitCode, useFlatConfig ? 2 : 0); + }); + }); + describe("when given a config with rules with options and severity level set to error", () => { const originalCwd = process.cwd; From b0ee848602ed86dfad815fb819da420c7842552e Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Sun, 2 Oct 2022 14:33:13 -0400 Subject: [PATCH 3/7] style: fix comments --- tests/lib/cli.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/lib/cli.js b/tests/lib/cli.js index 2a8fa3c57c9..bca7b27aedf 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -151,10 +151,7 @@ describe("cli", () => { const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); - /* - * When flat config is used, we get an exit code of 2 because - * the --ext option is unrecognized. - */ + // When flat config is used, we get an exit code of 2 because the --ext option is unrecognized. assert.strictEqual(exitCode, useFlatConfig ? 2 : 0); }); @@ -172,6 +169,7 @@ describe("cli", () => { const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); + // When flat config is used, we get an exit code of 2 because the --ext option is unrecognized. assert.strictEqual(exitCode, useFlatConfig ? 2 : 0); }); }); From d24d78799222687fdfdd1323f3afbf1c8398857a Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Sat, 8 Oct 2022 16:38:26 -0400 Subject: [PATCH 4/7] docs: document ESLINT_USE_FLAT_CONFIG --- docs/src/user-guide/configuring/configuration-files-new.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/user-guide/configuring/configuration-files-new.md b/docs/src/user-guide/configuring/configuration-files-new.md index c5175c420a9..1624bac978d 100644 --- a/docs/src/user-guide/configuring/configuration-files-new.md +++ b/docs/src/user-guide/configuring/configuration-files-new.md @@ -10,7 +10,7 @@ eleventyNavigation: --- ::: warning -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. +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` environemnt 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. ::: ## Configuration File @@ -554,10 +554,10 @@ Here, the `eslint:recommended` predefined configuration is applied first and the 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. -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: +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: ```shell -npx eslint -c some-other-file.js **/*.js +ESLINT_USE_FLAT_CONFIG=true npx eslint -c some-other-file.js **/*.js ``` In this case, ESLint will not search for `eslint.config.js` and will instead use `some-other-file.js`. From 9ada070e68ec368c3190f55d35efc903d5b552ed Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Sat, 8 Oct 2022 16:46:11 -0400 Subject: [PATCH 5/7] fix: keep original process.env --- tests/lib/cli.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/lib/cli.js b/tests/lib/cli.js index bca7b27aedf..13851b0d7c6 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -139,11 +139,15 @@ describe("cli", () => { describe("flat config", () => { const originalCwd = process.cwd; - const originalEnv = { ...process.env }; + const originalEnv = process.env; + + beforeEach(() => { + process.env = { ...originalEnv }; + }); afterEach(() => { process.cwd = originalCwd; - process.env = { ...originalEnv }; + process.env = originalEnv; }); it(`should use it when an eslint.config.js is present and useFlatConfig is true:${configType}`, async () => { From 5b38238758df600bf2e4f0663cce8f385b55e5f1 Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Sat, 8 Oct 2022 16:46:25 -0400 Subject: [PATCH 6/7] fix: ESLINT_USE_FLAT_CONFIG=true test --- tests/lib/cli.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/lib/cli.js b/tests/lib/cli.js index 13851b0d7c6..1ff7142ad45 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -138,16 +138,16 @@ describe("cli", () => { }); describe("flat config", () => { - const originalCwd = process.cwd; const originalEnv = process.env; + const originalCwd = process.cwd; beforeEach(() => { process.env = { ...originalEnv }; }); afterEach(() => { - process.cwd = originalCwd; process.env = originalEnv; + process.cwd = originalCwd; }); it(`should use it when an eslint.config.js is present and useFlatConfig is true:${configType}`, async () => { @@ -160,8 +160,8 @@ describe("cli", () => { }); it(`should not use it when ESLINT_USE_FLAT_CONFIG=false even if an eslint.config.js is present:${configType}`, async () => { - process.cwd = getFixturePath; process.env.ESLINT_USE_FLAT_CONFIG = "false"; + process.cwd = getFixturePath; const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); @@ -171,6 +171,9 @@ describe("cli", () => { 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 () => { process.env.ESLINT_USE_FLAT_CONFIG = "true"; + // Set the CWD to outside the fixtures/ directory so that no eslint.config.js is found + process.cwd = () => getFixturePath(".."); + const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig); // When flat config is used, we get an exit code of 2 because the --ext option is unrecognized. From 1bd9d75abcfdc748206025682a844f828a617caa Mon Sep 17 00:00:00 2001 From: Tomer Aberbach Date: Wed, 12 Oct 2022 10:06:43 -0400 Subject: [PATCH 7/7] fix: typo in docs Co-authored-by: Milos Djermanovic --- docs/src/user-guide/configuring/configuration-files-new.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/user-guide/configuring/configuration-files-new.md b/docs/src/user-guide/configuring/configuration-files-new.md index 1624bac978d..ed12678a31f 100644 --- a/docs/src/user-guide/configuring/configuration-files-new.md +++ b/docs/src/user-guide/configuring/configuration-files-new.md @@ -10,7 +10,7 @@ eleventyNavigation: --- ::: warning -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` environemnt 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. +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. ::: ## Configuration File