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

fix: Ensure FlatESLint#findConfigFile() doesn't throw. #17151

Merged
merged 1 commit into from May 5, 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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name error sounds generic and I expected it to be an instance of Error. Can we use something like isConfigFileDetected/hasConfigFile? I feel it's better suited for a boolean value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can return an error object error = new Error("Could not find config file.");, which can be thrown directly where it is used?

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

    // config file is required to calculate config
    if (error) {
        throw error;
    }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aladdin-add I like that suggestion. Can you open a PR with that change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, will do!


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