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

Update: Suggest missing rule in flat config (fixes #14027) #15074

Merged
merged 3 commits into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
28 changes: 21 additions & 7 deletions lib/config/rule-validator.js
Expand Up @@ -35,16 +35,30 @@ function findRuleDefinition(ruleId, config) {
pluginName = ruleIdParts.join("/");
}

if (!config.plugins || !config.plugins[pluginName]) {
throw new TypeError(`Key "rules": Key "${ruleId}": Could not find plugin "${pluginName}".`);
nzakas marked this conversation as resolved.
Show resolved Hide resolved
}
if (config.plugins) {

if (!config.plugins[pluginName].rules || !config.plugins[pluginName].rules[ruleName]) {
throw new TypeError(`Key "rules": Key "${ruleId}": Could not find "${ruleName}" in plugin "${pluginName}".`);
}
const plugin = config.plugins[pluginName];

// first check for exact rule match
if (plugin && plugin.rules && plugin.rules[ruleName]) {
return config.plugins[pluginName].rules[ruleName];
}

let errorMessage = `Key "rules": Key "${ruleId}": Could not find "${ruleName}" in plugin "${pluginName}".`;

return config.plugins[pluginName].rules[ruleName];
// otherwise, let's see if we can find the rule name elsewhere
for (const [otherPluginName, otherPlugin] of Object.entries(config.plugins)) {
if (otherPlugin.rules && otherPlugin.rules[ruleName]) {
errorMessage += ` Did you mean "${otherPluginName}/${ruleName}"?`;
nzakas marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}

throw new TypeError(errorMessage);

}

throw new TypeError(`Key "rules": Key "${ruleId}": Could not find plugin "${pluginName}".`);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/config/flat-config-array.js
Expand Up @@ -56,6 +56,16 @@ const baseConfig = {
}
}
}
},
test1: {
rules: {
match: {}
}
},
test2: {
rules: {
nomatch: {}
}
}
}
};
Expand Down Expand Up @@ -1278,6 +1288,28 @@ describe("FlatConfigArray", () => {
], "Key \"rules\": Key \"foo\": Expected severity of \"off\", 0, \"warn\", 1, \"error\", or 2.");
});

it("should error when rule doesn't exist", async () => {

await assertInvalidConfig([
{
rules: {
foox: [1, "bar"]
}
}
], /Key "rules": Key "foox": Could not find "foox" in plugin "@"./u);
});

it("should error and suggest alternative when rule doesn't exist", async () => {

await assertInvalidConfig([
{
rules: {
"test2/match": "error"
}
}
], /Key "rules": Key "test2\/match": Could not find "match" in plugin "test2"\. Did you mean "test1\/match"\?/u);
});

it("should error when rule options don't match schema", async () => {

await assertInvalidConfig([
Expand Down