From 2ec47a26e877990175371fc903894d7c4d635aa1 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 7 Jul 2020 17:07:14 -0400 Subject: [PATCH] Added more specific notice for invalid (:) plugin names --- lib/cli-engine/config-array-factory.js | 4 ++-- lib/shared/naming.js | 2 +- messages/plugin-invalid.txt | 9 +++++++++ tests/lib/cli-engine/config-array-factory.js | 18 ++++++++++++++++++ tests/lib/shared/naming.js | 3 ++- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 messages/plugin-invalid.txt diff --git a/lib/cli-engine/config-array-factory.js b/lib/cli-engine/config-array-factory.js index 7c0fba65c67d..06357d0197de 100644 --- a/lib/cli-engine/config-array-factory.js +++ b/lib/cli-engine/config-array-factory.js @@ -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)) { @@ -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, diff --git a/lib/shared/naming.js b/lib/shared/naming.js index 32cff94538af..e959a18c035a 100644 --- a/lib/shared/naming.js +++ b/lib/shared/naming.js @@ -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}`; } diff --git a/messages/plugin-invalid.txt b/messages/plugin-invalid.txt new file mode 100644 index 000000000000..6d6e39d80b0e --- /dev/null +++ b/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. diff --git a/tests/lib/cli-engine/config-array-factory.js b/tests/lib/cli-engine/config-array-factory.js index 3b979c1d38f7..90c80e3672f2 100644 --- a/tests/lib/cli-engine/config-array-factory.js +++ b/tests/lib/cli-engine/config-array-factory.js @@ -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({ diff --git a/tests/lib/shared/naming.js b/tests/lib/shared/naming.js index 0a868a872ed8..8e6fdb99d859 100644 --- a/tests/lib/shared/naming.js +++ b/tests/lib/shared/naming.js @@ -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");