Skip to content

Commit

Permalink
feat: Expose shouldUseFlatConfig (#17129)
Browse files Browse the repository at this point in the history
  • Loading branch information
cprussin committed May 8, 2023
1 parent 9682d66 commit c7f2be2
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 42 deletions.
28 changes: 1 addition & 27 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
27 changes: 26 additions & 1 deletion lib/eslint/flat-eslint.js
Expand Up @@ -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<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
//------------------------------------------------------------------------------

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
80 changes: 80 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,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);
});
});
});
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

0 comments on commit c7f2be2

Please sign in to comment.