Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: antfu/eslint-config
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.9.1
Choose a base ref
...
head repository: antfu/eslint-config
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.10.0
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Mar 26, 2024

  1. docs: update readme

    antfu committed Mar 26, 2024
    Copy the full SHA
    8d58e77 View commit details
  2. docs: update

    antfu committed Mar 26, 2024
    Copy the full SHA
    8768a6f View commit details
  3. Copy the full SHA
    d33ba66 View commit details
  4. chore: release v2.10.0

    antfu committed Mar 26, 2024
    Copy the full SHA
    85333a0 View commit details
Showing with 78 additions and 35 deletions.
  1. +50 −24 README.md
  2. +1 −0 eslint.config.js
  3. +2 −1 package.json
  4. +9 −0 pnpm-lock.yaml
  5. +16 −10 src/factory.ts
74 changes: 50 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -21,53 +21,46 @@
## Usage

### Wizard
### Starter Wizard

We provided a CLI tool to help you set up your project, or migrate from the legacy config to the new flat config.
We provided a CLI tool to help you set up your project, or migrate from the legacy config to the new flat config with one command.

```bash
npx @antfu/eslint-config@latest
```

### Install
### Manual Install

If you prefer to set up manually:

```bash
pnpm i -D eslint @antfu/eslint-config
```

### Create config file

With [`"type": "module"`](https://nodejs.org/api/packages.html#type) in `package.json` (recommended):
And create `eslint.config.mjs` in your project root:

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

export default antfu()
```

With CJS:

```js
// eslint.config.js
const antfu = require('@antfu/eslint-config').default

module.exports = antfu()
```

> [!TIP]
> ESLint only detects `eslint.config.js` as the flat config entry, meaning you need to put `type: module` in your `package.json` or you have to use CJS in `eslint.config.js`. If you want explicit extension like `.mjs` or `.cjs`, or even `eslint.config.ts`, you can install [`eslint-ts-patch`](https://github.com/antfu/eslint-ts-patch) to fix it.
<details>
<summary>
Combined with legacy config:
</summary>

If you still use some configs from the legacy eslintrc format, you can use the [`@eslint/eslintrc`](https://www.npmjs.com/package/@eslint/eslintrc) package to convert them to the flat config.

```js
// eslint.config.js
const antfu = require('@antfu/eslint-config').default
const { FlatCompat } = require('@eslint/eslintrc')
// eslint.config.mjs
import antfu from '@antfu/eslint-config'
import { FlatCompat } from '@eslint/eslintrc'

const compat = new FlatCompat()

module.exports = antfu(
export default antfu(
{
ignores: [],
},
@@ -86,6 +79,8 @@ module.exports = antfu(

> Note that `.eslintignore` no longer works in Flat config, see [customization](#customization) for more details.
</details>

### Add script for package.json

For example:
@@ -99,7 +94,7 @@ For example:
}
```

## VS Code support (auto fix)
## VS Code support (auto fix on save)

Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)

@@ -305,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:
@@ -359,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.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import styleMigrate from '@stylistic/eslint-plugin-migrate'
import JITI from 'jiti'

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@antfu/eslint-config",
"type": "module",
"version": "2.9.1",
"version": "2.10.0",
"packageManager": "pnpm@8.15.5",
"description": "Anthony's ESLint config",
"author": "Anthony Fu <anthonyfu117@hotmail.com> (https://github.com/antfu/)",
@@ -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",
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
Original file line number Diff line number Diff line change
@@ -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,
@@ -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)[] = [
@@ -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,
@@ -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