Skip to content

Commit

Permalink
fix: Ensure FlatESLint#findConfigFile() doesn't throw. (#17151)
Browse files Browse the repository at this point in the history
If findConfigFile() can't find eslint.config.js, it now returns
undefined instead of throwing an error.

Fixes #17150
  • Loading branch information
nzakas committed May 5, 2023
1 parent 4c7a170 commit f076e54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
21 changes: 15 additions & 6 deletions lib/eslint/flat-eslint.js
Expand Up @@ -326,14 +326,15 @@ async function loadFlatConfigFile(filePath) {
* as override config file is not explicitly set to `false`, it will search
* upwards from the cwd for a file named `eslint.config.js`.
* @param {import("./eslint").ESLintOptions} options The ESLint instance options.
* @returns {{configFilePath:string,basePath:string}} Location information for
* @returns {{configFilePath:string,basePath:string,error:boolean}} Location information for
* the config file.
*/
async function locateConfigFileToUse({ configFile, cwd }) {

// determine where to load config file from
let configFilePath;
let basePath = cwd;
let error = false;

if (typeof configFile === "string") {
debug(`Override config file path is ${configFile}`);
Expand All @@ -342,16 +343,18 @@ async function locateConfigFileToUse({ configFile, cwd }) {
debug("Searching for eslint.config.js");
configFilePath = await findFlatConfigFile(cwd);

if (!configFilePath) {
throw new Error("Could not find config file.");
if (configFilePath) {
basePath = path.resolve(path.dirname(configFilePath));
} else {
error = true;
}

basePath = path.resolve(path.dirname(configFilePath));
}

return {
configFilePath,
basePath
basePath,
error
};

}
Expand All @@ -378,7 +381,13 @@ async function calculateConfigArray(eslint, {
return slots.configs;
}

const { configFilePath, basePath } = await locateConfigFileToUse({ configFile, cwd });
const { configFilePath, basePath, error } = await locateConfigFileToUse({ configFile, cwd });

// config file is required to calculate config
if (error) {
throw new Error("Could not find config file.");
}

const configs = new FlatConfigArray(baseConfig || [], { basePath, shouldIgnore });

// load config file
Expand Down
10 changes: 9 additions & 1 deletion tests/lib/eslint/flat-eslint.js
Expand Up @@ -3956,14 +3956,22 @@ describe("FlatESLint", () => {

describe("findConfigFile()", () => {

it("should return null when overrideConfigFile is true", async () => {
it("should return undefined when overrideConfigFile is true", async () => {
const engine = new FlatESLint({
overrideConfigFile: true
});

assert.strictEqual(await engine.findConfigFile(), void 0);
});

it("should return undefined when a config file isn't found", async () => {
const engine = new FlatESLint({
cwd: path.resolve(__dirname, "../../../../")
});

assert.strictEqual(await engine.findConfigFile(), void 0);
});

it("should return custom config file path when overrideConfigFile is a nonempty string", async () => {
const engine = new FlatESLint({
overrideConfigFile: "my-config.js"
Expand Down

0 comments on commit f076e54

Please sign in to comment.