Skip to content

Commit 6a7df74

Browse files
committedApr 15, 2024·
feat: improve types support
1 parent d361a2e commit 6a7df74

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed
 

‎scripts/typegen.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ const configs = await combine(
3535
yaml(),
3636
)
3737

38-
const dts = await flatConfigsToRulesDTS(configs, {
38+
const configNames = configs.map(i => i.name).filter(Boolean) as string[]
39+
40+
let dts = await flatConfigsToRulesDTS(configs, {
3941
includeAugmentation: false,
4042
})
4143

44+
dts = `
45+
type ConfigNames = ${configNames.map(i => `'${i}'`).join(' | ')}
46+
${dts}`
47+
4248
await fs.writeFile('src/typegen.d.ts', dts)

‎src/factory.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'node:fs'
33
import { isPackageExists } from 'local-pkg'
44
import { FlatConfigComposer } from 'eslint-flat-config-utils'
55
import type { Linter } from 'eslint'
6-
import type { Awaitable, OptionsConfig, TypedFlatConfigItem } from './types'
6+
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types'
77
import {
88
astro,
99
comments,
@@ -72,8 +72,8 @@ export const defaultPluginRenaming = {
7272
*/
7373
export function antfu(
7474
options: OptionsConfig & TypedFlatConfigItem = {},
75-
...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer<any> | Linter.FlatConfig[]>[]
76-
): FlatConfigComposer<TypedFlatConfigItem> {
75+
...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer<any, any> | Linter.FlatConfig[]>[]
76+
): FlatConfigComposer<TypedFlatConfigItem, ConfigNames> {
7777
const {
7878
astro: enableAstro = false,
7979
autoRenamePlugins = true,
@@ -254,7 +254,7 @@ export function antfu(
254254
if (Object.keys(fusedConfig).length)
255255
configs.push([fusedConfig])
256256

257-
let composer = new FlatConfigComposer<TypedFlatConfigItem>()
257+
let composer = new FlatConfigComposer<TypedFlatConfigItem, ConfigNames>()
258258

259259
composer = composer
260260
.append(

‎src/types.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@ import type { Options as VueBlocksOptions } from 'eslint-processor-vue-blocks'
44
import type { Linter } from 'eslint'
55
import type { StylisticCustomizeOptions } from '@stylistic/eslint-plugin'
66
import type { VendoredPrettierOptions } from './vender/prettier-types'
7-
import type { RuleOptions } from './typegen'
7+
import type { ConfigNames, RuleOptions } from './typegen'
88

99
export type Awaitable<T> = T | Promise<T>
1010

1111
export type Rules = RuleOptions
1212

13-
export type TypedFlatConfigItem = Omit<Linter.FlatConfig, 'plugins'> & {
14-
/**
15-
* Custom name of each config item
16-
*/
17-
name?: string
13+
export type { ConfigNames }
1814

15+
export type TypedFlatConfigItem = Omit<Linter.FlatConfig<Linter.RulesRecord & Rules>, 'plugins'> & {
1916
// Relax plugins type limitation, as most of the plugins did not have correct type info yet.
2017
/**
2118
* 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.
2219
*
2320
* @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration)
2421
*/
2522
plugins?: Record<string, any>
26-
27-
/**
28-
* An object containing a name-value mapping of rules to use.
29-
*/
30-
rules?: Linter.RulesRecord & Rules
3123
}
3224

3325
export interface OptionsFiles {

0 commit comments

Comments
 (0)
Please sign in to comment.