Skip to content

Commit

Permalink
Update: Implement missing functionality from ESLint port (fixes #12) (#…
Browse files Browse the repository at this point in the history
…23)

* Update: Implement eslint/eslint#13473 (fixes #12)

* Fix lint errors
  • Loading branch information
nzakas committed Jan 15, 2021
1 parent b86d0c2 commit f1179c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/config-array-factory.js
Expand Up @@ -281,14 +281,15 @@ function loadESLintIgnoreFile(filePath) {
* Creates an error to notify about a missing config to extend from.
* @param {string} configName The name of the missing config.
* @param {string} importerName The name of the config that imported the missing config
* @param {string} messageTemplate The text template to source error strings from.
* @returns {Error} The error object to throw
* @private
*/
function configMissingError(configName, importerName) {
function configInvalidError(configName, importerName, messageTemplate) {
return Object.assign(
new Error(`Failed to load config "${configName}" to extend from.`),
{
messageTemplate: "extend-config-missing",
messageTemplate,
messageData: { configName, importerName }
}
);
Expand Down Expand Up @@ -809,7 +810,7 @@ class ConfigArrayFactory {
});
}

throw configMissingError(extendName, ctx.name);
throw configInvalidError(extendName, ctx.name, "extend-config-missing");
}

/**
Expand All @@ -821,6 +822,11 @@ class ConfigArrayFactory {
*/
_loadExtendedPluginConfig(extendName, ctx) {
const slashIndex = extendName.lastIndexOf("/");

if (slashIndex === -1) {
throw configInvalidError(extendName, ctx.filePath, "plugin-invalid");
}

const pluginName = extendName.slice("plugin:".length, slashIndex);
const configName = extendName.slice(slashIndex + 1);

Expand All @@ -841,7 +847,7 @@ class ConfigArrayFactory {
});
}

throw plugin.error || configMissingError(extendName, ctx.filePath);
throw plugin.error || configInvalidError(extendName, ctx.filePath, "extend-config-missing");
}

/**
Expand Down Expand Up @@ -874,7 +880,7 @@ class ConfigArrayFactory {
} catch (error) {
/* istanbul ignore else */
if (error && error.code === "MODULE_NOT_FOUND") {
throw configMissingError(extendName, ctx.filePath);
throw configInvalidError(extendName, ctx.filePath, "extend-config-missing");
}
throw error;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/config-array-factory.js
Expand Up @@ -1568,6 +1568,23 @@ 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 config specifier is missing config name", () => {
try {
applyExtends({
extends: "plugin:some-plugin",
rules: { eqeqeq: 2 }
});
} catch (err) {
assert.strictEqual(err.messageTemplate, "plugin-invalid");
assert.deepStrictEqual(err.messageData, {
configName: "plugin:some-plugin",
importerName: path.join(getPath(), "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

0 comments on commit f1179c5

Please sign in to comment.