Skip to content

Commit 8bf5505

Browse files
authored
feat: expose shouldUseFlatConfig (#17169)
1 parent 7f183e0 commit 8bf5505

6 files changed

Lines changed: 117 additions & 43 deletions

File tree

lib/cli.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ const fs = require("fs"),
1919
path = require("path"),
2020
{ promisify } = require("util"),
2121
{ ESLint } = require("./eslint"),
22-
{ FlatESLint } = require("./eslint/flat-eslint"),
22+
{ FlatESLint, shouldUseFlatConfig } = require("./eslint/flat-eslint"),
2323
createCLIOptions = require("./options"),
2424
log = require("./shared/logging"),
2525
RuntimeInfo = require("./shared/runtime-info");
2626
const { Legacy: { naming } } = require("@eslint/eslintrc");
27-
const { findFlatConfigFile } = require("./eslint/flat-eslint");
2827
const { ModuleImporter } = require("@humanwhocodes/module-importer");
2928

3029
const debug = require("debug")("eslint:cli");
@@ -275,31 +274,6 @@ async function printResults(engine, results, format, outputFile, resultsMeta) {
275274
return true;
276275
}
277276

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

332-
const usingFlatConfig = await shouldUseFlatConfig(allowFlatConfig);
306+
const usingFlatConfig = allowFlatConfig && await shouldUseFlatConfig();
333307

334308
debug("Using flat config?", usingFlatConfig);
335309

lib/eslint/flat-eslint.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,31 @@ class FlatESLint {
12061206
}
12071207
}
12081208

1209+
/**
1210+
* Returns whether flat config should be used.
1211+
* @returns {Promise<boolean>} Whether flat config should be used.
1212+
*/
1213+
async function shouldUseFlatConfig() {
1214+
switch (process.env.ESLINT_USE_FLAT_CONFIG) {
1215+
case "true":
1216+
return true;
1217+
case "false":
1218+
return false;
1219+
default:
1220+
1221+
/*
1222+
* If neither explicitly enabled nor disabled, then use the presence
1223+
* of a flat config file to determine enablement.
1224+
*/
1225+
return !!(await findFlatConfigFile(process.cwd()));
1226+
}
1227+
}
1228+
12091229
//------------------------------------------------------------------------------
12101230
// Public Interface
12111231
//------------------------------------------------------------------------------
12121232

12131233
module.exports = {
12141234
FlatESLint,
1215-
findFlatConfigFile
1235+
shouldUseFlatConfig
12161236
};

lib/unsupported-api.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//-----------------------------------------------------------------------------
1313

1414
const { FileEnumerator } = require("./cli-engine/file-enumerator");
15-
const { FlatESLint } = require("./eslint/flat-eslint");
15+
const { FlatESLint, shouldUseFlatConfig } = require("./eslint/flat-eslint");
1616
const FlatRuleTester = require("./rule-tester/flat-rule-tester");
1717

1818
//-----------------------------------------------------------------------------
@@ -22,6 +22,7 @@ const FlatRuleTester = require("./rule-tester/flat-rule-tester");
2222
module.exports = {
2323
builtinRules: require("./rules"),
2424
FlatESLint,
25+
shouldUseFlatConfig,
2526
FlatRuleTester,
2627
FileEnumerator
2728
};

tests/lib/cli.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("cli", () => {
6363

6464
const localCLI = proxyquire("../../lib/cli", {
6565
"./eslint": { ESLint: fakeESLint },
66-
"./flat-eslint": { FlatESLint: fakeESLint, findFlatConfigFile: () => null },
66+
"./flat-eslint": { FlatESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
6767
"./shared/logging": log
6868
});
6969

@@ -940,7 +940,7 @@ describe("cli", () => {
940940

941941
localCLI = proxyquire("../../lib/cli", {
942942
"./eslint": { ESLint: fakeESLint },
943-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
943+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
944944
"./shared/logging": log
945945
});
946946

@@ -959,7 +959,7 @@ describe("cli", () => {
959959

960960
localCLI = proxyquire("../../lib/cli", {
961961
"./eslint": { ESLint: fakeESLint },
962-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
962+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
963963
"./shared/logging": log
964964
});
965965

@@ -990,7 +990,7 @@ describe("cli", () => {
990990

991991
localCLI = proxyquire("../../lib/cli", {
992992
"./eslint": { ESLint: fakeESLint },
993-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
993+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
994994
"./shared/logging": log
995995
});
996996

@@ -1026,7 +1026,7 @@ describe("cli", () => {
10261026

10271027
localCLI = proxyquire("../../lib/cli", {
10281028
"./eslint": { ESLint: fakeESLint },
1029-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1029+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
10301030

10311031
"./shared/logging": log
10321032
});
@@ -1063,7 +1063,7 @@ describe("cli", () => {
10631063

10641064
localCLI = proxyquire("../../lib/cli", {
10651065
"./eslint": { ESLint: fakeESLint },
1066-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1066+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
10671067

10681068
"./shared/logging": log
10691069
});
@@ -1081,7 +1081,7 @@ describe("cli", () => {
10811081

10821082
localCLI = proxyquire("../../lib/cli", {
10831083
"./eslint": { ESLint: fakeESLint },
1084-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1084+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
10851085

10861086
"./shared/logging": log
10871087
});
@@ -1112,7 +1112,7 @@ describe("cli", () => {
11121112

11131113
localCLI = proxyquire("../../lib/cli", {
11141114
"./eslint": { ESLint: fakeESLint },
1115-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1115+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
11161116

11171117
"./shared/logging": log
11181118
});
@@ -1140,7 +1140,7 @@ describe("cli", () => {
11401140

11411141
localCLI = proxyquire("../../lib/cli", {
11421142
"./eslint": { ESLint: fakeESLint },
1143-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1143+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
11441144

11451145
"./shared/logging": log
11461146
});
@@ -1175,7 +1175,7 @@ describe("cli", () => {
11751175

11761176
localCLI = proxyquire("../../lib/cli", {
11771177
"./eslint": { ESLint: fakeESLint },
1178-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1178+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
11791179

11801180
"./shared/logging": log
11811181
});
@@ -1212,7 +1212,7 @@ describe("cli", () => {
12121212

12131213
localCLI = proxyquire("../../lib/cli", {
12141214
"./eslint": { ESLint: fakeESLint },
1215-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1215+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
12161216

12171217
"./shared/logging": log
12181218
});
@@ -1248,7 +1248,7 @@ describe("cli", () => {
12481248

12491249
localCLI = proxyquire("../../lib/cli", {
12501250
"./eslint": { ESLint: fakeESLint },
1251-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1251+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
12521252

12531253
"./shared/logging": log
12541254
});
@@ -1265,7 +1265,7 @@ describe("cli", () => {
12651265

12661266
localCLI = proxyquire("../../lib/cli", {
12671267
"./eslint": { ESLint: fakeESLint },
1268-
"./eslint/flat-eslint": { ESLint: fakeESLint, findFlatConfigFile: () => null },
1268+
"./eslint/flat-eslint": { ESLint: fakeESLint, shouldUseFlatConfig: () => Promise.resolve(false) },
12691269

12701270
"./shared/logging": log
12711271
});

tests/lib/eslint/flat-eslint.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
2323
const shell = require("shelljs");
2424
const hash = require("../../../lib/cli-engine/hash");
2525
const { unIndent, createCustomTeardown } = require("../../_utils");
26+
const { shouldUseFlatConfig } = require("../../../lib/eslint/flat-eslint");
2627
const coreRules = require("../../../lib/rules");
2728

2829
//------------------------------------------------------------------------------
@@ -5478,3 +5479,77 @@ describe("FlatESLint", () => {
54785479
});
54795480

54805481
});
5482+
5483+
describe("shouldUseFlatConfig", () => {
5484+
5485+
/**
5486+
* Check that `shouldUseFlatConfig` returns the expected value from a CWD
5487+
* with a flat config and one without a flat config.
5488+
* @param {boolean} expectedValueWithConfig the expected return value of
5489+
* `shouldUseFlatConfig` when in a directory with a flat config present
5490+
* @param {boolean} expectedValueWithoutConfig the expected return value of
5491+
* `shouldUseFlatConfig` when in a directory without any flat config present
5492+
* @returns {void}
5493+
*/
5494+
function testShouldUseFlatConfig(expectedValueWithConfig, expectedValueWithoutConfig) {
5495+
describe("when there is a flat config file present", () => {
5496+
const originalDir = process.cwd();
5497+
5498+
beforeEach(() => {
5499+
process.chdir(__dirname);
5500+
});
5501+
5502+
afterEach(() => {
5503+
process.chdir(originalDir);
5504+
});
5505+
5506+
it(`is \`${expectedValueWithConfig}\``, async () => {
5507+
assert.strictEqual(await shouldUseFlatConfig(), expectedValueWithConfig);
5508+
});
5509+
});
5510+
5511+
describe("when there is no flat config file present", () => {
5512+
const originalDir = process.cwd();
5513+
5514+
beforeEach(() => {
5515+
process.chdir(os.tmpdir());
5516+
});
5517+
5518+
afterEach(() => {
5519+
process.chdir(originalDir);
5520+
});
5521+
5522+
it(`is \`${expectedValueWithoutConfig}\``, async () => {
5523+
assert.strictEqual(await shouldUseFlatConfig(), expectedValueWithoutConfig);
5524+
});
5525+
});
5526+
}
5527+
5528+
describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is `'true'`", () => {
5529+
beforeEach(() => {
5530+
process.env.ESLINT_USE_FLAT_CONFIG = true;
5531+
});
5532+
5533+
afterEach(() => {
5534+
delete process.env.ESLINT_USE_FLAT_CONFIG;
5535+
});
5536+
5537+
testShouldUseFlatConfig(true, true);
5538+
});
5539+
5540+
describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is `'false'`", () => {
5541+
beforeEach(() => {
5542+
process.env.ESLINT_USE_FLAT_CONFIG = false;
5543+
});
5544+
5545+
afterEach(() => {
5546+
delete process.env.ESLINT_USE_FLAT_CONFIG;
5547+
});
5548+
5549+
testShouldUseFlatConfig(false, false);
5550+
});
5551+
5552+
describe("when the env variable `ESLINT_USE_FLAT_CONFIG` is unset", () => {
5553+
testShouldUseFlatConfig(true, false);
5554+
});
5555+
});

tests/lib/unsupported-api.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ describe("unsupported-api", () => {
2727
assert.isFunction(api.FlatESLint);
2828
});
2929

30+
it("should have shouldUseFlatConfig exposed", () => {
31+
assert.isFunction(api.shouldUseFlatConfig);
32+
});
33+
3034
it("should have FlatRuleTester exposed", () => {
3135
assert.isFunction(api.FlatRuleTester);
3236
});

0 commit comments

Comments
 (0)