Skip to content

Commit

Permalink
feat!: use flat config (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 21, 2023
1 parent 6254fa8 commit 3ad62d5
Show file tree
Hide file tree
Showing 114 changed files with 14,183 additions and 2,690 deletions.
6 changes: 0 additions & 6 deletions .eslintrc.json

This file was deleted.

39 changes: 39 additions & 0 deletions .vscode/settings.json
@@ -0,0 +1,39 @@
{
// Enable the flat config support
"eslint.experimental.useFlatConfig": true,

// Disable the default formatter
"prettier.enable": false,
"editor.formatOnSave": false,

// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.organizeImports": false
},

// Silent the stylistic rules in you IDE, but still auto fix them
"eslint.rules.customizations": [
{
"rule": "@stylistic/*",
"severity": "off"
}
],

// The following is optional.
// It's better to put under project setting `.vscode/settings.json`
// to avoid conflicts with working with different eslint configs
// that does not support all formats.
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"html",
"markdown",
"json",
"jsonc",
"yaml"
]
}
140 changes: 111 additions & 29 deletions README.md
Expand Up @@ -8,9 +8,13 @@
- Lint also for json, yaml, markdown
- Sorted imports, dangling commas
- Reasonable defaults, best practices, only one-line of config
- [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
- **Style principle**: Minimal for reading, stable for diff

> Configs uses [ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)
> Configs uses [🌈 ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)
> [!IMPORTANT]
> The main branch is for v1.0-beta, which rewrites to ESLint Flat config, check #250 for more details.
## Usage

Expand All @@ -20,12 +24,20 @@
pnpm add -D eslint @antfu/eslint-config
```

### Config `.eslintrc`
### Create config file

```json
{
"extends": "@antfu"
}
```js
// eslint.config.js
import antfu from '@antfu/eslint-config'

export default [
...antfu,
{
rules: {
// your overrides
},
},
]
```

> You don't need `.eslintignore` normally as it has been provided by the preset.
Expand All @@ -43,14 +55,18 @@ For example:
}
```

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

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

Add the following settings to your `settings.json`:

```jsonc
{
// Enable the flat config support
"eslint.experimental.useFlatConfig": true,

// Disable the default formatter
"prettier.enable": false,
"editor.formatOnSave": false,

Expand Down Expand Up @@ -87,19 +103,98 @@ Add the following settings to your `settings.json`:
}
```

### TypeScript Aware Rules
## Flat Config

Type aware rules are enabled when a `tsconfig.eslint.json` is found in the project root, which will introduce some stricter rules into your project. If you want to enable it while have no `tsconfig.eslint.json` in the project root, you can change tsconfig name by modifying `ESLINT_TSCONFIG` env.
Since v1.0, we migrated to [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), provides a much better organization and composition.

You can now compose your own config easily:

```js
// .eslintrc.js
const process = require('node:process')
// eslint.config.js
import {
presetAuto,
presetJavaScriptCore,
presetLangsExtensions,
presetTypeScript,
} from '@antfu/eslint-config'

export default [
// javascript, node, unicorn, jsdoc, imports, etc.
...presetJavaScriptCore,
// typescript support
...presetTypeScript,
// yaml, markdown, json, support
...presetLangsExtensions,
]
```

process.env.ESLINT_TSCONFIG = 'tsconfig.json'
Or even more granular:

module.exports = {
extends: '@antfu'
}
```js
// eslint.config.js
import {
comments,
ignores,
imports,
javascript,
javascriptStylistic,
jsdoc,
jsonc,
markdown,
node,
sortPackageJson,
sortTsconfig,
typescript,
typescriptStylistic,
unicorn,
vue,
yml,
} from '@antfu/eslint-config'

export default [
...ignores,
...javascript,
...comments,
...node,
...jsdoc,
...imports,
...unicorn,
...javascriptStylistic,

...typescript,
...typescriptStylistic,

...vue,

...jsonc,
...yml,
...markdown,
]
```

Check out the [presets](https://github.com/antfu/eslint-config/blob/main/packages/eslint-config/src/presets.ts) and [configs](https://github.com/antfu/eslint-config/blob/main/packages/eslint-config/src/configs) for more details.

> Thanks to [sxzz/eslint-config](https://github.com/sxzz/eslint-config) for the inspiration and reference.
### Type Aware Rules

You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by importing `typescriptWithLanguageServer` config:

```js
// eslint.config.js
import { presetAuto, typescriptWithLanguageServer } from '@antfu/eslint-config'

export default [
...presetAuto,
...typescriptWithLanguageServer({
tsconfig: 'tsconfig.json', // path to your tsconfig
}),
{
rules: {
// your overrides
},
},
]
```

### Lint Staged
Expand Down Expand Up @@ -145,20 +240,7 @@ This config does NOT lint CSS. I personally use [UnoCSS](https://github.com/unoc

### I prefer XXX...

Sure, you can override the rules in your `.eslintrc` file.

<!-- eslint-skip -->

```jsonc
{
"extends": "@antfu",
"rules": {
// your rules...
}
}
```

Or you can always fork this repo and make your own.
Sure, you can override rules locally in your project to fit your needs. Or you can always fork this repo and make your own.

## Check Also

Expand Down
19 changes: 19 additions & 0 deletions eslint.config.js
@@ -0,0 +1,19 @@
import { defineFlatConfig } from 'eslint-define-config'
import { presetAuto } from '@antfu/eslint-config'
import stylisticMigrate from '@stylistic/eslint-plugin-migrate'
import sortKeys from 'eslint-plugin-sort-keys'

export default defineFlatConfig([
...presetAuto,
{
files: ['**/eslint-config/src/**/*.ts'],
plugins: {
'@stylistic/migrate': stylisticMigrate,
'sort-keys': sortKeys,
},
rules: {
'@stylistic/migrate/rules': 'error',
'sort-keys/sort-keys-fix': 'error',
},
},
])
File renamed without changes.
1 change: 1 addition & 0 deletions fixtures/vitesse/.gitignore → example/vitesse/.gitignore
Expand Up @@ -7,3 +7,4 @@ dist-ssr
node_modules
.idea/
*.log
cypress/downloads
2 changes: 2 additions & 0 deletions example/vitesse/.npmrc
@@ -0,0 +1,2 @@
shamefully-hoist=true
strict-peer-dependencies=false
2 changes: 1 addition & 1 deletion fixtures/vitesse/LICENSE → example/vitesse/LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2021 Anthony Fu
Copyright (c) 2020-PRESENT Anthony Fu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit 3ad62d5

Please sign in to comment.