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: Use ESLINT_USE_FLAT_CONFIG environment variable for flat config #16356

Merged
merged 7 commits into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 26 additions & 1 deletion lib/cli.js
Expand Up @@ -278,6 +278,31 @@ async function printResults(engine, results, format, outputFile) {
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 @@ -307,7 +332,7 @@ const cli = {
* switch to flat config we can remove this logic.
*/

const usingFlatConfig = allowFlatConfig && !!(await findFlatConfigFile(process.cwd()));
const usingFlatConfig = await shouldUseFlatConfig(allowFlatConfig);

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

Expand Down
37 changes: 37 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -137,6 +137,43 @@ describe("cli", () => {

});

describe("flat config", () => {
const originalCwd = process.cwd;
const originalEnv = { ...process.env };

afterEach(() => {
process.cwd = originalCwd;
process.env = { ...originalEnv };
});
TomerAberbach marked this conversation as resolved.
Show resolved Hide resolved

it(`should use it when an eslint.config.js is present and useFlatConfig is true:${configType}`, async () => {
process.cwd = getFixturePath;

const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig);

// When flat config is used, we get an exit code of 2 because the --ext option is unrecognized.
assert.strictEqual(exitCode, useFlatConfig ? 2 : 0);
});

it(`should not use it when ESLINT_USE_FLAT_CONFIG=false even if an eslint.config.js is present:${configType}`, async () => {
process.cwd = getFixturePath;
process.env.ESLINT_USE_FLAT_CONFIG = "false";

const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig);

assert.strictEqual(exitCode, 0);
});

it(`should use it when ESLINT_USE_FLAT_CONFIG=true and useFlatConfig is true even if an eslint.config.js is not present:${configType}`, async () => {
process.env.ESLINT_USE_FLAT_CONFIG = "true";

const exitCode = await cli.execute(`--no-ignore --ext .js ${getFixturePath("files")}`, null, useFlatConfig);

// When flat config is used, we get an exit code of 2 because the --ext option is unrecognized.
assert.strictEqual(exitCode, useFlatConfig ? 2 : 0);
});
});
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved

describe("when given a config with rules with options and severity level set to error", () => {

const originalCwd = process.cwd;
Expand Down