From 6a7df744c99f9c42a8eb5c235a260e53e2fd2c9d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 15 Apr 2024 15:25:29 +0200 Subject: [PATCH] feat: improve types support --- scripts/typegen.ts | 8 +++++++- src/factory.ts | 8 ++++---- src/types.ts | 14 +++----------- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/scripts/typegen.ts b/scripts/typegen.ts index ca24fa5531..27a82fedc8 100644 --- a/scripts/typegen.ts +++ b/scripts/typegen.ts @@ -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) diff --git a/src/factory.ts b/src/factory.ts index d8f7b7a2b5..a229546c92 100644 --- a/src/factory.ts +++ b/src/factory.ts @@ -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, @@ -72,8 +72,8 @@ export const defaultPluginRenaming = { */ export function antfu( options: OptionsConfig & TypedFlatConfigItem = {}, - ...userConfigs: Awaitable | Linter.FlatConfig[]>[] -): FlatConfigComposer { + ...userConfigs: Awaitable | Linter.FlatConfig[]>[] +): FlatConfigComposer { const { astro: enableAstro = false, autoRenamePlugins = true, @@ -254,7 +254,7 @@ export function antfu( if (Object.keys(fusedConfig).length) configs.push([fusedConfig]) - let composer = new FlatConfigComposer() + let composer = new FlatConfigComposer() composer = composer .append( diff --git a/src/types.ts b/src/types.ts index fc2cedd12b..c457f946cc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,18 +4,15 @@ 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 | Promise export type Rules = RuleOptions -export type TypedFlatConfigItem = Omit & { - /** - * Custom name of each config item - */ - name?: string +export type { ConfigNames } +export type TypedFlatConfigItem = Omit, '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. @@ -23,11 +20,6 @@ export type TypedFlatConfigItem = Omit & { * @see [Using plugins in your configuration](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#using-plugins-in-your-configuration) */ plugins?: Record - - /** - * An object containing a name-value mapping of rules to use. - */ - rules?: Linter.RulesRecord & Rules } export interface OptionsFiles {