Skip to content

Commit 6dabfb7

Browse files
nicolaichukljharb
andcommitted
fix: check unused rules with overrides
Fixes #317. Co-authored-by: nicolaichuk <[email protected]> Co-authored-by: Jordan Harband <[email protected]>
1 parent bc5a855 commit 6dabfb7

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/lib/rule-finder.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ async function _getConfigs(overrideConfigFile, files) {
3333

3434
async function _getConfig(configFile, files) {
3535
return Array.from(await _getConfigs(configFile, files)).reduce((prev, item) => {
36-
return Object.assign(prev, item, {rules: Object.assign({}, prev.rules, item.rules)});
36+
return Object.assign(prev, item, {
37+
rules: Object.assign({}, prev.rules, item.rules),
38+
plugins: [...new Set([].concat(prev.plugins || [], item.plugins || []))]
39+
});
3740
}, {});
3841
}
3942

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"plugins": [
3+
"plugin"
4+
],
5+
"rules": {
6+
"foo-rule": [2],
7+
"old-rule": [2],
8+
9+
"plugin/old-plugin-rule": [2]
10+
},
11+
12+
"overrides": [
13+
{
14+
"files": ["**/*.json"],
15+
16+
"plugins": [
17+
"@scope/scoped-plugin",
18+
"@scope"
19+
],
20+
"rules": {
21+
"@scope/scoped-plugin/foo-rule": [2],
22+
"@scope/foo-rule": [2]
23+
}
24+
},
25+
{
26+
"files": ["**/*.txt"],
27+
28+
"plugins": [
29+
"@scope-with-dash/scoped-with-dash-plugin",
30+
"@scope-with-dash/eslint-plugin"
31+
],
32+
"rules": {
33+
"@scope-with-dash/scoped-with-dash-plugin/old-plugin-rule": [2]
34+
}
35+
}
36+
]
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
some files

test/lib/rule-finder.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ const specifiedFileAbsolute = path.join(process.cwd(), specifiedFileRelative);
147147
const noRulesFile = path.join(process.cwd(), `./test/fixtures/${eslintVersion}/eslint-with-plugin-with-no-rules.json`);
148148
const noDuplicateRulesFiles = `./test/fixtures/${eslintVersion}/eslint-dedupe-plugin-rules.json`;
149149
const usingDeprecatedRulesFile = path.join(process.cwd(), `./test/fixtures/${eslintVersion}/eslint-with-deprecated-rules.json`);
150+
const usingWithOverridesFile = path.join(process.cwd(), `./test/fixtures/${eslintVersion}/eslint-with-overrides.json`);
150151

151152
describe('rule-finder', function() {
152153
// increase timeout because proxyquire adds a significant delay
@@ -610,4 +611,22 @@ describe('rule-finder', function() {
610611
'plugin/old-plugin-rule'
611612
]);
612613
});
614+
615+
it('check overrides - unused rules', async () => {
616+
const ruleFinder = await getRuleFinder(usingWithOverridesFile, {'ext': ['.txt', '.json']});
617+
assert.deepEqual(ruleFinder.getUnusedRules(), [
618+
"@scope-with-dash/bar-rule",
619+
"@scope-with-dash/foo-rule",
620+
"@scope-with-dash/scoped-with-dash-plugin/bar-rule",
621+
"@scope-with-dash/scoped-with-dash-plugin/foo-rule",
622+
"@scope/bar-rule",
623+
"@scope/scoped-plugin/bar-rule",
624+
"bar-rule",
625+
"baz-rule",
626+
"plugin/bar-rule",
627+
"plugin/baz-rule",
628+
"plugin/foo-rule",
629+
]);
630+
});
631+
613632
});

0 commit comments

Comments
 (0)