Skip to content

Commit

Permalink
Extract packages from rules in eslint plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 1, 2024
1 parent bb86a05 commit e1061c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/knip/fixtures/plugins/eslint/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ module.exports = {
plugins: [['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }]],
},
},
overrides: [
{
extends: ['plugin:@org/name/typescript'],
rules: {
'@other-org/no-unused-expressions': 'error',
'@other-org/no-unused-vars': 'error',
},
},
],
};
9 changes: 7 additions & 2 deletions packages/knip/src/plugins/eslint/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const getDependencies = (config: ESLintConfig | OverrideConfig) => {
? getDependenciesFromConfig(config.parserOptions.babelOptions)
: [];
const settings = config.settings ? getDependenciesFromSettings(config.settings) : [];
const rules = getDependenciesFromRules(config.rules);
const overrides: string[] = config.overrides ? [config.overrides].flat().flatMap(getDependencies) : [];

return compact([...extendsSpecifiers, ...plugins, parser, ...babelDependencies, ...settings, ...overrides]);
return compact([...extendsSpecifiers, ...plugins, parser, ...babelDependencies, ...settings, ...rules, ...overrides]);
};

type GetDependenciesDeep = (
Expand Down Expand Up @@ -85,7 +86,11 @@ const resolveExtendSpecifier = (specifier: string) => {
return resolveSpecifier(namespace, specifier);
};

// Super custom: find dependencies of specific ESLint plugins through settings
const getDependenciesFromRules = (rules: ESLintConfig['rules'] = {}) =>
Object.keys(rules).flatMap(ruleKey =>
ruleKey.includes('/') ? [resolveSpecifier('eslint-plugin', ruleKey.split('/').slice(0, -1).join('/'))] : []
);

const getDependenciesFromSettings = (settings: ESLintConfig['settings'] = {}) => {
return Object.entries(settings).flatMap(([settingKey, settings]) => {
if (settingKey === 'import/resolver') {
Expand Down
4 changes: 3 additions & 1 deletion packages/knip/test/plugins/eslint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ test('Find dependencies with the ESLint plugin', async () => {
assert(issues.unlisted['.eslintrc.js']['eslint-plugin-eslint-comments']);
assert(issues.unlisted['.eslintrc.js']['eslint-plugin-eslint-plugin']);
assert(issues.unlisted['.eslintrc.js']['eslint-plugin-import']);
assert(issues.unlisted['.eslintrc.js']['@org/eslint-plugin-name/typescript']);
assert(issues.unlisted['.eslintrc.js']['@other-org/eslint-plugin']);

assert(issues.unlisted['.eslintrc.json']['@babel/plugin-syntax-import-assertions']);
assert(issues.unlisted['.eslintrc.json']['eslint-config-airbnb']);
Expand All @@ -43,7 +45,7 @@ test('Find dependencies with the ESLint plugin', async () => {

assert.deepEqual(counters, {
...baseCounters,
unlisted: 24,
unlisted: 26,
processed: 4,
total: 4,
});
Expand Down

0 comments on commit e1061c5

Please sign in to comment.