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: Improve config error messages #17385
Conversation
Includes some keys to catch known eslintrc keys when they appear in flat config. This throws a specific error that the ESLint CLI can then output a more helpful message about. fixes #17370
✅ Deploy Preview for docs-eslint canceled.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handful of thoughts, can't see any top-level keys you missed.
messages/eslintrc-incompat.js
Outdated
|
||
/* eslint consistent-return: 0 -- no default case */ | ||
|
||
module.exports = function({ key }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this look slightly nicer as an object rather than a big switch? A little less syntax noise to my eyes 😄
e.g.
const keyErrors = {
env: `
A config object is using the "env" key, which is not supported in flat config system.
Flat config uses "languageOptions.globals" to define global variables for your files.
Please see the following page for information on how to convert your config object into the correct format:
https://eslint.org/docs/latest/use/configure/migration-guide#configuring-language-options
`,
extends: `
A config object is using the "extends" key, which is not supported in flat config system.
Instead of "extends", you can include config objects that you'd like to extend from directly in the flat config array.
Please see the following page for more information:
https://eslint.org/docs/latest/use/configure/configuration-files-new#using-predefined-configurations
`,
...
};
module.exports = key => keyErrors[key].trim();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh nice, I like it.
lib/config/flat-config-schema.js
Outdated
* @param {string} key The eslintrc key to create a schema for. | ||
* @returns {ObjectPropertySchema} The schema. | ||
*/ | ||
function createEslintrcSchema(key) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might suggest a more explicit name than createEslintrcSchema
- the current name could be confused as adding support for eslintrc config keys at a glance? Perhaps rejectEslintrcKeySchema
or similar to spell it out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point, making it explicit that this is throwing an error makes sense.
lib/config/flat-config-schema.js
Outdated
//----------------------------------------------------------------------------- | ||
// Full schema | ||
//----------------------------------------------------------------------------- | ||
|
||
exports.flatConfigSchema = { | ||
|
||
// eslintrc-style keys that should warn | ||
env: createEslintrcSchema("env"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though we're unlikely to add any further keys, I might suggest a slight refactor here to remove the duplication of the key name, and not take up half the space in this schema with keys that we don't actually care about in the end:
function rejectEslintrcKeySchema(key) {
return {
merge: "replace",
validate() {
throw new IncompatibleKeyError(key);
}
};
}
function rejectEslintrcKeySchemas(...keys) {
return Object.fromEntries(keys.map(key => [key, rejectEslintrcKeySchema(key)]));
}
exports.flatConfigSchema = {
...rejectEslintrcKeySchemas("env", "extends", "globals", "ignorePatterns", "noInlineConfig", "overrides", "parser", "parserOptions", "reportUnusedDisableDirectives", "root"),
settings: ...
linterOptions: ...
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another nice update, thanks.
No, all eslintrc top-level keys that don't exist as flat config top-level keys are included. |
This test should be updated: eslint/tests/lib/rule-tester/flat-rule-tester.js Lines 1331 to 1340 in 42faa17
|
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
* main: (101 commits) docs: Migrating `eslint-env` configuration comments (eslint#17390) Merge pull request from GHSA-qwh7-v8hg-w8rh feat: adds option for allowing empty object patterns as parameter (eslint#17365) fix: FlatESLint#getRulesMetaForResults shouldn't throw on unknown rules (eslint#17393) docs: fix Ignoring Files section in config migration guide (eslint#17392) docs: Update README feat: Improve config error messages (eslint#17385) fix: Update no-loop-func to not overlap with no-undef (eslint#17358) docs: Update README docs: Update README 8.45.0 Build: changelog update for 8.45.0 chore: package.json update for @eslint/js release docs: add playground links to correct and incorrect code blocks (eslint#17306) docs: Expand rule option schema docs (eslint#17198) docs: Config Migration Guide (eslint#17230) docs: Update README fix: Fix suggestion message in `no-useless-escape` (eslint#17339) docs: Update README chore: update eslint-config-eslint exports (eslint#17336) ...
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[x] Add something to the core
[ ] Other, please explain:
What changes did you make? (Give an overview)
Includes some keys to catch known eslintrc keys when they appear in flat config. This throws a specific error that the ESLint CLI can then output a more helpful message about.
refs #17370
Is there anything you'd like reviewers to focus on?
Did I miss any top-level keys?
(Note: we can probably do more to do things like catch the different in
plugins
format, just wanted to get the ball rolling.)