Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose shouldUseFlatConfig #17169

Merged
merged 1 commit into from May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 2 additions & 28 deletions lib/cli.js
Expand Up @@ -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");
Expand Down Expand Up @@ -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<boolean>} 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
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -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);

Expand Down
22 changes: 21 additions & 1 deletion lib/eslint/flat-eslint.js
Expand Up @@ -1212,11 +1212,31 @@ class FlatESLint {
}
}

/**
* Returns whether flat config should be used.
* @returns {Promise<boolean>} 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
};
3 changes: 2 additions & 1 deletion lib/unsupported-api.js
Expand Up @@ -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");

//-----------------------------------------------------------------------------
Expand All @@ -22,6 +22,7 @@ const FlatRuleTester = require("./rule-tester/flat-rule-tester");
module.exports = {
builtinRules: require("./rules"),
FlatESLint,
shouldUseFlatConfig,
FlatRuleTester,
FileEnumerator
};
26 changes: 13 additions & 13 deletions tests/lib/cli.js
Expand Up @@ -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
});

Expand Down Expand Up @@ -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
});

Expand All @@ -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
});

Expand Down Expand Up @@ -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
});

Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand All @@ -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
});
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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
});
Expand All @@ -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
});
Expand Down
75 changes: 75 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -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");

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -5448,3 +5449,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);
});
});
4 changes: 4 additions & 0 deletions tests/lib/unsupported-api.js
Expand Up @@ -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);
});
Expand Down