From f076e54ecdb0fae70d9b43ad6888606097beef97 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Fri, 5 May 2023 12:57:33 -0700 Subject: [PATCH] fix: Ensure FlatESLint#findConfigFile() doesn't throw. (#17151) If findConfigFile() can't find eslint.config.js, it now returns undefined instead of throwing an error. Fixes #17150 --- lib/eslint/flat-eslint.js | 21 +++++++++++++++------ tests/lib/eslint/flat-eslint.js | 10 +++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/eslint/flat-eslint.js b/lib/eslint/flat-eslint.js index 49228f16e05..faf055b265e 100644 --- a/lib/eslint/flat-eslint.js +++ b/lib/eslint/flat-eslint.js @@ -326,7 +326,7 @@ 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 }) { @@ -334,6 +334,7 @@ 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}`); @@ -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 }; } @@ -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 diff --git a/tests/lib/eslint/flat-eslint.js b/tests/lib/eslint/flat-eslint.js index 0eea528a12c..ce79693af1b 100644 --- a/tests/lib/eslint/flat-eslint.js +++ b/tests/lib/eslint/flat-eslint.js @@ -3956,7 +3956,7 @@ 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 }); @@ -3964,6 +3964,14 @@ describe("FlatESLint", () => { 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"