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: show an explicit message when undefined is included as a config #18276

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions lib/eslint/eslint.js
Expand Up @@ -395,6 +395,10 @@ async function calculateConfigArray(eslint, {
const fileConfig = await loadFlatConfigFile(configFilePath);

if (Array.isArray(fileConfig)) {
if (fileConfig.includes(void 0)) {
throw new Error("You have included `undefined` in your array of configs - this is commonly the result of trying to use a config from a plugin that does not exist; make sure you have not typo'd the name!");
}
Comment on lines +398 to +400
Copy link
Member

Choose a reason for hiding this comment

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

This function only loads configs for the ESLint class, not for Linter or RuleTester.

Maybe it would be better to add this check in @humanwhocodes/config-array in normalize and normalizeSync, where it will run for all usages, and to make the error message more generic. What do you think @nzakas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't mind that, but I figured the error message might be ESLint specific meaning it shouldn't belong in config-array; I don't think it's a big deal for RuleTester not to do this (since that is usually used for unit type tests and can easily be verified with tools like TypeScript) but I could see it being more useful for Linter

Copy link
Member

Choose a reason for hiding this comment

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

So I think what we want to do here is throw an error from config-array during normalize and normalizeSync that includes the index of the offending item (calling rethrowConfigError(). Then in ESLint we can catch that error and display an ESLint-specific message.

Copy link
Member

Choose a reason for hiding this comment

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

@G-Rath would you like to submit a PR to https://github.com/humanwhocodes/config-array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup this is still on my radar, just been a bit busy - I hope to put something up in the next week or so, but feel free to make the pr if you want it sooner

Copy link
Member

Choose a reason for hiding this comment

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

I'll take this.


configs.push(...fileConfig);
} else {
configs.push(fileConfig);
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/undefined-config/a.js
@@ -0,0 +1 @@
var foo = "bar";
14 changes: 14 additions & 0 deletions tests/fixtures/undefined-config/eslint.config.js
@@ -0,0 +1,14 @@
// as in '= require("eslint-plugin-mine")'
const myPlugin = {
configs: { recommended: {} }
}

module.exports = [
{
rules: {
quotes: ["error", "single"]
}
},
// 'whoops, this should really be "recommended"'
myPlugin.configs.eslint_recommended
]
10 changes: 10 additions & 0 deletions tests/lib/eslint/eslint.js
Expand Up @@ -1222,6 +1222,16 @@ describe("ESLint", () => {
assert.strictEqual(results[0].messages[0].ruleId, "quotes");
});

it("should error early with config files that include an undefined", async () => {
eslint = new ESLint({
cwd: getFixturePath("undefined-config")
});

await assert.rejects(async () => {
await eslint.lintFiles(["a*.js"]);
}, /You have included `undefined` in your array of configs - this is commonly the result of trying to use a config from a plugin that does not exist; make sure you have not typo'd the name!/u);
});

// https://github.com/eslint/eslint/issues/16265
describe("Dot files in searches", () => {

Expand Down