|
| 1 | +// eslint-disable-next-line import/extensions |
| 2 | +import 'core-js/es/set/index.js'; // for Node.js <22.0.0 |
| 3 | + |
| 4 | +import { cpus } from 'node:os'; |
| 5 | +import { basename } from 'node:path'; |
| 6 | + |
| 7 | +import { FlatCompat } from '@eslint/eslintrc'; |
| 8 | +import type { Entry } from '@nodelib/fs.walk'; |
| 9 | +import * as fsWalk from '@nodelib/fs.walk'; |
| 10 | +import { ESLint, type Rule } from 'eslint'; |
| 11 | +import { builtinRules } from 'eslint/use-at-your-own-risk'; |
| 12 | + |
| 13 | +const defaultCwd = process.cwd(); |
| 14 | + |
| 15 | +/** `${ruleId}` (from ESLint core) or `${pluginName}/${ruleName}` (from ESLint plugin) */ |
| 16 | +export type ESLintRuleId = string; |
| 17 | +export interface IESLintRule { |
| 18 | + id: ESLintRuleId; |
| 19 | + meta: Rule.RuleMetaData | undefined; |
| 20 | +} |
| 21 | +export type ESLintRuleMap = Map<ESLintRuleId, IESLintRule>; |
| 22 | + |
| 23 | +export interface IESLintConfigRulesLintResult { |
| 24 | + // other context |
| 25 | + readonly eslint: ESLint; |
| 26 | + readonly compat: FlatCompat; |
| 27 | + |
| 28 | + readonly pluginMap: Map<string, ESLint.Plugin>; |
| 29 | + /** all rules that can be used */ |
| 30 | + readonly ruleMap: ESLintRuleMap; |
| 31 | + |
| 32 | + // calculated result |
| 33 | + /** |
| 34 | + * Rules that specified in the config file |
| 35 | + * |
| 36 | + * Whether the rule is 'off' or 'warn' or 'error' is not considered. |
| 37 | + */ |
| 38 | + readonly usedRuleIds: Set<string>; |
| 39 | + readonly usedPluginSpecifiers: Set<string>; |
| 40 | + |
| 41 | + readonly usedUnknownRuleIds: Set<string>; |
| 42 | + readonly usedKnownRuleIds: Set<string>; |
| 43 | + readonly usedDeprecatedRuleIds: Set<string>; |
| 44 | +} |
| 45 | + |
| 46 | +const collator = new Intl.Collator('en'); |
| 47 | +/** |
| 48 | + * Sort ruleIds in a way that |
| 49 | + * core rules come first, then plugin rules |
| 50 | + * for human readability |
| 51 | + */ |
| 52 | +export const sortedRuleIds = (ruleIds: Iterable<string>): string[] => |
| 53 | + Array.from(ruleIds).sort((a, b) => { |
| 54 | + if (a.includes('/') && !b.includes('/')) { |
| 55 | + return 1; |
| 56 | + } |
| 57 | + if (!a.includes('/') && b.includes('/')) { |
| 58 | + return -1; |
| 59 | + } |
| 60 | + return collator.compare(a, b); |
| 61 | + }); |
| 62 | + |
| 63 | +export const lintESLintConfigRules = async ( |
| 64 | + /** |
| 65 | + * The directory to lint, default to `process.cwd()` |
| 66 | + */ |
| 67 | + cwd = defaultCwd, |
| 68 | +): Promise<IESLintConfigRulesLintResult> => { |
| 69 | + const eslint = new ESLint({ cwd }); |
| 70 | + const compat = new FlatCompat({ |
| 71 | + baseDirectory: cwd, |
| 72 | + resolvePluginsRelativeTo: cwd, |
| 73 | + }); |
| 74 | + |
| 75 | + let usedRuleIds: Set<string> = new Set(); |
| 76 | + let usedPluginSpecifiers: Set<string> = new Set(); |
| 77 | + const ruleMap: ESLintRuleMap = new Map( |
| 78 | + Array.from(builtinRules.entries(), ([ruleId, rule]) => [ |
| 79 | + ruleId, |
| 80 | + { id: ruleId, meta: rule.meta } satisfies IESLintRule, |
| 81 | + ]), |
| 82 | + ); |
| 83 | + |
| 84 | + /** |
| 85 | + * Iterate over all files in the project to |
| 86 | + * collect used rules and plugin specifiers |
| 87 | + */ |
| 88 | + await fsWalk |
| 89 | + .walkStream( |
| 90 | + cwd, |
| 91 | + new fsWalk.Settings({ |
| 92 | + deepFilter: (_entry) => |
| 93 | + !['.git', 'node_modules'].includes(basename(_entry.path)), |
| 94 | + entryFilter: (_entry) => _entry.dirent.isFile(), |
| 95 | + }), |
| 96 | + ) |
| 97 | + .forEach( |
| 98 | + async (entry: Promise<Entry>) => { |
| 99 | + const config = (await eslint.calculateConfigForFile( |
| 100 | + (await entry).path, |
| 101 | + )) as |
| 102 | + | undefined |
| 103 | + | { |
| 104 | + rules: Record<string, unknown>; |
| 105 | + plugins: string[]; |
| 106 | + }; |
| 107 | + |
| 108 | + if (config !== undefined) { |
| 109 | + usedRuleIds = usedRuleIds.union(new Set(Object.keys(config.rules))); |
| 110 | + usedPluginSpecifiers = usedPluginSpecifiers.union( |
| 111 | + new Set(config.plugins), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * config === undefined, means the file is ignored by ESLint |
| 117 | + * @see https://github.com/eslint/eslint/blob/63881dc11299aba1d0960747c199a4cf48d6b9c8/lib/eslint/eslint.js#L1208-L1212 |
| 118 | + */ |
| 119 | + }, |
| 120 | + { |
| 121 | + concurrency: cpus().length * 2, |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + // resolve all plugins |
| 126 | + const pluginMap: Map<string, ESLint.Plugin> = new Map( |
| 127 | + Object.entries(compat.plugins(...usedPluginSpecifiers)[0].plugins ?? {}), |
| 128 | + ); |
| 129 | + |
| 130 | + for (const [pluginName, plugin] of pluginMap.entries()) { |
| 131 | + /** |
| 132 | + * MEMO: do not use plugin.meta.name, it is not always available |
| 133 | + */ |
| 134 | + for (const [ruleId, rule] of Object.entries(plugin.rules ?? {})) { |
| 135 | + if (typeof rule === 'object') { |
| 136 | + ruleMap.set(`${pluginName}/${ruleId}`, { |
| 137 | + id: `${pluginName}/${ruleId}`, |
| 138 | + meta: rule.meta, |
| 139 | + }); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + const usedUnknownRules = usedRuleIds.difference(ruleMap); |
| 145 | + const usedKnownRules = usedRuleIds.intersection(ruleMap); |
| 146 | + const usedDeprecatedRules = new Set( |
| 147 | + Array.from(usedKnownRules).filter((ruleId) => { |
| 148 | + return ruleMap.get(ruleId)?.meta?.deprecated; |
| 149 | + }), |
| 150 | + ); |
| 151 | + |
| 152 | + return { |
| 153 | + eslint, |
| 154 | + compat, |
| 155 | + |
| 156 | + usedRuleIds, |
| 157 | + usedPluginSpecifiers, |
| 158 | + pluginMap, |
| 159 | + ruleMap, |
| 160 | + |
| 161 | + usedUnknownRuleIds: usedUnknownRules, |
| 162 | + usedKnownRuleIds: usedKnownRules, |
| 163 | + usedDeprecatedRuleIds: usedDeprecatedRules, |
| 164 | + }; |
| 165 | +}; |
0 commit comments