-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,6 +212,22 @@ function assertIsObject(value) { | |
} | ||
} | ||
|
||
/** | ||
* The error type when there's an eslintrc-style options in a flat config. | ||
*/ | ||
class IncompatibleKeyError extends Error { | ||
|
||
/** | ||
* @param {string} key The invalid key. | ||
*/ | ||
constructor(key) { | ||
super("This appears to be in eslintrc format rather than flat config format."); | ||
this.messageTemplate = "eslintrc-incompat"; | ||
this.messageData = { key }; | ||
} | ||
} | ||
|
||
|
||
//----------------------------------------------------------------------------- | ||
// Low-Level Schemas | ||
//----------------------------------------------------------------------------- | ||
|
@@ -438,11 +454,40 @@ const sourceTypeSchema = { | |
} | ||
}; | ||
|
||
/** | ||
* Creates a schema that always throws an error. Useful for warning | ||
* about eslintrc-style keys. | ||
* @param {string} key The eslintrc key to create a schema for. | ||
* @returns {ObjectPropertySchema} The schema. | ||
*/ | ||
function createEslintrcSchema(key) { | ||
return { | ||
merge: "replace", | ||
validate() { | ||
throw new IncompatibleKeyError(key); | ||
} | ||
}; | ||
} | ||
|
||
//----------------------------------------------------------------------------- | ||
// 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Another nice update, thanks. |
||
extends: createEslintrcSchema("extends"), | ||
globals: createEslintrcSchema("globals"), | ||
ignorePatterns: createEslintrcSchema("ignorePatterns"), | ||
noInlineConfig: createEslintrcSchema("noInlineConfig"), | ||
overrides: createEslintrcSchema("overrides"), | ||
parser: createEslintrcSchema("parser"), | ||
parserOptions: createEslintrcSchema("parserOptions"), | ||
reportUnusedDisableDirectives: createEslintrcSchema("reportUnusedDisableDirectives"), | ||
root: createEslintrcSchema("root"), | ||
|
||
// flat config keys | ||
settings: deepObjectAssignSchema, | ||
linterOptions: { | ||
schema: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"use strict"; | ||
|
||
/* eslint consistent-return: 0 -- no default case */ | ||
|
||
module.exports = function({ key }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooh nice, I like it. |
||
|
||
switch (key) { | ||
case "env": | ||
return ` | ||
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 | ||
`.trim(); | ||
|
||
case "extends": | ||
return ` | ||
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 | ||
nzakas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`.trim(); | ||
|
||
case "globals": | ||
|
||
return ` | ||
A config object is using the "globals" 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 | ||
`.trim(); | ||
|
||
case "ignorePatterns": | ||
|
||
return ` | ||
A config object is using the "ignorePatterns" key, which is not supported in flat config system. | ||
|
||
Flat config uses "ignores" to specify files to ignore. | ||
|
||
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#ignoring-files | ||
`.trim(); | ||
|
||
case "noInlineConfig": | ||
|
||
return ` | ||
A config object is using the "noInlineConfig" key, which is not supported in flat config system. | ||
|
||
Flat config uses "linterOptions.noInlineConfig" to specify files to ignore. | ||
|
||
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#linter-options | ||
`.trim(); | ||
|
||
case "overrides": | ||
|
||
return ` | ||
A config object is using the "overrides" key, which is not supported in flat config system. | ||
|
||
Flat config is an array that acts like the eslintrc "overrides" array. | ||
|
||
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#glob-based-configs | ||
`.trim(); | ||
|
||
case "parser": | ||
|
||
return ` | ||
A config object is using the "parser" key, which is not supported in flat config system. | ||
|
||
Flat config uses "languageOptions.parser" to override the default parser. | ||
|
||
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 | ||
nzakas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`.trim(); | ||
|
||
case "parserOptions": | ||
|
||
return ` | ||
A config object is using the "parserOptions" key, which is not supported in flat config system. | ||
|
||
Flat config uses "languageOptions.parserOptions" to specify parser options. | ||
|
||
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 | ||
`.trim(); | ||
|
||
case "reportUnusedDisableDirectives": | ||
|
||
return ` | ||
A config object is using the "reportUnusedDisableDirectives" key, which is not supported in flat config system. | ||
|
||
Flat config uses "linterOptions.reportUnusedDisableDirectives" to specify files to ignore. | ||
|
||
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#linter-options | ||
`.trim(); | ||
|
||
case "root": | ||
|
||
return ` | ||
A config object is using the "root" key, which is not supported in flat config system. | ||
|
||
Flat configs always act as if they are the root config file, so this key can be safely removed. | ||
`.trim(); | ||
|
||
// no default | ||
} | ||
|
||
}; |
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? PerhapsrejectEslintrcKeySchema
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.