import type { Linter } from 'eslint'; /** * Concat multiple flat configs into a single flat config array. * * It also resolves promises and flattens the result. * * @example * * ```ts * import eslint from '@eslint/js' * * export default ESLintConfig.concat( * { * plugins: {}, * rules: {}, * }, * // It can also takes a array of configs: * [ * { * plugins: {}, * rules: {}, * } * // ... * ], * // Or promises: * Promise.resolve({ * files: ['*.ts'], * rules: {}, * }) * ); * ``` * @param configs */ async function concat( ...configs: Array> | Promise | ReadonlyArray | T> ): Promise> { // eslint-disable-next-line ts/await-thenable const resolved = await Promise.all(configs); return resolved.flat() as Array; } /** * Always return 'off'. `_status` is the previous rule value. * * @param _status */ function fixme(_status: [number | string, ...Array] | number | string | undefined) { return 'off' as const; } /** * Maps rule names and values using the provided mapping function. * * @example * ```ts * ESLintConfig.mapRules( * { * 'rule-name': 'error', * 'rule-to-drop': 'error', * }, * (rule, value) => { * if (rule === 'rule-name') return ['new-rule-name', value]; * return undefined; * } * ); // { 'new-rule-name': 'error' } * ``` * @param rules * @param mapFn */ function mapRules(rules: Record, mapFn: (rule: string, value: any) => [string, any] | undefined): Record { const mappedRules = Object.create(null) as Record; for (let index = 0, keys = Object.keys(rules); index < keys.length; index++) { // eslint-disable-next-line ts/no-non-null-assertion const key = keys[index]!; const mapped = mapFn(key, rules[key]); if (mapped == null) continue; // eslint-disable-next-line ts/no-unsafe-assignment mappedRules[mapped[0]] = mapped[1]; } // eslint-disable-next-line ts/no-unsafe-argument Object.setPrototypeOf(mappedRules, Object.getPrototypeOf(rules)); return mappedRules; } /** * Return a new merged flat configuration * * @param configs */ function merge(...configs: Array): T { const keys = new Set(configs.flatMap((i) => Object.keys(i))); const merged = configs.reduce((acc, cur) => { return { ...acc, ...cur, files: [ ...(acc.files ?? []), ...(cur.files ?? []), ], ignores: [ ...(acc.ignores ?? []), ...(cur.ignores ?? []), ], languageOptions: { ...acc.languageOptions, ...cur.languageOptions, }, linterOptions: { ...acc.linterOptions, ...cur.linterOptions, }, plugins: { ...acc.plugins, ...cur.plugins, }, rules: { ...acc.rules, ...cur.rules, }, }; }, {} as T); // Remove unused keys for (const key of Object.keys(merged)) { if (!keys.has(key)) // eslint-disable-next-line ts/no-dynamic-delete, ts/no-unsafe-member-access delete (merged as any)[key]; } return merged; } /** * Renames rules in the given object according to the given map. * * Given a map `{ 'old-prefix': 'new-prefix' }`, and a rule object * `{ 'old-prefix/rule-name': 'error' }`, this function will return * `{ 'new-prefix/rule-name': 'error' }`. * * @param rules The object containing the rules to rename. * @param map The object containing the rename map. */ function renameRules(rules: Record, map: Record): Record { const mapKeys = Object.keys(map); return mapRules(rules, (key, value) => { // eslint-disable-next-line ts/prefer-for-of for (let index = 0; index < mapKeys.length; index++) { // eslint-disable-next-line ts/no-non-null-assertion const from = mapKeys[index]!; // eslint-disable-next-line ts/no-non-null-assertion const to = map[from]!; if (key.startsWith(`${from}/`)) return [to + key.slice(from.length), value]; else if (from === '' && !key.includes('/') && to !== '') return [to + key, value]; } return [key, value]; }); } /** * @namespace */ export const ESLintConfig = Object.freeze({ concat, fixme, mapRules, merge, renameRules, });