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: Improve config error messages #17385

Merged
merged 4 commits into from Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions lib/config/flat-config-schema.js
Expand Up @@ -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
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -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) {
Copy link
Contributor

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?

Copy link
Member Author

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.

return {
merge: "replace",
validate() {
throw new IncompatibleKeyError(key);
}
};
}

//-----------------------------------------------------------------------------
// Full schema
//-----------------------------------------------------------------------------

exports.flatConfigSchema = {

// eslintrc-style keys that should warn
env: createEslintrcSchema("env"),
Copy link
Contributor

@matwilko matwilko Jul 18, 2023

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: ...
...

Copy link
Member Author

Choose a reason for hiding this comment

The 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: {
Expand Down
116 changes: 116 additions & 0 deletions messages/eslintrc-incompat.js
@@ -0,0 +1,116 @@
"use strict";

/* eslint consistent-return: 0 -- no default case */

module.exports = function({ key }) {
Copy link
Contributor

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();

Copy link
Member Author

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.


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
}

};
28 changes: 28 additions & 0 deletions tests/lib/config/flat-config-array.js
Expand Up @@ -1938,5 +1938,33 @@ describe("FlatConfigArray", () => {

});

describe("Invalid Keys", () => {

[
"env",
"extends",
"globals",
"ignorePatterns",
"noInlineConfig",
"overrides",
"parser",
"parserOptions",
"reportUnusedDisableDirectives",
"root"
].forEach(key => {

it(`should error when a ${key} key is found`, async () => {
await assertInvalidConfig([
{
[key]: "foo"
}
], `Key "${key}": This appears to be in eslintrc format rather than flat config format.`);

});
});


});

});
});