-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-config): update configs and improve type definitions (#578)
- Loading branch information
1 parent
9b395b9
commit bbfa9b1
Showing
24 changed files
with
7,411 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@bfra.me/eslint-config": minor | ||
--- | ||
|
||
Overhaul and refactor types and type generation. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bfra.me/eslint-config": patch | ||
--- | ||
|
||
Add epilogue override for sources of CLIs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@bfra.me/eslint-config": patch | ||
--- | ||
|
||
Add missing config names for TypeScript typed linting. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
src/*.d.ts linguist-generated=true | ||
src/rules.d.ts -diff linguist-generated=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
.eslint-config-inspector | ||
src/types.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,62 @@ | ||
import fs from 'node:fs/promises' | ||
import {builtinRules} from 'eslint/use-at-your-own-risk' | ||
import {composer} from 'eslint-flat-config-utils' | ||
import {flatConfigsToRulesDTS} from 'eslint-typegen/core' | ||
import type vitest from '@vitest/eslint-plugin' | ||
import {defineConfig} from '../src/define-config' | ||
|
||
const configs = await composer( | ||
defineConfig({ | ||
plugins: { | ||
'': { | ||
rules: Object.fromEntries(builtinRules), | ||
}, | ||
const configs = await defineConfig({ | ||
plugins: { | ||
'': { | ||
rules: Object.fromEntries(builtinRules), | ||
}, | ||
vitest: true, | ||
}), | ||
// TODO: The `vitest/valid-title` rule breaks the generated types if saved as a .ts instead of a .d.ts file. | ||
).override('@bfra.me/vitest/plugin', config => { | ||
const { | ||
plugins: {vitest: vitestPlugin}, | ||
} = config as {plugins: {vitest: typeof vitest}} | ||
if (vitestPlugin.rules && 'valid-title' in vitestPlugin.rules) { | ||
// HACK: Remove the rule before passing the config to the type generator. | ||
delete (vitestPlugin.rules as {[key: string]: unknown})['valid-title'] | ||
} | ||
return config | ||
}, | ||
typescript: { | ||
tsconfigPath: 'tsconfig.json', | ||
}, | ||
vitest: true, | ||
}) | ||
|
||
const rulesTypeName = 'Rules' | ||
const configNames = configs.map(config => config.name).filter(Boolean) as string[] | ||
const configNamesDts = | ||
configNames.length > 0 ? `${configNames.map(name => `'${name}'`).join(' | ')}` : 'never' | ||
const configType = `Linter.Config<Linter.RulesRecord & ${rulesTypeName}>` | ||
|
||
let dts = await flatConfigsToRulesDTS(configs, { | ||
const rulesDts = await flatConfigsToRulesDTS(configs, { | ||
exportTypeName: rulesTypeName, | ||
includeAugmentation: false, | ||
includeIgnoreComments: false, | ||
}) | ||
|
||
dts = | ||
`// This file is generated by scripts/generate-types.ts | ||
// Do not edit this file directly. | ||
/* eslint-disable */ | ||
` + | ||
dts + | ||
` | ||
const configNames = configs.map(config => config.name).filter(Boolean) as string[] | ||
const configDts = `import type {Linter} from 'eslint' | ||
import type {FlatConfigComposer, ResolvableFlatConfig} from 'eslint-flat-config-utils' | ||
import type {Rules} from './rules' | ||
import type * as FCUTypes from 'eslint-flat-config-utils' | ||
/** | ||
* Represents a value that resolves to one or more ESLint flat configurations. | ||
* @see https://jsr.io/@antfu/eslint-flat-config-utils/doc/~/ResolvableFlatConfig | ||
*/ | ||
export type AwaitableFlatConfig = ResolvableFlatConfig<Config> | ||
/** | ||
* Each configuration object contains all of the information ESLint needs to execute on a set of files. | ||
* Represents the configuration for the linter. | ||
* This interface extends the {@link Linter.Config} interface, expanding {@link Rules} to include the rules defined in all configurations. | ||
* | ||
* @see https://eslint.org/docs/latest/use/configure/configuration-files#configuration-objects | ||
*/ | ||
export type Config = ${configType} | ||
*/ | ||
export interface Config extends Linter.Config<Linter.RulesRecord & ${rulesTypeName}> {} | ||
/** | ||
* Defines a 'composer' for ESLint flat configurations. | ||
* @template Config - The ESLint flat configuration type, extending Linter.Config. | ||
* @template ConfigNames - A literal union of all flat configurations provided by this package. | ||
* @see {@link ConfigNames} | ||
* @see https://jsr.io/@antfu/eslint-flat-config-utils/doc/~/FlatConfigComposer | ||
*/ | ||
export type FlatConfigComposer< | ||
Config extends Linter.Config = ${configType}, | ||
ConfigNames extends string = ${configNamesDts} | (string & Record<never, never>) | ||
> = FCUTypes.FlatConfigComposer<Config, ConfigNames> | ||
export type ConfigComposer = FlatConfigComposer<Config, ConfigNames> | ||
/** | ||
* Represents a value that resolves to one or more ESLint flat configurations. | ||
* @template Config - The ESLint flat configuration type, extending Linter.Config. | ||
* @see https://jsr.io/@antfu/eslint-flat-config-utils/doc/~/ResolvableFlatConfig | ||
* Defines the names of the available ESLint configurations. | ||
*/ | ||
export type ResolvableFlatConfig< | ||
Config extends Linter.Config = ${configType} | ||
> = FCUTypes.ResolvableFlatConfig<Config> | ||
export type ConfigNames =${configNames.length > 0 ? `\n | ${configNames.map(name => `'${name}'`).join('\n | ')}` : ' never'} | ||
` | ||
|
||
export type * from './define-config' | ||
const preamble = `// This file is generated by scripts/generate-types.ts | ||
// Do not edit this file directly. | ||
` | ||
|
||
await fs.writeFile('src/types.ts', dts) | ||
await fs.writeFile('src/config.d.ts', preamble + configDts) | ||
await fs.writeFile('src/rules.d.ts', preamble + rulesDts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
import {composer} from 'eslint-flat-config-utils' | ||
import type {FlatConfigComposer} from './types' | ||
|
||
type InferConfig<T> = T extends FlatConfigComposer<infer U> ? U : never | ||
type InferConfigNames<T> = T extends FlatConfigComposer<any, infer U> ? U : never | ||
import type {AwaitableFlatConfig, Config, ConfigComposer, ConfigNames} from './config' | ||
|
||
/** | ||
* Composes an ESLint configuration object from the provided flat configurations. | ||
* | ||
* @param configs - The configuration names to compose. | ||
* @returns The composed ESLint configuration object. | ||
*/ | ||
export const composeConfig = composer< | ||
InferConfig<FlatConfigComposer>, | ||
InferConfigNames<FlatConfigComposer> | ||
> | ||
// @ts-expect-error - TypeScript insists that the return type should be `Promise<T>`, but it's aa type which acts like a `Promise<T>`. | ||
export const composeConfig = async (...configs: AwaitableFlatConfig[]): ConfigComposer => | ||
composer<Config, ConfigNames>(...configs) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.