|
| 1 | +# @coderwyd/eslint-config |
| 2 | + |
| 3 | +- Single quotes, no semi |
| 4 | +- Auto fix for formatting (aimed to be used standalone **without** Prettier) |
| 5 | +- Designed to work with TypeScript, Vue out-of-box |
| 6 | +- Lint also for json, yaml, markdown |
| 7 | +- Sorted imports, dangling commas |
| 8 | +- Reasonable defaults, best practices, only one-line of config |
| 9 | +- **Style principle**: Minimal for reading, stable for diff |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Install |
| 14 | + |
| 15 | +```bash |
| 16 | +pnpm add -D eslint @coderwyd/eslint-config |
| 17 | +``` |
| 18 | + |
| 19 | +### Config `.eslintrc` |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "extends": "@coderwyd" |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +> You don't need `.eslintignore` normally as it has been provided by the preset. |
| 28 | +
|
| 29 | +### Add script for package.json |
| 30 | + |
| 31 | +For example: |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "scripts": { |
| 36 | + "lint": "eslint .", |
| 37 | + "lint:fix": "eslint . --fix" |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### VS Code support (auto fix) |
| 43 | + |
| 44 | +Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) |
| 45 | + |
| 46 | +Add the following settings to your `settings.json`: |
| 47 | + |
| 48 | +```jsonc |
| 49 | +{ |
| 50 | + "prettier.enable": false, |
| 51 | + "editor.formatOnSave": false, |
| 52 | + "editor.codeActionsOnSave": { |
| 53 | + "source.fixAll.eslint": true, |
| 54 | + "source.organizeImports": false |
| 55 | + }, |
| 56 | + |
| 57 | + // The following is optional. |
| 58 | + // It's better to put under project setting `.vscode/settings.json` |
| 59 | + // to avoid conflicts with working with different eslint configs |
| 60 | + // that does not support all formats. |
| 61 | + "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "vue", "html", "markdown", "json", "jsonc", "yaml"] |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### TypeScript Aware Rules |
| 66 | + |
| 67 | +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. |
| 68 | + |
| 69 | +```js |
| 70 | +// .eslintrc.js |
| 71 | +const process = require('node:process') |
| 72 | +process.env.ESLINT_TSCONFIG = 'tsconfig.json' |
| 73 | + |
| 74 | +module.exports = { |
| 75 | + extends: '@coderwyd', |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Lint Staged |
| 80 | + |
| 81 | +If you want to apply lint and auto-fix before every commit, you can add the following to your `package.json`: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "simple-git-hooks": { |
| 86 | + "pre-commit": "pnpm lint-staged" |
| 87 | + }, |
| 88 | + "lint-staged": { |
| 89 | + "*": "eslint --fix" |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +and then |
| 95 | + |
| 96 | +```bash |
| 97 | +npm i -D lint-staged simple-git-hooks |
| 98 | +``` |
| 99 | + |
| 100 | +## Badge |
| 101 | + |
| 102 | +If you enjoy this code style, and would like to mention it in your project, here is the badge you can use: |
| 103 | + |
| 104 | +## FAQ |
| 105 | + |
| 106 | +### I prefer XXX... |
| 107 | + |
| 108 | +Sure, you can override the rules in your `.eslintrc` file. |
| 109 | + |
| 110 | +<!-- eslint-skip --> |
| 111 | + |
| 112 | +```jsonc |
| 113 | +{ |
| 114 | + "extends": "@coderwyd", |
| 115 | + "rules": { |
| 116 | + // your rules... |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Or you can always fork this repo and make your own. |
| 122 | + |
| 123 | +## License |
| 124 | + |
| 125 | +[MIT](./LICENSE) License © 2019-PRESENT [Donny Wang](https://github.com/coderwyd) |
0 commit comments