@dhzh/eslint-config 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +623 -0
- package/bin/index.js +2 -0
- package/dist/cli.cjs +584 -0
- package/dist/cli.d.cts +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +555 -0
- package/dist/index.cjs +2330 -0
- package/dist/index.d.cts +599 -0
- package/dist/index.d.ts +599 -0
- package/dist/index.js +2233 -0
- package/package.json +167 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019-PRESENT Anthony Fu<https://github.com/antfu>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
# @antfu/eslint-config
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/@antfu/eslint-config) [](https://github.com/antfu/eslint-config)
|
|
4
|
+
|
|
5
|
+
- Single quotes, no semi
|
|
6
|
+
- Auto fix for formatting (aimed to be used standalone **without** Prettier)
|
|
7
|
+
- Sorted imports, dangling commas
|
|
8
|
+
- Reasonable defaults, best practices, only one line of config
|
|
9
|
+
- Designed to work with TypeScript, JSX, Vue out-of-box
|
|
10
|
+
- Lints also for json, yaml, toml, markdown
|
|
11
|
+
- Opinionated, but [very customizable](#customization)
|
|
12
|
+
- [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
|
|
13
|
+
- Using [ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)
|
|
14
|
+
- Respects `.gitignore` by default
|
|
15
|
+
- Optional [React](#react), [Svelte](#svelte), [UnoCSS](#unocss), [Astro](#astro) support
|
|
16
|
+
- Optional [formatters](#formatters) support for CSS, HTML, etc.
|
|
17
|
+
- **Style principle**: Minimal for reading, stable for diff, consistent
|
|
18
|
+
|
|
19
|
+
> [!IMPORTANT]
|
|
20
|
+
> Since v1.0.0, this config is rewritten to the new [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), check the [release note](https://github.com/antfu/eslint-config/releases/tag/v1.0.0) for more details.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Wizard
|
|
25
|
+
|
|
26
|
+
We provided a CLI tool to help you set up your project, or migrate from the legacy config to the new flat config.
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npx @antfu/eslint-config@latest
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm i -D eslint @antfu/eslint-config
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Create config file
|
|
39
|
+
|
|
40
|
+
With [`"type": "module"`](https://nodejs.org/api/packages.html#type) in `package.json` (recommended):
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
// eslint.config.js
|
|
44
|
+
import antfu from '@antfu/eslint-config'
|
|
45
|
+
|
|
46
|
+
export default antfu()
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
With CJS:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
// eslint.config.js
|
|
53
|
+
const antfu = require('@antfu/eslint-config').default
|
|
54
|
+
|
|
55
|
+
module.exports = antfu()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
> [!TIP]
|
|
59
|
+
> 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.
|
|
60
|
+
|
|
61
|
+
Combined with legacy config:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
// eslint.config.js
|
|
65
|
+
const antfu = require('@antfu/eslint-config').default
|
|
66
|
+
const { FlatCompat } = require('@eslint/eslintrc')
|
|
67
|
+
|
|
68
|
+
const compat = new FlatCompat()
|
|
69
|
+
|
|
70
|
+
module.exports = antfu(
|
|
71
|
+
{
|
|
72
|
+
ignores: [],
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
// Legacy config
|
|
76
|
+
...compat.config({
|
|
77
|
+
extends: [
|
|
78
|
+
'eslint:recommended',
|
|
79
|
+
// Other extends...
|
|
80
|
+
],
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// Other flat configs...
|
|
84
|
+
)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
> Note that `.eslintignore` no longer works in Flat config, see [customization](#customization) for more details.
|
|
88
|
+
|
|
89
|
+
### Add script for package.json
|
|
90
|
+
|
|
91
|
+
For example:
|
|
92
|
+
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"scripts": {
|
|
96
|
+
"lint": "eslint .",
|
|
97
|
+
"lint:fix": "eslint . --fix"
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## VS Code support (auto fix)
|
|
103
|
+
|
|
104
|
+
Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
|
|
105
|
+
|
|
106
|
+
Add the following settings to your `.vscode/settings.json`:
|
|
107
|
+
|
|
108
|
+
```jsonc
|
|
109
|
+
{
|
|
110
|
+
// Enable the ESlint flat config support
|
|
111
|
+
"eslint.experimental.useFlatConfig": true,
|
|
112
|
+
|
|
113
|
+
// Disable the default formatter, use eslint instead
|
|
114
|
+
"prettier.enable": false,
|
|
115
|
+
"editor.formatOnSave": false,
|
|
116
|
+
|
|
117
|
+
// Auto fix
|
|
118
|
+
"editor.codeActionsOnSave": {
|
|
119
|
+
"source.fixAll.eslint": "explicit",
|
|
120
|
+
"source.organizeImports": "never"
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
// Silent the stylistic rules in you IDE, but still auto fix them
|
|
124
|
+
"eslint.rules.customizations": [
|
|
125
|
+
{ "rule": "style/*", "severity": "off" },
|
|
126
|
+
{ "rule": "format/*", "severity": "off" },
|
|
127
|
+
{ "rule": "*-indent", "severity": "off" },
|
|
128
|
+
{ "rule": "*-spacing", "severity": "off" },
|
|
129
|
+
{ "rule": "*-spaces", "severity": "off" },
|
|
130
|
+
{ "rule": "*-order", "severity": "off" },
|
|
131
|
+
{ "rule": "*-dangle", "severity": "off" },
|
|
132
|
+
{ "rule": "*-newline", "severity": "off" },
|
|
133
|
+
{ "rule": "*quotes", "severity": "off" },
|
|
134
|
+
{ "rule": "*semi", "severity": "off" }
|
|
135
|
+
],
|
|
136
|
+
|
|
137
|
+
// Enable eslint for all supported languages
|
|
138
|
+
"eslint.validate": [
|
|
139
|
+
"javascript",
|
|
140
|
+
"javascriptreact",
|
|
141
|
+
"typescript",
|
|
142
|
+
"typescriptreact",
|
|
143
|
+
"vue",
|
|
144
|
+
"html",
|
|
145
|
+
"markdown",
|
|
146
|
+
"json",
|
|
147
|
+
"jsonc",
|
|
148
|
+
"yaml",
|
|
149
|
+
"toml"
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Customization
|
|
155
|
+
|
|
156
|
+
Since v1.0, we migrated to [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new). It provides much better organization and composition.
|
|
157
|
+
|
|
158
|
+
Normally you only need to import the `antfu` preset:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
// eslint.config.js
|
|
162
|
+
import antfu from '@antfu/eslint-config'
|
|
163
|
+
|
|
164
|
+
export default antfu()
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
And that's it! Or you can configure each integration individually, for example:
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
// eslint.config.js
|
|
171
|
+
import antfu from '@antfu/eslint-config'
|
|
172
|
+
|
|
173
|
+
export default antfu({
|
|
174
|
+
// Enable stylistic formatting rules
|
|
175
|
+
// stylistic: true,
|
|
176
|
+
|
|
177
|
+
// Or customize the stylistic rules
|
|
178
|
+
stylistic: {
|
|
179
|
+
indent: 2, // 4, or 'tab'
|
|
180
|
+
quotes: 'single', // or 'double'
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
// TypeScript and Vue are auto-detected, you can also explicitly enable them:
|
|
184
|
+
typescript: true,
|
|
185
|
+
vue: true,
|
|
186
|
+
|
|
187
|
+
// Disable jsonc and yaml support
|
|
188
|
+
jsonc: false,
|
|
189
|
+
yaml: false,
|
|
190
|
+
|
|
191
|
+
// `.eslintignore` is no longer supported in Flat config, use `ignores` instead
|
|
192
|
+
ignores: [
|
|
193
|
+
'**/fixtures',
|
|
194
|
+
// ...globs
|
|
195
|
+
]
|
|
196
|
+
})
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
The `antfu` factory function also accepts any number of arbitrary custom config overrides:
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
// eslint.config.js
|
|
203
|
+
import antfu from '@antfu/eslint-config'
|
|
204
|
+
|
|
205
|
+
export default antfu(
|
|
206
|
+
{
|
|
207
|
+
// Configures for antfu's config
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
// From the second arguments they are ESLint Flat Configs
|
|
211
|
+
// you can have multiple configs
|
|
212
|
+
{
|
|
213
|
+
files: ['**/*.ts'],
|
|
214
|
+
rules: {},
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
rules: {},
|
|
218
|
+
},
|
|
219
|
+
)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Going more advanced, you can also import fine-grained configs and compose them as you wish:
|
|
223
|
+
|
|
224
|
+
<details>
|
|
225
|
+
<summary>Advanced Example</summary>
|
|
226
|
+
|
|
227
|
+
We wouldn't recommend using this style in general unless you know exactly what they are doing, as there are shared options between configs and might need extra care to make them consistent.
|
|
228
|
+
|
|
229
|
+
```js
|
|
230
|
+
// eslint.config.js
|
|
231
|
+
import {
|
|
232
|
+
combine,
|
|
233
|
+
comments,
|
|
234
|
+
ignores,
|
|
235
|
+
imports,
|
|
236
|
+
javascript,
|
|
237
|
+
jsdoc,
|
|
238
|
+
jsonc,
|
|
239
|
+
markdown,
|
|
240
|
+
node,
|
|
241
|
+
sortPackageJson,
|
|
242
|
+
sortTsconfig,
|
|
243
|
+
stylistic,
|
|
244
|
+
toml,
|
|
245
|
+
typescript,
|
|
246
|
+
unicorn,
|
|
247
|
+
vue,
|
|
248
|
+
yaml,
|
|
249
|
+
} from '@antfu/eslint-config'
|
|
250
|
+
|
|
251
|
+
export default combine(
|
|
252
|
+
ignores(),
|
|
253
|
+
javascript(/* Options */),
|
|
254
|
+
comments(),
|
|
255
|
+
node(),
|
|
256
|
+
jsdoc(),
|
|
257
|
+
imports(),
|
|
258
|
+
unicorn(),
|
|
259
|
+
typescript(/* Options */),
|
|
260
|
+
stylistic(),
|
|
261
|
+
vue(),
|
|
262
|
+
jsonc(),
|
|
263
|
+
yaml(),
|
|
264
|
+
toml(),
|
|
265
|
+
markdown(),
|
|
266
|
+
)
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
</details>
|
|
270
|
+
|
|
271
|
+
Check out the [configs](https://github.com/antfu/eslint-config/blob/main/src/configs) and [factory](https://github.com/antfu/eslint-config/blob/main/src/factory.ts) for more details.
|
|
272
|
+
|
|
273
|
+
> Thanks to [sxzz/eslint-config](https://github.com/sxzz/eslint-config) for the inspiration and reference.
|
|
274
|
+
|
|
275
|
+
### Plugins Renaming
|
|
276
|
+
|
|
277
|
+
Since flat config requires us to explicitly provide the plugin names (instead of the mandatory convention from npm package name), we renamed some plugins to make the overall scope more consistent and easier to write.
|
|
278
|
+
|
|
279
|
+
| New Prefix | Original Prefix | Source Plugin |
|
|
280
|
+
| ---------- | ---------------------- | ------------------------------------------------------------------------------------------ |
|
|
281
|
+
| `import/*` | `import-x/*` | [eslint-plugin-import-x](https://github.com/un-es/eslint-plugin-import-x) |
|
|
282
|
+
| `node/*` | `n/*` | [eslint-plugin-n](https://github.com/eslint-community/eslint-plugin-n) |
|
|
283
|
+
| `yaml/*` | `yml/*` | [eslint-plugin-yml](https://github.com/ota-meshi/eslint-plugin-yml) |
|
|
284
|
+
| `ts/*` | `@typescript-eslint/*` | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) |
|
|
285
|
+
| `style/*` | `@stylistic/*` | [@stylistic/eslint-plugin](https://github.com/eslint-stylistic/eslint-stylistic) |
|
|
286
|
+
| `test/*` | `vitest/*` | [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest) |
|
|
287
|
+
| `test/*` | `no-only-tests/*` | [eslint-plugin-no-only-tests](https://github.com/levibuzolic/eslint-plugin-no-only-tests) |
|
|
288
|
+
|
|
289
|
+
When you want to override rules, or disable them inline, you need to update to the new prefix:
|
|
290
|
+
|
|
291
|
+
```diff
|
|
292
|
+
-// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
293
|
+
+// eslint-disable-next-line ts/consistent-type-definitions
|
|
294
|
+
type foo = { bar: 2 }
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
> [!NOTE]
|
|
298
|
+
> About plugin renaming - it is actually rather a dangrous move that might leading to potential naming collisions, pointed out [here](https://github.com/eslint/eslint/discussions/17766) and [here](https://github.com/prettier/eslint-config-prettier#eslintconfigjs-flat-config-plugin-caveat). As this config also very **personal** and **opinionated**, I ambitiously position this config as the only **"top-level"** config per project, that might pivots the taste of how rules are named.
|
|
299
|
+
>
|
|
300
|
+
> This config cares more about the user-facings DX, and try to ease out the implementation details. For example, users could keep using the semantic `import/order` without ever knowing the underlying plugin has migrated twice to `eslint-plugin-i` and then to `eslint-plugin-import-x`. User are also not forced to migrate to the implicit `i/order` halfway only because we swapped the implementation to a fork.
|
|
301
|
+
>
|
|
302
|
+
> That said, it's probably still not a good idea. You might not want to doing this if you are maintaining your own eslint config.
|
|
303
|
+
>
|
|
304
|
+
> 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.
|
|
305
|
+
|
|
306
|
+
### Rules Overrides
|
|
307
|
+
|
|
308
|
+
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:
|
|
309
|
+
|
|
310
|
+
```js
|
|
311
|
+
// eslint.config.js
|
|
312
|
+
import antfu from '@antfu/eslint-config'
|
|
313
|
+
|
|
314
|
+
export default antfu(
|
|
315
|
+
{
|
|
316
|
+
vue: true,
|
|
317
|
+
typescript: true
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
// Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
|
|
321
|
+
files: ['**/*.vue'],
|
|
322
|
+
rules: {
|
|
323
|
+
'vue/operator-linebreak': ['error', 'before'],
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
// Without `files`, they are general rules for all files
|
|
328
|
+
rules: {
|
|
329
|
+
'style/semi': ['error', 'never'],
|
|
330
|
+
},
|
|
331
|
+
}
|
|
332
|
+
)
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
We also provided a `overrides` options in each integration to make it easier:
|
|
336
|
+
|
|
337
|
+
```js
|
|
338
|
+
// eslint.config.js
|
|
339
|
+
import antfu from '@antfu/eslint-config'
|
|
340
|
+
|
|
341
|
+
export default antfu({
|
|
342
|
+
vue: {
|
|
343
|
+
overrides: {
|
|
344
|
+
'vue/operator-linebreak': ['error', 'before'],
|
|
345
|
+
},
|
|
346
|
+
},
|
|
347
|
+
typescript: {
|
|
348
|
+
overrides: {
|
|
349
|
+
'ts/consistent-type-definitions': ['error', 'interface'],
|
|
350
|
+
},
|
|
351
|
+
},
|
|
352
|
+
yaml: {
|
|
353
|
+
overrides: {
|
|
354
|
+
// ...
|
|
355
|
+
},
|
|
356
|
+
},
|
|
357
|
+
})
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### Optional Configs
|
|
361
|
+
|
|
362
|
+
We provide some optional configs for specific use cases, that we don't include their dependencies by default.
|
|
363
|
+
|
|
364
|
+
#### Formatters
|
|
365
|
+
|
|
366
|
+
> [!WARNING]
|
|
367
|
+
> Experimental feature, changes might not follow semver.
|
|
368
|
+
|
|
369
|
+
Use external formatters to format files that ESLint cannot handle yet (`.css`, `.html`, etc). Powered by [`eslint-plugin-format`](https://github.com/antfu/eslint-plugin-format).
|
|
370
|
+
|
|
371
|
+
```js
|
|
372
|
+
// eslint.config.js
|
|
373
|
+
import antfu from '@antfu/eslint-config'
|
|
374
|
+
|
|
375
|
+
export default antfu({
|
|
376
|
+
formatters: {
|
|
377
|
+
/**
|
|
378
|
+
* Format CSS, LESS, SCSS files, also the `<style>` blocks in Vue
|
|
379
|
+
* By default uses Prettier
|
|
380
|
+
*/
|
|
381
|
+
css: true,
|
|
382
|
+
/**
|
|
383
|
+
* Format HTML files
|
|
384
|
+
* By default uses Prettier
|
|
385
|
+
*/
|
|
386
|
+
html: true,
|
|
387
|
+
/**
|
|
388
|
+
* Format Markdown files
|
|
389
|
+
* Supports Prettier and dprint
|
|
390
|
+
* By default uses Prettier
|
|
391
|
+
*/
|
|
392
|
+
markdown: 'prettier'
|
|
393
|
+
}
|
|
394
|
+
})
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
|
|
398
|
+
|
|
399
|
+
```bash
|
|
400
|
+
npm i -D eslint-plugin-format
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
#### React
|
|
404
|
+
|
|
405
|
+
To enable React support, you need to explicitly turn it on:
|
|
406
|
+
|
|
407
|
+
```js
|
|
408
|
+
// eslint.config.js
|
|
409
|
+
import antfu from '@antfu/eslint-config'
|
|
410
|
+
|
|
411
|
+
export default antfu({
|
|
412
|
+
react: true,
|
|
413
|
+
})
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
|
|
417
|
+
|
|
418
|
+
```bash
|
|
419
|
+
npm i -D eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-refresh
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
#### Svelte
|
|
423
|
+
|
|
424
|
+
To enable svelte support, you need to explicitly turn it on:
|
|
425
|
+
|
|
426
|
+
```js
|
|
427
|
+
// eslint.config.js
|
|
428
|
+
import antfu from '@antfu/eslint-config'
|
|
429
|
+
|
|
430
|
+
export default antfu({
|
|
431
|
+
svelte: true,
|
|
432
|
+
})
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
|
|
436
|
+
|
|
437
|
+
```bash
|
|
438
|
+
npm i -D eslint-plugin-svelte
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
#### Astro
|
|
442
|
+
|
|
443
|
+
To enable astro support, you need to explicitly turn it on:
|
|
444
|
+
|
|
445
|
+
```js
|
|
446
|
+
// eslint.config.js
|
|
447
|
+
import antfu from '@antfu/eslint-config'
|
|
448
|
+
|
|
449
|
+
export default antfu({
|
|
450
|
+
astro: true,
|
|
451
|
+
})
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
|
|
455
|
+
|
|
456
|
+
```bash
|
|
457
|
+
npm i -D eslint-plugin-astro
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
#### UnoCSS
|
|
461
|
+
|
|
462
|
+
To enable UnoCSS support, you need to explicitly turn it on:
|
|
463
|
+
|
|
464
|
+
```js
|
|
465
|
+
// eslint.config.js
|
|
466
|
+
import antfu from '@antfu/eslint-config'
|
|
467
|
+
|
|
468
|
+
export default antfu({
|
|
469
|
+
unocss: true,
|
|
470
|
+
})
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
|
|
474
|
+
|
|
475
|
+
```bash
|
|
476
|
+
npm i -D @unocss/eslint-plugin
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### Optional Rules
|
|
480
|
+
|
|
481
|
+
This config also provides some optional plugins/rules for extended usage.
|
|
482
|
+
|
|
483
|
+
#### `perfectionist` (sorting)
|
|
484
|
+
|
|
485
|
+
This plugin [`eslint-plugin-perfectionist`](https://github.com/azat-io/eslint-plugin-perfectionist) allows you to sorted object keys, imports, etc, with auto-fix.
|
|
486
|
+
|
|
487
|
+
The plugin is installed but no rules are enabled by default.
|
|
488
|
+
|
|
489
|
+
It's recommended to opt-in on each file individually using [configuration comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1).
|
|
490
|
+
|
|
491
|
+
```js
|
|
492
|
+
/* eslint perfectionist/sort-objects: "error" */
|
|
493
|
+
const objectWantedToSort = {
|
|
494
|
+
a: 2,
|
|
495
|
+
b: 1,
|
|
496
|
+
c: 3,
|
|
497
|
+
}
|
|
498
|
+
/* eslint perfectionist/sort-objects: "off" */
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
### Type Aware Rules
|
|
502
|
+
|
|
503
|
+
You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by passing the options object to the `typescript` config:
|
|
504
|
+
|
|
505
|
+
```js
|
|
506
|
+
// eslint.config.js
|
|
507
|
+
import antfu from '@antfu/eslint-config'
|
|
508
|
+
|
|
509
|
+
export default antfu({
|
|
510
|
+
typescript: {
|
|
511
|
+
tsconfigPath: 'tsconfig.json',
|
|
512
|
+
},
|
|
513
|
+
})
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
### Editor Specific Disables
|
|
517
|
+
|
|
518
|
+
Some rules are disabled when inside ESLint IDE integrations, namely [`unused-imports/no-unused-imports`](https://www.npmjs.com/package/eslint-plugin-unused-imports) [`test/no-only-tests`](https://github.com/levibuzolic/eslint-plugin-no-only-tests)
|
|
519
|
+
|
|
520
|
+
This is to prevent unused imports from getting removed by the IDE during refactoring to get a better developer experience. Those rules will be applied when you run ESLint in the terminal or [Lint Staged](#lint-staged). If you don't want this behavior, you can disable them:
|
|
521
|
+
|
|
522
|
+
```js
|
|
523
|
+
// eslint.config.js
|
|
524
|
+
import antfu from '@antfu/eslint-config'
|
|
525
|
+
|
|
526
|
+
export default antfu({
|
|
527
|
+
isInEditor: false
|
|
528
|
+
})
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
### Lint Staged
|
|
532
|
+
|
|
533
|
+
If you want to apply lint and auto-fix before every commit, you can add the following to your `package.json`:
|
|
534
|
+
|
|
535
|
+
```json
|
|
536
|
+
{
|
|
537
|
+
"simple-git-hooks": {
|
|
538
|
+
"pre-commit": "pnpm lint-staged"
|
|
539
|
+
},
|
|
540
|
+
"lint-staged": {
|
|
541
|
+
"*": "eslint --fix"
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
and then
|
|
547
|
+
|
|
548
|
+
```bash
|
|
549
|
+
npm i -D lint-staged simple-git-hooks
|
|
550
|
+
|
|
551
|
+
// to active the hooks
|
|
552
|
+
npx simple-git-hooks
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
## View what rules are enabled
|
|
556
|
+
|
|
557
|
+
I built a visual tool to help you view what rules are enabled in your project and apply them to what files, [eslint-flat-config-viewer](https://github.com/antfu/eslint-flat-config-viewer)
|
|
558
|
+
|
|
559
|
+
Go to your project root that contains `eslint.config.js` and run:
|
|
560
|
+
|
|
561
|
+
```bash
|
|
562
|
+
npx eslint-flat-config-viewer
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
## Versioning Policy
|
|
566
|
+
|
|
567
|
+
This project follows [Semantic Versioning](https://semver.org/) for releases. However, since this is just a config and involves opinions and many moving parts, we don't treat rules changes as breaking changes.
|
|
568
|
+
|
|
569
|
+
### Changes Considered as Breaking Changes
|
|
570
|
+
|
|
571
|
+
- Node.js version requirement changes
|
|
572
|
+
- Huge refactors that might break the config
|
|
573
|
+
- Plugins made major changes that might break the config
|
|
574
|
+
- Changes that might affect most of the codebases
|
|
575
|
+
|
|
576
|
+
### Changes Considered as Non-breaking Changes
|
|
577
|
+
|
|
578
|
+
- Enable/disable rules and plugins (that might become stricter)
|
|
579
|
+
- Rules options changes
|
|
580
|
+
- Version bumps of dependencies
|
|
581
|
+
|
|
582
|
+
## Badge
|
|
583
|
+
|
|
584
|
+
If you enjoy this code style, and would like to mention it in your project, here is the badge you can use:
|
|
585
|
+
|
|
586
|
+
```md
|
|
587
|
+
[](https://github.com/antfu/eslint-config)
|
|
588
|
+
```
|
|
589
|
+
|
|
590
|
+
[](https://github.com/antfu/eslint-config)
|
|
591
|
+
|
|
592
|
+
## FAQ
|
|
593
|
+
|
|
594
|
+
### Prettier?
|
|
595
|
+
|
|
596
|
+
[Why I don't use Prettier](https://antfu.me/posts/why-not-prettier)
|
|
597
|
+
|
|
598
|
+
Well, you can still use Prettier to format files that are not supported well by ESLint yet, such as `.css`, `.html`, etc. See [formatters](#formatters) for more details.
|
|
599
|
+
|
|
600
|
+
### dprint?
|
|
601
|
+
|
|
602
|
+
[dprint](https://dprint.dev/) is also a great formatter that with more abilities to customize. However, it's in the same model as Prettier which reads the AST and reprints the code from scratch. This means it's similar to Prettier, which ignores the original line breaks and might also cause the inconsistent diff. So in general, we prefer to use ESLint to format and lint JavaScript/TypeScript code.
|
|
603
|
+
|
|
604
|
+
Meanwhile, we do have dprint integrations for formatting other files such as `.md`. See [formatters](#formatters) for more details.
|
|
605
|
+
|
|
606
|
+
### How to format CSS?
|
|
607
|
+
|
|
608
|
+
You can opt-in to the [`formatters`](#formatters) feature to format your CSS. Note that it's only doing formatting, but not linting. If you want proper linting support, give [`stylelint`](https://stylelint.io/) a try.
|
|
609
|
+
|
|
610
|
+
### I prefer XXX...
|
|
611
|
+
|
|
612
|
+
Sure, you can configure and override rules locally in your project to fit your needs. If that still does not work for you, you can always fork this repo and maintain your own.
|
|
613
|
+
|
|
614
|
+
## Check Also
|
|
615
|
+
|
|
616
|
+
- [antfu/dotfiles](https://github.com/antfu/dotfiles) - My dotfiles
|
|
617
|
+
- [antfu/vscode-settings](https://github.com/antfu/vscode-settings) - My VS Code settings
|
|
618
|
+
- [antfu/starter-ts](https://github.com/antfu/starter-ts) - My starter template for TypeScript library
|
|
619
|
+
- [antfu/vitesse](https://github.com/antfu/vitesse) - My starter template for Vue & Vite app
|
|
620
|
+
|
|
621
|
+
## License
|
|
622
|
+
|
|
623
|
+
[MIT](./LICENSE) License © 2019-PRESENT [Anthony Fu](https://github.com/antfu)
|
package/bin/index.js
ADDED