Skip to content

Commit

Permalink
feat: improve types support
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 15, 2024
1 parent d361a2e commit 6a7df74
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
8 changes: 7 additions & 1 deletion scripts/typegen.ts
Expand Up @@ -35,8 +35,14 @@ const configs = await combine(
yaml(),
)

const dts = await flatConfigsToRulesDTS(configs, {
const configNames = configs.map(i => i.name).filter(Boolean) as string[]

let dts = await flatConfigsToRulesDTS(configs, {
includeAugmentation: false,
})

dts = `
type ConfigNames = ${configNames.map(i => `'${i}'`).join(' | ')}
${dts}`

await fs.writeFile('src/typegen.d.ts', dts)
8 changes: 4 additions & 4 deletions src/factory.ts
Expand Up @@ -3,7 +3,7 @@ import fs from 'node:fs'
import { isPackageExists } from 'local-pkg'
import { FlatConfigComposer } from 'eslint-flat-config-utils'
import type { Linter } from 'eslint'
import type { Awaitable, OptionsConfig, TypedFlatConfigItem } from './types'
import type { Awaitable, ConfigNames, OptionsConfig, TypedFlatConfigItem } from './types'
import {
astro,
comments,
Expand Down Expand Up @@ -72,8 +72,8 @@ export const defaultPluginRenaming = {
*/
export function antfu(
options: OptionsConfig & TypedFlatConfigItem = {},
...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer<any> | Linter.FlatConfig[]>[]
): FlatConfigComposer<TypedFlatConfigItem> {
...userConfigs: Awaitable<TypedFlatConfigItem | TypedFlatConfigItem[] | FlatConfigComposer<any, any> | Linter.FlatConfig[]>[]
): FlatConfigComposer<TypedFlatConfigItem, ConfigNames> {
const {
astro: enableAstro = false,
autoRenamePlugins = true,
Expand Down Expand Up @@ -254,7 +254,7 @@ export function antfu(
if (Object.keys(fusedConfig).length)
configs.push([fusedConfig])

let composer = new FlatConfigComposer<TypedFlatConfigItem>()
let composer = new FlatConfigComposer<TypedFlatConfigItem, ConfigNames>()

composer = composer
.append(
Expand Down
14 changes: 3 additions & 11 deletions src/types.ts
Expand Up @@ -4,30 +4,22 @@ import type { Options as VueBlocksOptions } from 'eslint-processor-vue-blocks'
import type { Linter } from 'eslint'
import type { StylisticCustomizeOptions } from '@stylistic/eslint-plugin'
import type { VendoredPrettierOptions } from './vender/prettier-types'
import type { RuleOptions } from './typegen'
import type { ConfigNames, RuleOptions } from './typegen'

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

export type Rules = RuleOptions

export type TypedFlatConfigItem = Omit<Linter.FlatConfig, 'plugins'> & {
/**
* Custom name of each config item
*/
name?: string
export type { ConfigNames }

export type TypedFlatConfigItem = Omit<Linter.FlatConfig<Linter.RulesRecord & Rules>, 'plugins'> & {
// Relax plugins type limitation, as most of the plugins did not have correct type info yet.
/**
* 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.
*
* @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration)
*/
plugins?: Record<string, any>

/**
* An object containing a name-value mapping of rules to use.
*/
rules?: Linter.RulesRecord & Rules
}

export interface OptionsFiles {
Expand Down

0 comments on commit 6a7df74

Please sign in to comment.