Skip to content

Commit 7f54c15

Browse files
committedMar 9, 2025
feat!: sxzz preset return Composer
1 parent 41d0afe commit 7f54c15

File tree

7 files changed

+57
-24
lines changed

7 files changed

+57
-24
lines changed
 

‎eslint.config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// import { sxzz } from './dist/index.js'
21
import { sxzz } from './src/index'
32

43
export default sxzz(

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@eslint/markdown": "catalog:plugins",
5555
"eslint-config-flat-gitignore": "catalog:plugins",
5656
"eslint-config-prettier": "catalog:plugins",
57+
"eslint-flat-config-utils": "catalog:",
5758
"eslint-plugin-antfu": "catalog:plugins",
5859
"eslint-plugin-command": "catalog:plugins",
5960
"eslint-plugin-de-morgan": "catalog:plugins",

‎pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pnpm-workspace.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ overrides:
44
catalog:
55
'@eslint/js': ^9.21.0
66
eslint: ^9.21.0
7+
eslint-flat-config-utils: ^2.0.1
78
globals: ^16.0.0
89
local-pkg: ^1.1.1
910

‎scripts/typegen.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ import { builtinRules } from 'eslint/use-at-your-own-risk'
44
import pico from 'picocolors'
55
import { sxzz } from '../src/presets'
66

7-
const dts = await flatConfigsToRulesDTS(
8-
await sxzz(
9-
[
10-
{
11-
plugins: { '': { rules: Object.fromEntries(builtinRules) } },
12-
},
13-
],
14-
{ vue: true, unocss: true, pnpm: true },
15-
),
16-
{ includeAugmentation: false, exportTypeName: 'Rules' },
7+
const configs = await sxzz(
8+
[
9+
{
10+
plugins: { '': { rules: Object.fromEntries(builtinRules) } },
11+
},
12+
],
13+
{ vue: true, unocss: true, pnpm: true },
1714
)
15+
let dts = await flatConfigsToRulesDTS(configs, {
16+
includeAugmentation: false,
17+
exportTypeName: 'Rules',
18+
})
19+
20+
const configNames = configs.map((i) => i.name).filter(Boolean) as string[]
21+
dts += `
22+
// Names of all the configs
23+
export type ConfigNames = ${configNames.map((i) => `'${i}'`).join(' | ')}
24+
`
1825

1926
await writeFile('src/typegen.ts', dts)
2027

‎src/presets.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FlatConfigComposer, type Awaitable } from 'eslint-flat-config-utils'
12
import {
23
command,
34
comments,
@@ -24,6 +25,7 @@ import {
2425
yml,
2526
} from './configs'
2627
import { hasUnocss, hasVue } from './env'
28+
import type { ConfigNames } from './typegen'
2729
import type { Config } from './types'
2830

2931
/** Ignore common files and include javascript support */
@@ -74,7 +76,7 @@ export const presetAll = async (): Promise<Config[]> => [
7476
]
7577

7678
/** `@sxzz`'s preset. */
77-
export async function sxzz(
79+
export function sxzz(
7880
config: Config | Config[] = [],
7981
{
8082
command: enableCommand = true,
@@ -96,29 +98,31 @@ export async function sxzz(
9698
command: boolean
9799
pnpm: boolean
98100
}> = {},
99-
): Promise<Config[]> {
100-
const configs: Config[] = [...presetBasic(), ...yml(), ...presetJsonc()]
101+
): FlatConfigComposer<Config, ConfigNames> {
102+
const configs: Awaitable<Config[]>[] = [presetBasic(), yml(), presetJsonc()]
101103
if (enableVue) {
102-
configs.push(...vue())
104+
configs.push(vue())
103105
}
104106
if (enableMarkdown) {
105-
configs.push(...markdown())
107+
configs.push(markdown())
106108
}
107109
if (enableUnocss) {
108-
configs.push(...(await unocss()))
110+
configs.push(unocss())
109111
}
110112
if (enablePrettier) {
111-
configs.push(...prettier())
113+
configs.push(prettier())
112114
}
113115
if (enableCommand) {
114-
configs.push(...command())
116+
configs.push(command())
115117
}
116118
if (enablePnpm) {
117-
configs.push(...(await pnpm()))
119+
configs.push(pnpm())
118120
}
119121
if (Object.keys(config).length > 0) {
120-
configs.push(...(Array.isArray(config) ? config : [config]))
122+
configs.push(Array.isArray(config) ? config : [config])
121123
}
122-
configs.push(...specialCases())
123-
return configs
124+
configs.push(specialCases())
125+
126+
const composer = new FlatConfigComposer<Config, ConfigNames>(...configs)
127+
return composer
124128
}

‎src/types.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
import type { Rules } from './typegen'
22
import type { Linter } from 'eslint'
33

4-
export type Config = Linter.Config<Linter.RulesRecord & Rules>
4+
export type Config = Omit<
5+
Linter.Config<Linter.RulesRecord & Rules>,
6+
'plugins'
7+
> & {
8+
// Relax plugins type limitation, as most of the plugins did not have correct type info yet.
9+
/**
10+
* An object containing a name-value mapping of plugin names to plugin objects. When `files` is specified, these plugins are only available to the matching files.
11+
*
12+
* @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration)
13+
*/
14+
plugins?: Record<string, any>
15+
}

0 commit comments

Comments
 (0)
Please sign in to comment.