Skip to content

Commit 67c0074

Browse files
authored
Update: Suggest missing rule in flat config (fixes #14027) (#15074)
* Update: Suggest missing rule in flat config (fixes #14027) * Switch to older syntax * Fix error messages
1 parent cf34e5c commit 67c0074

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

lib/config/rule-validator.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,33 @@ function findRuleDefinition(ruleId, config) {
3535
pluginName = ruleIdParts.join("/");
3636
}
3737

38-
if (!config.plugins || !config.plugins[pluginName]) {
39-
throw new TypeError(`Key "rules": Key "${ruleId}": Could not find plugin "${pluginName}".`);
40-
}
38+
const errorMessageHeader = `Key "rules": Key "${ruleId}"`;
39+
let errorMessage = `${errorMessageHeader}: Could not find plugin "${pluginName}".`;
4140

42-
if (!config.plugins[pluginName].rules || !config.plugins[pluginName].rules[ruleName]) {
43-
throw new TypeError(`Key "rules": Key "${ruleId}": Could not find "${ruleName}" in plugin "${pluginName}".`);
44-
}
41+
// if the plugin exists then we need to check if the rule exists
42+
if (config.plugins && config.plugins[pluginName]) {
43+
44+
const plugin = config.plugins[pluginName];
45+
46+
// first check for exact rule match
47+
if (plugin.rules && plugin.rules[ruleName]) {
48+
return config.plugins[pluginName].rules[ruleName];
49+
}
4550

46-
return config.plugins[pluginName].rules[ruleName];
51+
errorMessage = `${errorMessageHeader}: Could not find "${ruleName}" in plugin "${pluginName}".`;
52+
53+
// otherwise, let's see if we can find the rule name elsewhere
54+
for (const [otherPluginName, otherPlugin] of Object.entries(config.plugins)) {
55+
if (otherPlugin.rules && otherPlugin.rules[ruleName]) {
56+
errorMessage += ` Did you mean "${otherPluginName}/${ruleName}"?`;
57+
break;
58+
}
59+
}
60+
61+
// falls through to throw error
62+
}
4763

64+
throw new TypeError(errorMessage);
4865
}
4966

5067
/**

tests/lib/config/flat-config-array.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ const baseConfig = {
5656
}
5757
}
5858
}
59+
},
60+
test1: {
61+
rules: {
62+
match: {}
63+
}
64+
},
65+
test2: {
66+
rules: {
67+
nomatch: {}
68+
}
5969
}
6070
}
6171
};
@@ -1278,6 +1288,39 @@ describe("FlatConfigArray", () => {
12781288
], "Key \"rules\": Key \"foo\": Expected severity of \"off\", 0, \"warn\", 1, \"error\", or 2.");
12791289
});
12801290

1291+
it("should error when rule doesn't exist", async () => {
1292+
1293+
await assertInvalidConfig([
1294+
{
1295+
rules: {
1296+
foox: [1, "bar"]
1297+
}
1298+
}
1299+
], /Key "rules": Key "foox": Could not find "foox" in plugin "@"./u);
1300+
});
1301+
1302+
it("should error and suggest alternative when rule doesn't exist", async () => {
1303+
1304+
await assertInvalidConfig([
1305+
{
1306+
rules: {
1307+
"test2/match": "error"
1308+
}
1309+
}
1310+
], /Key "rules": Key "test2\/match": Could not find "match" in plugin "test2"\. Did you mean "test1\/match"\?/u);
1311+
});
1312+
1313+
it("should error when plugin for rule doesn't exist", async () => {
1314+
1315+
await assertInvalidConfig([
1316+
{
1317+
rules: {
1318+
"doesnt-exist/match": "error"
1319+
}
1320+
}
1321+
], /Key "rules": Key "doesnt-exist\/match": Could not find plugin "doesnt-exist"\./u);
1322+
});
1323+
12811324
it("should error when rule options don't match schema", async () => {
12821325

12831326
await assertInvalidConfig([

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy