From e1061c5519ebc7d9ad0457c7437787c23501e5cc Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Sun, 1 Sep 2024 09:11:20 +0200 Subject: [PATCH] Extract packages from rules in eslint plugin --- packages/knip/fixtures/plugins/eslint/.eslintrc.js | 9 +++++++++ packages/knip/src/plugins/eslint/helpers.ts | 9 +++++++-- packages/knip/test/plugins/eslint.test.ts | 4 +++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/knip/fixtures/plugins/eslint/.eslintrc.js b/packages/knip/fixtures/plugins/eslint/.eslintrc.js index dfb48e367..d3aef9977 100644 --- a/packages/knip/fixtures/plugins/eslint/.eslintrc.js +++ b/packages/knip/fixtures/plugins/eslint/.eslintrc.js @@ -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', + }, + }, + ], }; diff --git a/packages/knip/src/plugins/eslint/helpers.ts b/packages/knip/src/plugins/eslint/helpers.ts index d4903a176..06f697668 100644 --- a/packages/knip/src/plugins/eslint/helpers.ts +++ b/packages/knip/src/plugins/eslint/helpers.ts @@ -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 = ( @@ -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') { diff --git a/packages/knip/test/plugins/eslint.test.ts b/packages/knip/test/plugins/eslint.test.ts index 8ccf2cbf9..0345f976b 100644 --- a/packages/knip/test/plugins/eslint.test.ts +++ b/packages/knip/test/plugins/eslint.test.ts @@ -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']); @@ -43,7 +45,7 @@ test('Find dependencies with the ESLint plugin', async () => { assert.deepEqual(counters, { ...baseCounters, - unlisted: 24, + unlisted: 26, processed: 4, total: 4, });