2021-10-31 22:58:00 +08:00
# @antfu/eslint-config
2022-03-11 04:46:10 +08:00
[![npm ](https://img.shields.io/npm/v/@antfu/eslint-config?color=a1b858&label= )](https://npmjs.com/package/@antfu/eslint-config)
2021-10-31 22:58:00 +08:00
2022-03-11 04:43:17 +08:00
- Single quotes, no semi
2023-03-24 17:08:38 +08:00
- Auto fix for formatting (aimed to be used standalone **without** Prettier)
2022-09-19 10:30:50 +08:00
- Designed to work with TypeScript, Vue out-of-box
2022-03-11 04:43:17 +08:00
- Lint also for json, yaml, markdown
2022-11-30 09:04:41 +08:00
- Sorted imports, dangling commas
2022-03-11 04:43:17 +08:00
- Reasonable defaults, best practices, only one-line of config
2022-11-30 09:04:41 +08:00
- **Style principle**: Minimal for reading, stable for diff
2022-03-11 04:43:17 +08:00
2021-10-31 22:58:00 +08:00
## Usage
### Install
```bash
pnpm add -D eslint @antfu/eslint -config
```
### Config `.eslintrc`
```json
{
2022-03-10 20:22:02 +08:00
"extends": "@antfu"
2021-10-31 22:58:00 +08:00
}
```
2022-03-11 04:43:17 +08:00
> You don't need `.eslintignore` normally as it has been provided by the preset.
2021-10-31 22:58:00 +08:00
### Add script for package.json
For example:
```json
{
"scripts": {
2022-04-06 20:45:50 +08:00
"lint": "eslint .",
"lint:fix": "eslint . --fix"
2021-10-31 22:58:00 +08:00
}
}
```
2023-06-28 20:10:01 +08:00
### VS Code support (auto fix)
2021-10-31 22:58:00 +08:00
2023-06-28 20:10:01 +08:00
Install [VS Code ESLint extension ](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint )
2021-10-31 22:58:00 +08:00
2023-06-28 20:10:01 +08:00
Add the following settings to your `settings.json` :
```jsonc
2021-10-31 22:58:00 +08:00
{
"prettier.enable": false,
2022-10-01 02:45:37 +08:00
"editor.formatOnSave": false,
2021-10-31 22:58:00 +08:00
"editor.codeActionsOnSave": {
2023-06-18 18:42:25 +08:00
"source.fixAll.eslint": true,
2023-07-16 06:20:31 +08:00
"source.organizeImports": false
2023-07-10 09:02:20 +08:00
},
2023-07-16 06:20:31 +08:00
2023-07-10 09:02:20 +08:00
// 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"
]
2021-10-31 22:58:00 +08:00
}
```
2022-03-11 04:46:10 +08:00
2022-11-30 09:02:21 +08:00
### TypeScript Aware Rules
2023-06-18 18:42:25 +08:00
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.
2022-12-13 01:01:40 +08:00
```js
// .eslintrc.js
process.env.ESLINT_TSCONFIG = 'tsconfig.json'
module.exports = {
extends: '@antfu'
}
```
2022-11-30 09:02:21 +08:00
2023-03-24 17:08:38 +08:00
### Lint Staged
2022-10-10 07:33:09 +08:00
2023-03-24 17:08:38 +08:00
If you want to apply lint and auto-fix before every commit, you can add the following to your `package.json` :
```json
{
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
},
"lint-staged": {
"*": "eslint --fix"
}
}
```
and then
```bash
npm i -D lint-staged simple-git-hooks
```
## FAQ
### Prettier?
[Why I don't use Prettier ](https://antfu.me/posts/why-not-prettier )
### How to lint CSS?
This config does NOT lint CSS. I personally use [UnoCSS ](https://github.com/unocss/unocss ) so I don't write CSS. If you still prefer CSS, you can use [stylelint ](https://stylelint.io/ ) for CSS linting.
### I prefer XXX...
Sure, you can override the rules in your `.eslintrc` file.
2023-03-29 21:46:16 +08:00
<!-- eslint - skip -->
2023-03-24 17:08:38 +08:00
```jsonc
{
"extends": "@antfu",
"rules": {
// your rules...
}
}
```
Or you can always fork this repo and make your own.
2022-10-10 07:33:09 +08:00
2022-03-11 04:46:10 +08:00
## Check Also
2022-04-05 04:45:55 +08:00
- [antfu/dotfiles ](https://github.com/antfu/dotfiles ) - My dotfiles
2022-03-11 04:46:10 +08:00
- [antfu/vscode-settings ](https://github.com/antfu/vscode-settings ) - My VS Code settings
- [antfu/ts-starter ](https://github.com/antfu/ts-starter ) - My starter template for TypeScript library
- [antfu/vitesse ](https://github.com/antfu/vitesse ) - My starter template for Vue & Vite app
## License
2022-07-10 10:44:34 +08:00
[MIT ](./LICENSE ) License © 2019-PRESENT [Anthony Fu ](https://github.com/antfu )