Skip to content

Commit

Permalink
Added more specific notice for invalid (:) plugin names
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Goldberg committed Jul 7, 2020
1 parent 0af1d28 commit 2ec47a2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/cli-engine/config-array-factory.js
Expand Up @@ -802,7 +802,7 @@ class ConfigArrayFactory {
*/
_loadExtendedPluginConfig(extendName, ctx) {
const slashIndex = extendName.lastIndexOf("/");
const pluginName = extendName.slice("plugin:".length, slashIndex);
const pluginName = slashIndex === -1 ? extendName : extendName.slice("plugin:".length, slashIndex);
const configName = extendName.slice(slashIndex + 1);

if (isFilePath(pluginName)) {
Expand Down Expand Up @@ -994,7 +994,7 @@ class ConfigArrayFactory {
error = resolveError;
/* istanbul ignore else */
if (error && error.code === "MODULE_NOT_FOUND") {
error.messageTemplate = "plugin-missing";
error.messageTemplate = request.indexOf(":") === -1 ? "plugin-missing" : "plugin-invalid";
error.messageData = {
pluginName: request,
resolvePluginsRelativeTo: ctx.pluginBasePath,
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/naming.js
Expand Up @@ -43,7 +43,7 @@ function normalizePackageName(name, prefix) {
*/
normalizedName = normalizedName.replace(/^@([^/]+)\/(.*)$/u, `@$1/${prefix}-$2`);
}
} else if (!normalizedName.startsWith(`${prefix}-`)) {
} else if (!normalizedName.startsWith(`${prefix}-`) && normalizedName.indexOf(":") === -1) {
normalizedName = `${prefix}-${normalizedName}`;
}

Expand Down
9 changes: 9 additions & 0 deletions messages/plugin-invalid.txt
@@ -0,0 +1,9 @@
ESLint couldn't find the plugin "<%- pluginName %>".

(The package "<%- pluginName %>" was not found when loaded as a Node module from the directory "<%- resolvePluginsRelativeTo %>".)

It looks like this might not be a valid plugin name. Did you use the name of a preset configuration instead of a plugin?

The plugin "<%- pluginName %>" was referenced from the config file in "<%- importerName %>".

If you still can't figure out the problem, please stop by https://eslint.org/chat to chat with the team.
18 changes: 18 additions & 0 deletions tests/lib/cli-engine/config-array-factory.js
Expand Up @@ -1503,6 +1503,24 @@ describe("ConfigArrayFactory", () => {
}, /Failed to load config "plugin:invalid-config\/bar" to extend from./u);
});

it("should throw an error with a message template when a plugin referenced for a plugin config is invalid", () => {
try {
applyExtends({
extends: "plugin:nonexistent-plugin",
rules: { eqeqeq: 2 }
});
} catch (err) {
assert.strictEqual(err.messageTemplate, "plugin-invalid");
assert.deepStrictEqual(err.messageData, {
pluginName: "plugin:nonexistent-plugin",
resolvePluginsRelativeTo: process.cwd(),
importerName: "whatever"
});
return;
}
assert.fail("Expected to throw an error");
});

it("should throw an error with a message template when a plugin referenced for a plugin config is not found", () => {
try {
applyExtends({
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/shared/naming.js
Expand Up @@ -25,7 +25,8 @@ describe("naming", () => {
["@z\\foo", "@z/eslint-config-foo"],
["@z\\foo\\bar.js", "@z/eslint-config-foo/bar.js"],
["@z/eslint-config", "@z/eslint-config"],
["@z/eslint-config-foo", "@z/eslint-config-foo"]
["@z/eslint-config-foo", "@z/eslint-config-foo"],
["plugin:invalid", "plugin:invalid"]
], (input, expected) => {
it(`should return ${expected} when passed ${input}`, () => {
const result = naming.normalizePackageName(input, "eslint-config");
Expand Down

0 comments on commit 2ec47a2

Please sign in to comment.