From c7f2be2f1453c12712d8488ef0e7f0d84680fb8a Mon Sep 17 00:00:00 2001 From: Connor Prussin Date: Mon, 8 May 2023 13:48:11 -0700 Subject: [PATCH] feat: Expose `shouldUseFlatConfig` (#17129) --- lib/cli.js | 28 +----------- lib/eslint/flat-eslint.js | 27 ++++++++++- lib/unsupported-api.js | 3 +- tests/lib/cli.js | 26 +++++------ tests/lib/eslint/flat-eslint.js | 80 +++++++++++++++++++++++++++++++++ tests/lib/unsupported-api.js | 4 ++ 6 files changed, 126 insertions(+), 42 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index afd1e65cbd1c..48aabcde0786 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 //------------------------------------------------------------------------------ diff --git a/lib/eslint/flat-eslint.js b/lib/eslint/flat-eslint.js index 8444153076d1..aa749de0173a 100644 --- a/lib/eslint/flat-eslint.js +++ b/lib/eslint/flat-eslint.js @@ -1212,11 +1212,36 @@ class FlatESLint { } } +/** + * 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 //------------------------------------------------------------------------------ module.exports = { FlatESLint, - findFlatConfigFile + shouldUseFlatConfig }; diff --git a/lib/unsupported-api.js b/lib/unsupported-api.js index c1daf54d6aeb..b688608ca884 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 cef1ab478df7..a487c9553def 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 ce79693af1b3..43dc870ec797 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"); //------------------------------------------------------------------------------ @@ -5448,3 +5449,82 @@ describe("FlatESLint", () => { }); }); + +describe("shouldUseFlatConfig", () => { + + /** + * Check that `shouldUseFlatConfig` returns `false` when passing `false` or + * the given argument when passing `true`. + * @param {boolean} expectedValueWhenPassingTrue The value that we expect + * `shouldUseFlatConfig` should return when we pass `true` to it. + * @returns {void} + * @private + */ + function testShouldUseFlatConfig(expectedValueWhenPassingTrue) { + describe("when passing `false`", () => { + it("is `false`", async () => { + assert.strictEqual(await shouldUseFlatConfig(false), false); + }); + }); + + describe("when passing `true`", () => { + it(`is \`${expectedValueWhenPassingTrue}\``, async () => { + assert.strictEqual(await shouldUseFlatConfig(true), expectedValueWhenPassingTrue); + }); + }); + } + + 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); + }); + + 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); + }); + + describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is unset", () => { + describe("when there is a flat config file present", () => { + const originalDir = process.cwd(); + + beforeEach(() => { + process.chdir(__dirname); + }); + + afterEach(() => { + process.chdir(originalDir); + }); + + testShouldUseFlatConfig(true); + }); + + describe("when there is no flat config file present", () => { + const originalDir = process.cwd(); + + beforeEach(() => { + process.chdir(os.tmpdir()); + }); + + afterEach(() => { + process.chdir(originalDir); + }); + + testShouldUseFlatConfig(false); + }); + }); +}); diff --git a/tests/lib/unsupported-api.js b/tests/lib/unsupported-api.js index 53b466adf06d..3a65ba230f80 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); });