From 769fed6cc3da8a181f4a846d2608ffe5b90d7a79 Mon Sep 17 00:00:00 2001 From: Konstantinos Paparas Date: Thu, 21 Mar 2024 15:26:20 +0100 Subject: [PATCH] feat: automatically rename plugins in factory --- src/factory.ts | 15 ++++++++---- src/utils.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/src/factory.ts b/src/factory.ts index 4cf149e..ca9fb1f 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -24,7 +24,7 @@ import { vuetify, yaml, } from './configs'; -import { combine, interopDefault } from './utils'; +import { combine, interopDefault, renamePluginInConfigs } from './utils'; import { storybook } from './configs/storybook'; import type { Awaitable, FlatConfigItem, OptionsConfig, UserConfigItem } from './types'; @@ -46,10 +46,17 @@ const VuePackages = [ '@slidev/cli', ]; +export const defaultPluginRenaming = { + 'import-x': 'import', + 'n': 'node', + 'vitest': 'test', + 'yml': 'yaml', +}; + /** * Construct an array of ESLint flat config items. */ -// eslint-disable-next-line require-await + export async function rotki( options: OptionsConfig & FlatConfigItem = {}, ...userConfigs: Awaitable[] @@ -222,12 +229,12 @@ export async function rotki( if (Object.keys(fusedConfig).length > 0) configs.push([fusedConfig]); - const merged = combine( + const merged = await combine( ...configs, ...userConfigs, ); - return merged; + return renamePluginInConfigs(merged, defaultPluginRenaming); } export type ResolvedOptions = T extends boolean diff --git a/src/utils.ts b/src/utils.ts index e736f74..edb366a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -59,3 +59,67 @@ export async function ensurePackages(packages: string[]) { if (result) await import('@antfu/install-pkg').then(i => i.installPackage(nonExistingPackages, { dev: true })); } + +/** + * Rename plugin prefixes in a rule object. + * Accepts a map of prefixes to rename. + * + * @example + * ```ts + * import { renameRules } from '@antfu/eslint-config' + * + * export default [{ + * rules: renameRules( + * { + * '@typescript-eslint/indent': 'error' + * }, + * { '@typescript-eslint': 'ts' } + * ) + * }] + * ``` + */ +export function renameRules(rules: Record, map: Record) { + return Object.fromEntries( + Object.entries(rules) + .map(([key, value]) => { + for (const [from, to] of Object.entries(map)) { + if (key.startsWith(`${from}/`)) + return [to + key.slice(from.length), value]; + } + return [key, value]; + }), + ); +} + +/** + * Rename plugin names a flat configs array + * + * @example + * ```ts + * import { renamePluginInConfigs } from '@antfu/eslint-config' + * import someConfigs from './some-configs' + * + * export default renamePluginInConfigs(someConfigs, { + * '@typescript-eslint': 'ts', + * 'import-x': 'import', + * }) + * ``` + */ +export function renamePluginInConfigs(configs: UserConfigItem[], map: Record): UserConfigItem[] { + return configs.map((i) => { + const clone = { ...i }; + if (clone.rules) + clone.rules = renameRules(clone.rules, map); + if (clone.plugins) { + clone.plugins = Object.fromEntries( + Object.entries(clone.plugins) + .map(([key, value]) => { + if (key in map) + return [map[key], value]; + return [key, value]; + }), + ); + } + return clone; + }); +}