diff --git a/lib/cli.js b/lib/cli.js index afd1e65cbd1..a14930e9b0f 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -19,12 +19,11 @@ const fs = require("fs"), path = require("path"), { promisify } = require("util"), { ESLint } = require("./eslint"), - { FlatESLint } = require("./eslint/flat-eslint"), + { FlatESLint, shouldUseFlatConfig } = require("./eslint/flat-eslint"), createCLIOptions = require("./options"), log = require("./shared/logging"), RuntimeInfo = require("./shared/runtime-info"); const { Legacy: { naming } } = require("@eslint/eslintrc"); -const { findFlatConfigFile } = require("./eslint/flat-eslint"); const { ModuleImporter } = require("@humanwhocodes/module-importer"); const debug = require("debug")("eslint:cli"); @@ -275,31 +274,6 @@ async function printResults(engine, results, format, outputFile, resultsMeta) { 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 //------------------------------------------------------------------------------ @@ -329,7 +303,7 @@ const cli = { * switch to flat config we can remove this logic. */ - const usingFlatConfig = await shouldUseFlatConfig(allowFlatConfig); + const usingFlatConfig = allowFlatConfig && await shouldUseFlatConfig(); debug("Using flat config?", usingFlatConfig); diff --git a/lib/eslint/flat-eslint.js b/lib/eslint/flat-eslint.js index 82177fff9d2..7f16a46be19 100644 --- a/lib/eslint/flat-eslint.js +++ b/lib/eslint/flat-eslint.js @@ -1206,11 +1206,31 @@ class FlatESLint { } } +/** + * Returns whether flat config should be used. + * @returns {Promise} Whether flat config should be used. + */ +async function shouldUseFlatConfig() { + 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 //------------------------------------------------------------------------------ module.exports = { FlatESLint, - findFlatConfigFile + shouldUseFlatConfig }; diff --git a/lib/unsupported-api.js b/lib/unsupported-api.js index c1daf54d6ae..b688608ca88 100644 --- a/lib/unsupported-api.js +++ b/lib/unsupported-api.js @@ -12,7 +12,7 @@ //----------------------------------------------------------------------------- const { FileEnumerator } = require("./cli-engine/file-enumerator"); -const { FlatESLint } = require("./eslint/flat-eslint"); +const { FlatESLint, shouldUseFlatConfig } = require("./eslint/flat-eslint"); const FlatRuleTester = require("./rule-tester/flat-rule-tester"); //----------------------------------------------------------------------------- @@ -22,6 +22,7 @@ const FlatRuleTester = require("./rule-tester/flat-rule-tester"); module.exports = { builtinRules: require("./rules"), FlatESLint, + shouldUseFlatConfig, FlatRuleTester, FileEnumerator }; diff --git a/tests/lib/cli.js b/tests/lib/cli.js index cef1ab478df..a487c9553de 100644 --- a/tests/lib/cli.js +++ b/tests/lib/cli.js @@ -63,7 +63,7 @@ describe("cli", () => { const localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./flat-eslint": { FlatESLint: fakeESLint, findFlatConfigFile: () => null }, + "./flat-eslint": { FlatESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -940,7 +940,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -959,7 +959,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -990,7 +990,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1026,7 +1026,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1063,7 +1063,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1081,7 +1081,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1112,7 +1112,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1140,7 +1140,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1175,7 +1175,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1212,7 +1212,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1248,7 +1248,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); @@ -1265,7 +1265,7 @@ describe("cli", () => { localCLI = proxyquire("../../lib/cli", { "./eslint": { ESLint: fakeESLint }, - "./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null }, + "./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) }, "./shared/logging": log }); diff --git a/tests/lib/eslint/flat-eslint.js b/tests/lib/eslint/flat-eslint.js index bfccce1aa1e..2130623d40f 100644 --- a/tests/lib/eslint/flat-eslint.js +++ b/tests/lib/eslint/flat-eslint.js @@ -23,6 +23,7 @@ const proxyquire = require("proxyquire").noCallThru().noPreserveCache(); const shell = require("shelljs"); const hash = require("../../../lib/cli-engine/hash"); const { unIndent, createCustomTeardown } = require("../../_utils"); +const { shouldUseFlatConfig } = require("../../../lib/eslint/flat-eslint"); const coreRules = require("../../../lib/rules"); //------------------------------------------------------------------------------ @@ -5478,3 +5479,77 @@ describe("FlatESLint", () => { }); }); + +describe("shouldUseFlatConfig", () => { + + /** + * Check that `shouldUseFlatConfig` returns the expected value from a CWD + * with a flat config and one without a flat config. + * @param {boolean} expectedValueWithConfig the expected return value of + * `shouldUseFlatConfig` when in a directory with a flat config present + * @param {boolean} expectedValueWithoutConfig the expected return value of + * `shouldUseFlatConfig` when in a directory without any flat config present + * @returns {void} + */ + function testShouldUseFlatConfig(expectedValueWithConfig, expectedValueWithoutConfig) { + describe("when there is a flat config file present", () => { + const originalDir = process.cwd(); + + beforeEach(() => { + process.chdir(__dirname); + }); + + afterEach(() => { + process.chdir(originalDir); + }); + + it(`is \`${expectedValueWithConfig}\``, async () => { + assert.strictEqual(await shouldUseFlatConfig(), expectedValueWithConfig); + }); + }); + + describe("when there is no flat config file present", () => { + const originalDir = process.cwd(); + + beforeEach(() => { + process.chdir(os.tmpdir()); + }); + + afterEach(() => { + process.chdir(originalDir); + }); + + it(`is \`${expectedValueWithoutConfig}\``, async () => { + assert.strictEqual(await shouldUseFlatConfig(), expectedValueWithoutConfig); + }); + }); + } + + describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is `'true'`", () => { + beforeEach(() => { + process.env.ESLINT_USE_FLAT_CONFIG = true; + }); + + afterEach(() => { + delete process.env.ESLINT_USE_FLAT_CONFIG; + }); + + testShouldUseFlatConfig(true, true); + }); + + describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is `'false'`", () => { + beforeEach(() => { + process.env.ESLINT_USE_FLAT_CONFIG = false; + }); + + afterEach(() => { + delete process.env.ESLINT_USE_FLAT_CONFIG; + }); + + testShouldUseFlatConfig(false, false); + }); + + describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is unset", () => { + testShouldUseFlatConfig(true, false); + }); +}); diff --git a/tests/lib/unsupported-api.js b/tests/lib/unsupported-api.js index 53b466adf06..3a65ba230f8 100644 --- a/tests/lib/unsupported-api.js +++ b/tests/lib/unsupported-api.js @@ -27,6 +27,10 @@ describe("unsupported-api", () => { assert.isFunction(api.FlatESLint); }); + it("should have shouldUseFlatConfig exposed", () => { + assert.isFunction(api.shouldUseFlatConfig); + }); + it("should have FlatRuleTester exposed", () => { assert.isFunction(api.FlatRuleTester); });