Skip to content

Commit

Permalink
feat: automatically rename plugins in factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsos committed Mar 21, 2024
1 parent 2106993 commit 769fed6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<UserConfigItem | UserConfigItem[]>[]
Expand Down Expand Up @@ -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> = T extends boolean
Expand Down
64 changes: 64 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>, map: Record<string, string>) {
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<string, string>): 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;
});
}

0 comments on commit 769fed6

Please sign in to comment.