Skip to content

Commit

Permalink
feat: support flat config pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 26, 2024
1 parent 8768a6f commit d33ba66
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
31 changes: 31 additions & 0 deletions README.md
Expand Up @@ -300,6 +300,8 @@ type foo = { bar: 2 }
>
> Feel free to open issues if you want to combine this config with some other config presets but faced naming collisions. I am happy to figure out a way to make them work. But at this moment I have no plan to revert the renaming.
Since v2.9.0, this preset will automatically rename the plugins also for your custom configs. You can use the original prefix to override the rules directly.

### Rules Overrides

Certain rules would only be enabled in specific files, for example, `ts/*` rules would only be enabled in `.ts` files and `vue/*` rules would only be enabled in `.vue` files. If you want to override the rules, you need to specify the file extension:
Expand Down Expand Up @@ -354,6 +356,35 @@ export default antfu({
})
```

### Pipeline

Since v2.10.0, the factory function `antfu()` returns a [pipeline object from `eslint-flat-config-utils`](https://github.com/antfu/eslint-flat-config-utils#pipe) where you can chain the methods to compose the config even more flexibly.

```js
// eslint.config.js
import antfu from '@antfu/eslint-config'

export default antfu()
.prepend(
// some configs before the main config
)
// overrides any named configs
.override(
'antfu:imports',
{
rules: {
'import/order': ['error', { 'newlines-between': 'always' }],
}
}
)
// rename plugin prefixes
.renamePlugins({
'old-prefix': 'new-prefix',
// ...
})
// ...
```

### Optional Configs

We provide some optional configs for specific use cases, that we don't include their dependencies by default.
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
@@ -1,3 +1,4 @@
// @ts-check
import styleMigrate from '@stylistic/eslint-plugin-migrate'
import JITI from 'jiti'

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -96,6 +96,7 @@
"@typescript-eslint/eslint-plugin": "^7.4.0",
"@typescript-eslint/parser": "^7.4.0",
"eslint-config-flat-gitignore": "^0.1.3",
"eslint-flat-config-utils": "0.0.4",
"eslint-merge-processors": "^0.1.0",
"eslint-plugin-antfu": "^2.1.2",
"eslint-plugin-eslint-comments": "^3.2.0",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions src/factory.ts
@@ -1,6 +1,7 @@
import process from 'node:process'
import fs from 'node:fs'
import { isPackageExists } from 'local-pkg'
import { FlatConfigPipeline } from 'eslint-flat-config-utils'
import type { Awaitable, FlatConfigItem, OptionsConfig, UserConfigItem } from './types'
import {
astro,
Expand All @@ -26,7 +27,7 @@ import {
vue,
yaml,
} from './configs'
import { combine, interopDefault, renamePluginInConfigs } from './utils'
import { interopDefault } from './utils'
import { formatters } from './configs/formatters'

const flatConfigProps: (keyof FlatConfigItem)[] = [
Expand Down Expand Up @@ -67,10 +68,10 @@ export const defaultPluginRenaming = {
* @returns {Promise<UserConfigItem[]>}
* The merged ESLint configurations.
*/
export async function antfu(
export function antfu(
options: OptionsConfig & FlatConfigItem = {},
...userConfigs: Awaitable<UserConfigItem | UserConfigItem[]>[]
): Promise<UserConfigItem[]> {
): FlatConfigPipeline<UserConfigItem> {
const {
astro: enableAstro = false,
autoRenamePlugins = true,
Expand Down Expand Up @@ -242,15 +243,20 @@ export async function antfu(
if (Object.keys(fusedConfig).length)
configs.push([fusedConfig])

const merged = await combine(
...configs,
...userConfigs,
)
let pipeline = new FlatConfigPipeline<UserConfigItem>()

pipeline = pipeline
.append(
...configs,
...userConfigs,
)

if (autoRenamePlugins)
return renamePluginInConfigs(merged, defaultPluginRenaming)
if (autoRenamePlugins) {
pipeline = pipeline
.renamePlugins(defaultPluginRenaming)
}

return merged
return pipeline
}

export type ResolvedOptions<T> = T extends boolean
Expand Down

0 comments on commit d33ba66

Please sign in to comment.