Skip to content

Commit 77df2f5

Browse files
committedMar 21, 2024·
feat: automatically rename plugins in factory
1 parent e0b58ea commit 77df2f5

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed
 

‎src/configs/typescript.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,11 @@ export async function typescript(
9898
rules: {
9999
...renameRules(
100100
pluginTs.configs['eslint-recommended'].overrides![0].rules!,
101-
'@typescript-eslint/',
102-
'ts/',
101+
{ '@typescript-eslint': 'ts' },
103102
),
104103
...renameRules(
105104
pluginTs.configs.strict.rules!,
106-
'@typescript-eslint/',
107-
'ts/',
105+
{ '@typescript-eslint': 'ts' },
108106
),
109107
'no-dupe-class-members': 'off',
110108
'no-loss-of-precision': 'off',

‎src/factory.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
vue,
2727
yaml,
2828
} from './configs'
29-
import { combine, interopDefault } from './utils'
29+
import { combine, interopDefault, renamePluginInConfigs } from './utils'
3030
import { formatters } from './configs/formatters'
3131

3232
const flatConfigProps: (keyof FlatConfigItem)[] = [
@@ -48,6 +48,15 @@ const VuePackages = [
4848
'@slidev/cli',
4949
]
5050

51+
export const defaultPluginRenaming = {
52+
'@stylistic': 'style',
53+
'@typescript-eslint': 'ts',
54+
'import-x': 'import',
55+
'n': 'node',
56+
'vitest': 'test',
57+
'yml': 'yaml',
58+
}
59+
5160
/**
5261
* Construct an array of ESLint flat config items.
5362
*
@@ -64,6 +73,7 @@ export async function antfu(
6473
): Promise<UserConfigItem[]> {
6574
const {
6675
astro: enableAstro = false,
76+
autoRenamePlugins = true,
6777
componentExts = [],
6878
gitignore: enableGitignore = true,
6979
isInEditor = !!((process.env.VSCODE_PID || process.env.VSCODE_CWD || process.env.JETBRAINS_IDE || process.env.VIM) && !process.env.CI),
@@ -232,11 +242,14 @@ export async function antfu(
232242
if (Object.keys(fusedConfig).length)
233243
configs.push([fusedConfig])
234244

235-
const merged = combine(
245+
const merged = await combine(
236246
...configs,
237247
...userConfigs,
238248
)
239249

250+
if (autoRenamePlugins)
251+
return renamePluginInConfigs(merged, defaultPluginRenaming)
252+
240253
return merged
241254
}
242255

‎src/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ export interface OptionsConfig extends OptionsComponentExts {
372372
*/
373373
isInEditor?: boolean
374374

375+
/**
376+
* Automatically rename plugins in the config.
377+
*
378+
* @default true
379+
*/
380+
autoRenamePlugins?: boolean
381+
375382
/**
376383
* Provide overrides for rules for each integration.
377384
*

‎src/utils.ts

+57-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import process from 'node:process'
22
import { isPackageExists } from 'local-pkg'
3-
import type { Awaitable, UserConfigItem } from './types'
3+
import type { Awaitable, FlatConfigItem, UserConfigItem } from './types'
44

55
export const parserPlain = {
66
meta: {
@@ -31,17 +31,70 @@ export async function combine(...configs: Awaitable<UserConfigItem | UserConfigI
3131
return resolved.flat()
3232
}
3333

34-
export function renameRules(rules: Record<string, any>, from: string, to: string) {
34+
/**
35+
* Rename plugin prefixes in a rule object.
36+
* Accepts a map of prefixes to rename.
37+
*
38+
* @example
39+
* ```ts
40+
* import { renameRules } from '@antfu/eslint-config'
41+
*
42+
* export default [{
43+
* rules: renameRules(
44+
* {
45+
* '@typescript-eslint/indent': 'error'
46+
* },
47+
* { '@typescript-eslint': 'ts' }
48+
* )
49+
* }]
50+
* ```
51+
*/
52+
export function renameRules(rules: Record<string, any>, map: Record<string, string>) {
3553
return Object.fromEntries(
3654
Object.entries(rules)
3755
.map(([key, value]) => {
38-
if (key.startsWith(from))
39-
return [to + key.slice(from.length), value]
56+
for (const [from, to] of Object.entries(map)) {
57+
if (key.startsWith(`${from}/`))
58+
return [to + key.slice(from.length), value]
59+
}
4060
return [key, value]
4161
}),
4262
)
4363
}
4464

65+
/**
66+
* Rename plugin names a flat configs array
67+
*
68+
* @example
69+
* ```ts
70+
* import { renamePluginInConfigs } from '@antfu/eslint-config'
71+
* import someConfigs from './some-configs'
72+
*
73+
* export default renamePluginInConfigs(someConfigs, {
74+
* '@typescript-eslint': 'ts',
75+
* 'import-x': 'import',
76+
* })
77+
* ```
78+
*/
79+
export function renamePluginInConfigs(configs: UserConfigItem[], map: Record<string, string>): UserConfigItem[] {
80+
return configs.map((i) => {
81+
const clone = { ...i }
82+
if (clone.rules)
83+
clone.rules = renameRules(clone.rules, map)
84+
if (clone.plugins) {
85+
clone.plugins = Object.fromEntries(
86+
Object.entries(clone.plugins)
87+
.map(([key, value]) => {
88+
if (key in map)
89+
return [map[key], value]
90+
return [key, value]
91+
}),
92+
)
93+
}
94+
return clone
95+
})
96+
}
97+
4598
export function toArray<T>(value: T | T[]): T[] {
4699
return Array.isArray(value) ? value : [value]
47100
}

0 commit comments

Comments
 (0)
Please sign in to comment.