@oliver139/eslint-config 6.0.5 → 7.0.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/README.md CHANGED
@@ -1,1020 +1,20 @@
1
- # @oliver139/eslint-config
1
+ # Oliver's ESLint Config
2
2
 
3
- [![npm](https://img.shields.io/npm/v/@oliver139/eslint-config?color=010101&style=for-the-badge)](https://npmjs.com/package/@oliver139/eslint-config)
4
-
5
- > [!NOTE]
6
- > Based on [Anthony's code style v7.7.3](https://github.com/antfu/eslint-config)
7
- > Content below keeps nearly unchanged
8
-
9
- <!-- [![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config) -->
10
-
11
- - Auto fix for formatting (aimed to be used standalone **without** Prettier)
12
- - Reasonable defaults, best practices, only one line of config
13
- - Designed to work with TypeScript, JSX, Vue, JSON, YAML, Toml, Markdown, etc. Out-of-box.
14
- - Opinionated, but [very customizable](#customization)
15
- - [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
16
- - Optional [React](#react), [Next.js](#nextjs), [Svelte](#svelte), [UnoCSS](#unocss), [Astro](#astro), [Solid](#solid) support
17
- - Optional [formatters](#formatters) support for formatting CSS, HTML, XML, etc.
18
- - **Style principle**: Minimal for reading, stable for diff, consistent
19
- - Sorted imports, dangling commas
20
- - Single quotes, no semi
21
- - Using [ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)
22
- - Respects `.gitignore` by default
23
- - Requires ESLint v9.5.0+
24
-
25
- > [!WARNING]
26
- > I am super appreciative and even a bit flattered that so many of you are fond of using this config. For that reason, I tried to make it as flexible and customizable as possible to fit more use cases.
27
- >
28
- > However, please keep in mind that this is still **_a personal config_** with a lot of opinions. Changes might not always work for everyone and every use case.
29
- >
30
- > If you are using this config directly, I suggest you **review the changes every time you update**. Or if you want more control over the rules, always feel free to fork it. Thanks!
31
-
32
- > [!TIP]
33
- > If you are interested in the tooling and the philosophy behind this config, I gave a talk about ESLint flat config at [JSNation 2024 - ESLint One for All Made Easy](https://gitnation.com/contents/eslint-one-for-all-made-easy), slides are [here](https://talks.antfu.me/2024/jsnation).
34
-
35
- ## Usage
36
-
37
- ### Starter Wizard
38
-
39
- 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.
3
+ My options of [@antfu/eslint-config](https://github.com/antfu/eslint-config).
40
4
 
41
- ```bash
42
- pnpm dlx @oliver139/eslint-config@latest
43
- ```
44
-
45
- ### Manual Install
5
+ [![npm](https://img.shields.io/npm/v/@oliver139/eslint-config?color=010101&style=for-the-badge)](https://npmjs.com/package/@oliver139/eslint-config)
46
6
 
47
- If you prefer to set up manually:
7
+ ## Install
48
8
 
49
9
  ```bash
50
- pnpm i -D eslint @oliver139/eslint-config
51
- ```
52
-
53
- And create `eslint.config.mjs` in your project root:
54
-
55
- ```js
56
- // eslint.config.mjs
57
- import eslintConfig from '@oliver139/eslint-config'
58
-
59
- export default eslintConfig()
60
- ```
61
-
62
- <details>
63
- <summary>
64
- Combined with legacy config:
65
- </summary>
66
-
67
- 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.
68
-
69
- ```js
70
- // eslint.config.mjs
71
- import { FlatCompat } from '@eslint/eslintrc'
72
- import eslintConfig from '@oliver139/eslint-config'
73
-
74
- const compat = new FlatCompat()
75
-
76
- export default eslintConfig(
77
- {
78
- ignores: [],
79
- },
80
-
81
- // Legacy config
82
- ...compat.config({
83
- extends: [
84
- 'eslint:recommended',
85
- // Other extends...
86
- ],
87
- })
88
-
89
- // Other flat configs...
90
- )
91
- ```
92
-
93
- > Note that `.eslintignore` no longer works in Flat config, see [customization](#customization) for more details.
94
-
95
- </details>
96
-
97
- ### Add script for package.json
98
-
99
- For example:
100
-
101
- ```json
102
- {
103
- "scripts": {
104
- "lint": "eslint",
105
- "lint:fix": "eslint --fix"
106
- }
107
- }
108
- ```
109
-
110
- ## IDE Support (auto fix on save)
111
-
112
- <details>
113
- <summary>🟦 VS Code support</summary>
114
-
115
- <br>
116
-
117
- Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
118
-
119
- Add the following settings to your `.vscode/settings.json`:
120
-
121
- ```jsonc
122
- {
123
- // Disable the default formatter, use eslint instead
124
- "prettier.enable": false,
125
- "editor.formatOnSave": false,
126
-
127
- // Auto fix
128
- "editor.codeActionsOnSave": {
129
- "source.fixAll.eslint": "explicit",
130
- "source.organizeImports": "never"
131
- },
132
-
133
- // Silent the stylistic rules in your IDE, but still auto fix them
134
- "eslint.rules.customizations": [
135
- { "rule": "style/*", "severity": "off", "fixable": true },
136
- { "rule": "format/*", "severity": "off", "fixable": true },
137
- { "rule": "*-indent", "severity": "off", "fixable": true },
138
- { "rule": "*-spacing", "severity": "off", "fixable": true },
139
- { "rule": "*-spaces", "severity": "off", "fixable": true },
140
- { "rule": "*-order", "severity": "off", "fixable": true },
141
- { "rule": "*-dangle", "severity": "off", "fixable": true },
142
- { "rule": "*-newline", "severity": "off", "fixable": true },
143
- { "rule": "*quotes", "severity": "off", "fixable": true },
144
- { "rule": "*semi", "severity": "off", "fixable": true }
145
- ],
146
-
147
- // Enable eslint for all supported languages
148
- "eslint.validate": [
149
- "javascript",
150
- "javascriptreact",
151
- "typescript",
152
- "typescriptreact",
153
- "vue",
154
- "html",
155
- "markdown",
156
- "json",
157
- "jsonc",
158
- "yaml",
159
- "toml",
160
- "xml",
161
- "gql",
162
- "graphql",
163
- "astro",
164
- "svelte",
165
- "css",
166
- "less",
167
- "scss",
168
- "pcss",
169
- "postcss"
170
- ]
171
- }
172
- ```
173
-
174
- </details>
175
-
176
- <details>
177
- <summary>🔲 Zed support</summary>
178
-
179
- <br>
180
-
181
- Add the following settings to your `.zed/settings.json`:
182
-
183
- ```jsonc
184
- {
185
- // Use ESLint's --fix:
186
- "code_actions_on_format": {
187
- "source.fixAll.eslint": true
188
- },
189
- "formatter": [],
190
- // Enable eslint for all supported languages
191
- // Defaults only include https://github.com/search?q=repo%3Azed-industries%2Fzed%20eslint_languages&type=code
192
- "languages": {
193
- "HTML": {
194
- "language_servers": ["...", "eslint"]
195
- },
196
- "Markdown": {
197
- "language_servers": ["...", "eslint"]
198
- },
199
- "Markdown-Inline": {
200
- "language_servers": ["...", "eslint"]
201
- },
202
- "JSON": {
203
- "language_servers": ["...", "eslint"]
204
- },
205
- "JSONC": {
206
- "language_servers": ["...", "eslint"]
207
- },
208
- "YAML": {
209
- "language_servers": ["...", "eslint"]
210
- },
211
- "CSS": {
212
- "language_servers": ["...", "eslint"]
213
- }
214
- // Add other languages as needed
215
- },
216
- "lsp": {
217
- "eslint": {
218
- "settings": {
219
- "workingDirectories": ["./"],
220
-
221
- // Silent the stylistic rules in your IDE, but still auto fix them
222
- "rulesCustomizations": [
223
- { "rule": "style/*", "severity": "off", "fixable": true },
224
- { "rule": "format/*", "severity": "off", "fixable": true },
225
- { "rule": "*-indent", "severity": "off", "fixable": true },
226
- { "rule": "*-spacing", "severity": "off", "fixable": true },
227
- { "rule": "*-spaces", "severity": "off", "fixable": true },
228
- { "rule": "*-order", "severity": "off", "fixable": true },
229
- { "rule": "*-dangle", "severity": "off", "fixable": true },
230
- { "rule": "*-newline", "severity": "off", "fixable": true },
231
- { "rule": "*quotes", "severity": "off", "fixable": true },
232
- { "rule": "*semi", "severity": "off", "fixable": true }
233
- ]
234
- }
235
- }
236
- }
237
- }
238
- ```
239
-
240
- </details>
241
-
242
- <details>
243
- <summary>🟩 Neovim Support</summary>
244
-
245
- <br>
246
-
247
- Update your configuration to use the following:
248
-
249
- ```lua
250
- local customizations = {
251
- { rule = 'style/*', severity = 'off', fixable = true },
252
- { rule = 'format/*', severity = 'off', fixable = true },
253
- { rule = '*-indent', severity = 'off', fixable = true },
254
- { rule = '*-spacing', severity = 'off', fixable = true },
255
- { rule = '*-spaces', severity = 'off', fixable = true },
256
- { rule = '*-order', severity = 'off', fixable = true },
257
- { rule = '*-dangle', severity = 'off', fixable = true },
258
- { rule = '*-newline', severity = 'off', fixable = true },
259
- { rule = '*quotes', severity = 'off', fixable = true },
260
- { rule = '*semi', severity = 'off', fixable = true },
261
- }
262
-
263
- local lspconfig = require('lspconfig')
264
- -- Enable eslint for all supported languages
265
- lspconfig.eslint.setup(
266
- {
267
- filetypes = {
268
- "javascript",
269
- "javascriptreact",
270
- "javascript.jsx",
271
- "typescript",
272
- "typescriptreact",
273
- "typescript.tsx",
274
- "vue",
275
- "html",
276
- "markdown",
277
- "json",
278
- "jsonc",
279
- "yaml",
280
- "toml",
281
- "xml",
282
- "gql",
283
- "graphql",
284
- "astro",
285
- "svelte",
286
- "css",
287
- "less",
288
- "scss",
289
- "pcss",
290
- "postcss"
291
- },
292
- settings = {
293
- -- Silent the stylistic rules in your IDE, but still auto fix them
294
- rulesCustomizations = customizations,
295
- },
296
- }
297
- )
298
- ```
299
-
300
- ### Neovim format on save
301
-
302
- There's few ways you can achieve format on save in neovim:
303
-
304
- - `nvim-lspconfig` has a `EslintFixAll` command predefined, you can create a autocmd to call this command after saving file.
305
-
306
- ```lua
307
- lspconfig.eslint.setup({
308
- --- ...
309
- on_attach = function(client, bufnr)
310
- vim.api.nvim_create_autocmd("BufWritePre", {
311
- buffer = bufnr,
312
- command = "EslintFixAll",
313
- })
314
- end,
315
- })
10
+ npm install -D eslint @oliver139/eslint-config@7 @antfu/eslint-config
316
11
  ```
317
12
 
318
- - Use [conform.nvim](https://github.com/stevearc/conform.nvim).
319
- - Use [none-ls](https://github.com/nvimtools/none-ls.nvim)
320
- - Use [nvim-lint](https://github.com/mfussenegger/nvim-lint)
321
-
322
- </details>
323
-
324
- ## Customization
325
-
326
- 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.
327
-
328
- Normally you only need to import the `antfu` preset:
329
-
330
- ```js
331
- // eslint.config.js
332
- import eslintConfig from '@oliver139/eslint-config'
333
-
334
- export default eslintConfig()
335
- ```
336
-
337
- And that's it! Or you can configure each integration individually, for example:
338
-
339
- ```js
340
- // eslint.config.js
341
- import eslintConfig from '@oliver139/eslint-config'
342
-
343
- export default eslintConfig({
344
- // Type of the project. 'lib' for libraries, the default is 'app'
345
- type: 'lib',
346
-
347
- // `.eslintignore` is no longer supported in Flat config, use `ignores` instead
348
- // The `ignores` option in the option (first argument) is specifically treated to always be global ignores
349
- // And will **extend** the config's default ignores, not override them
350
- // You can also pass a function to modify the default ignores
351
- ignores: [
352
- '**/fixtures',
353
- // ...globs
354
- ],
355
-
356
- // Parse the `.gitignore` file to get the ignores, on by default
357
- gitignore: true,
358
-
359
- // Enable stylistic formatting rules
360
- // stylistic: true,
361
-
362
- // Or customize the stylistic rules
363
- stylistic: {
364
- indent: 2, // 4, or 'tab'
365
- quotes: 'single', // or 'double'
366
- },
367
-
368
- // TypeScript and Vue are autodetected, you can also explicitly enable them:
369
- typescript: true,
370
- vue: true,
371
-
372
- // Disable jsonc and yaml support
373
- jsonc: false,
374
- yaml: false,
375
- })
376
- ```
377
-
378
- The `antfu` factory function also accepts any number of arbitrary custom config overrides:
379
-
380
- ```js
381
- // eslint.config.js
382
- import eslintConfig from '@oliver139/eslint-config'
383
-
384
- export default eslintConfig(
385
- {
386
- // Configures for antfu's config
387
- },
388
-
389
- // From the second arguments they are ESLint Flat Configs
390
- // you can have multiple configs
391
- {
392
- files: ['**/*.ts'],
393
- rules: {},
394
- },
395
- {
396
- rules: {},
397
- },
398
- )
399
- ```
400
-
401
- Going more advanced, you can also import fine-grained configs and compose them as you wish:
402
-
403
- <details>
404
- <summary>Advanced Example</summary>
405
-
406
- 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.
407
-
408
- ```js
409
- // eslint.config.js
410
- import {
411
- combine,
412
- comments,
413
- ignores,
414
- imports,
415
- javascript,
416
- jsdoc,
417
- jsonc,
418
- markdown,
419
- node,
420
- sortPackageJson,
421
- sortTsconfig,
422
- stylistic,
423
- toml,
424
- typescript,
425
- unicorn,
426
- vue,
427
- yaml,
428
- } from '@oliver139/eslint-config'
429
-
430
- export default combine(
431
- ignores(),
432
- javascript(/* Options */),
433
- comments(),
434
- node(),
435
- jsdoc(),
436
- imports(),
437
- unicorn(),
438
- typescript(/* Options */),
439
- stylistic(),
440
- vue(),
441
- jsonc(),
442
- yaml(),
443
- toml(),
444
- markdown(),
445
- )
446
- ```
447
-
448
- </details>
449
-
450
- 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.
451
-
452
- > Thanks to [sxzz/eslint-config](https://github.com/sxzz/eslint-config) for the inspiration and reference.
453
-
454
- ### Plugins Renaming
455
-
456
- 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.
457
-
458
- | New Prefix | Original Prefix | Source Plugin |
459
- | ---------- | ---------------------- | ----------------------------------------------------------------------------------------------------- |
460
- | `import/*` | `import-lite/*` | [eslint-plugin-import-lite](https://github.com/9romise/eslint-plugin-import-lite) |
461
- | `node/*` | `n/*` | [eslint-plugin-n](https://github.com/eslint-community/eslint-plugin-n) |
462
- | `yaml/*` | `yml/*` | [eslint-plugin-yml](https://github.com/ota-meshi/eslint-plugin-yml) |
463
- | `ts/*` | `@typescript-eslint/*` | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) |
464
- | `style/*` | `@stylistic/*` | [@stylistic/eslint-plugin](https://github.com/eslint-stylistic/eslint-stylistic) |
465
- | `test/*` | `vitest/*` | [@vitest/eslint-plugin](https://github.com/vitest-dev/eslint-plugin-vitest) |
466
- | `test/*` | `no-only-tests/*` | [eslint-plugin-no-only-tests](https://github.com/levibuzolic/eslint-plugin-no-only-tests) |
467
- | `next/*` | `@next/next` | [@next/eslint-plugin-next](https://github.com/vercel/next.js/tree/canary/packages/eslint-plugin-next) |
468
-
469
- When you want to override rules, or disable them inline, you need to update to the new prefix:
470
-
471
- ```diff
472
- -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
473
- +// eslint-disable-next-line ts/consistent-type-definitions
474
- type foo = { bar: 2 }
475
- ```
476
-
477
- > [!NOTE]
478
- > About plugin renaming - it is actually rather a dangerous move that might lead 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.
479
- >
480
- > 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.
481
- >
482
- > That said, it's probably still not a good idea. You might not want to do this if you are maintaining your own eslint config.
483
- >
484
- > 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.
485
-
486
- 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.
487
-
488
- <details>
489
- <summary>Change back to original prefix</summary>
490
-
491
- If you really want to use the original prefix, you can revert the plugin renaming by:
13
+ ## Usage
492
14
 
493
15
  ```ts
494
- import eslintConfig from '@oliver139/eslint-config'
495
-
496
- export default eslintConfig()
497
- .renamePlugins({
498
- ts: '@typescript-eslint',
499
- yaml: 'yml',
500
- node: 'n'
501
- // ...
502
- })
503
- ```
504
-
505
- </details>
506
-
507
- ### Rules Overrides
508
-
509
- 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:
510
-
511
- ```js
512
- // eslint.config.js
513
- import eslintConfig from '@oliver139/eslint-config'
514
-
515
- export default eslintConfig(
516
- {
517
- vue: true,
518
- typescript: true
519
- },
520
- {
521
- // Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
522
- files: ['**/*.vue'],
523
- rules: {
524
- 'vue/operator-linebreak': ['error', 'before'],
525
- },
526
- },
527
- {
528
- // Without `files`, they are general rules for all files
529
- rules: {
530
- 'style/semi': ['error', 'never'],
531
- },
532
- }
533
- )
534
- ```
535
-
536
- We also provided the `overrides` options in each integration to make it easier:
537
-
538
- ```js
539
- // eslint.config.js
540
- import eslintConfig from '@oliver139/eslint-config'
541
-
542
- export default eslintConfig({
543
- vue: {
544
- overrides: {
545
- 'vue/operator-linebreak': ['error', 'before'],
546
- },
547
- },
548
- typescript: {
549
- overrides: {
550
- 'ts/consistent-type-definitions': ['error', 'interface'],
551
- },
552
- },
553
- yaml: {
554
- overrides: {
555
- // ...
556
- },
557
- },
558
- })
559
- ```
560
-
561
- ### Config Composer
562
-
563
- Since v2.10.0, the factory function `antfu()` returns a [`FlatConfigComposer` object from `eslint-flat-config-utils`](https://github.com/antfu/eslint-flat-config-utils#composer) where you can chain the methods to compose the config even more flexibly.
564
-
565
- ```js
566
- // eslint.config.js
567
- import eslintConfig from '@oliver139/eslint-config'
568
-
569
- export default eslintConfig()
570
- .prepend(
571
- // some configs before the main config
572
- )
573
- // overrides any named configs
574
- .override(
575
- 'antfu/stylistic/rules',
576
- {
577
- rules: {
578
- 'style/generator-star-spacing': ['error', { after: true, before: false }],
579
- }
580
- }
581
- )
582
- // rename plugin prefixes
583
- .renamePlugins({
584
- 'old-prefix': 'new-prefix',
585
- // ...
586
- })
587
- // ...
588
- ```
589
-
590
- ### Vue
591
-
592
- Vue support is detected automatically by checking if `vue` is installed in your project. You can also explicitly enable/disable it:
593
-
594
- ```js
595
- // eslint.config.js
596
- import eslintConfig from '@oliver139/eslint-config'
597
-
598
- export default eslintConfig({
599
- vue: true
600
- })
601
- ```
602
-
603
- #### Vue 2
604
-
605
- We have limited support for Vue 2 (as it's already [reached EOL](https://v2.vuejs.org/eol/)). If you are still using Vue 2, you can configure it manually by setting `vueVersion` to `2`:
606
-
607
- ```js
608
- // eslint.config.js
609
- import eslintConfig from '@oliver139/eslint-config'
610
-
611
- export default eslintConfig({
612
- vue: {
613
- vueVersion: 2
614
- },
615
- })
616
- ```
617
-
618
- #### Vue Accessibility
619
-
620
- To enable Vue accessibility support, you need to explicitly turn it on:
621
-
622
- ```js
623
- // eslint.config.js
624
- import eslintConfig from '@oliver139/eslint-config'
625
-
626
- export default eslintConfig({
627
- vue: {
628
- a11y: true
629
- },
630
- })
631
- ```
632
-
633
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
634
-
635
- ```bash
636
- npm i -D eslint-plugin-vuejs-accessibility
637
- ```
638
-
639
- #### Vue Accessibility
640
-
641
- To enable Vue accessibility support, you need to explicitly turn it on:
642
-
643
- ```js
644
- // eslint.config.js
645
16
  import antfu from '@antfu/eslint-config'
17
+ import { options } from '@oliver139/eslint-config'
646
18
 
647
- export default antfu({
648
- vue: {
649
- a11y: true
650
- },
651
- })
19
+ export default antfu(...options())
652
20
  ```
653
-
654
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
655
-
656
- ```bash
657
- npm i -D eslint-plugin-vuejs-accessibility
658
- ```
659
-
660
- ### Optional Configs
661
-
662
- We provide some optional configs for specific use cases, that we don't include their dependencies by default.
663
-
664
- #### Formatters
665
-
666
- 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).
667
-
668
- ```js
669
- // eslint.config.js
670
- import eslintConfig from '@oliver139/eslint-config'
671
-
672
- export default eslintConfig({
673
- formatters: {
674
- /**
675
- * Format CSS, LESS, SCSS files, also the `<style>` blocks in Vue
676
- * By default uses Prettier
677
- */
678
- css: true,
679
- /**
680
- * Format HTML files
681
- * By default uses Prettier
682
- */
683
- html: true,
684
- /**
685
- * Format Markdown files
686
- * Supports Prettier and dprint
687
- * By default uses Prettier
688
- */
689
- markdown: 'prettier'
690
- }
691
- })
692
- ```
693
-
694
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
695
-
696
- ```bash
697
- npm i -D eslint-plugin-format
698
- ```
699
-
700
- #### React
701
-
702
- To enable React support, you need to explicitly turn it on:
703
-
704
- ```js
705
- // eslint.config.js
706
- import eslintConfig from '@oliver139/eslint-config'
707
-
708
- export default eslintConfig({
709
- react: true,
710
- })
711
- ```
712
-
713
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
714
-
715
- ```bash
716
- npm i -D @eslint-react/eslint-plugin eslint-plugin-react-refresh
717
- ```
718
-
719
- #### Next.js
720
-
721
- To enable Next.js support, you need to explicitly turn it on:
722
-
723
- ```js
724
- // eslint.config.js
725
- import antfu from '@antfu/eslint-config'
726
-
727
- export default antfu({
728
- nextjs: true,
729
- })
730
- ```
731
-
732
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
733
-
734
- ```bash
735
- npm i -D @next/eslint-plugin-next
736
- ```
737
-
738
- #### Svelte
739
-
740
- To enable svelte support, you need to explicitly turn it on:
741
-
742
- ```js
743
- // eslint.config.js
744
- import eslintConfig from '@oliver139/eslint-config'
745
-
746
- export default eslintConfig({
747
- svelte: true,
748
- })
749
- ```
750
-
751
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
752
-
753
- ```bash
754
- npm i -D eslint-plugin-svelte
755
- ```
756
-
757
- #### Astro
758
-
759
- To enable astro support, you need to explicitly turn it on:
760
-
761
- ```js
762
- // eslint.config.js
763
- import eslintConfig from '@oliver139/eslint-config'
764
-
765
- export default eslintConfig({
766
- astro: true,
767
- })
768
- ```
769
-
770
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
771
-
772
- ```bash
773
- npm i -D eslint-plugin-astro
774
- ```
775
-
776
- #### Solid
777
-
778
- To enable Solid support, you need to explicitly turn it on:
779
-
780
- ```js
781
- // eslint.config.js
782
- import eslintConfig from '@oliver139/eslint-config'
783
-
784
- export default eslintConfig({
785
- solid: true,
786
- })
787
- ```
788
-
789
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
790
-
791
- ```bash
792
- npm i -D eslint-plugin-solid
793
- ```
794
-
795
- #### UnoCSS
796
-
797
- To enable UnoCSS support, you need to explicitly turn it on:
798
-
799
- ```js
800
- // eslint.config.js
801
- import eslintConfig from '@oliver139/eslint-config'
802
-
803
- export default eslintConfig({
804
- unocss: true,
805
- })
806
- ```
807
-
808
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
809
-
810
- ```bash
811
- npm i -D @unocss/eslint-plugin
812
- ```
813
-
814
- #### Angular
815
-
816
- To enable Angular support, you need to explicitly turn it on:
817
-
818
- ```js
819
- // eslint.config.js
820
- import antfu from '@antfu/eslint-config'
821
-
822
- export default antfu({
823
- angular: true,
824
- })
825
- ```
826
-
827
- Running `npx eslint` should prompt you to install the required dependencies, otherwise, you can install them manually:
828
-
829
- ```bash
830
- npm i -D @angular-eslint/eslint-plugin @angular-eslint/eslint-plugin-template @angular-eslint/template-parser
831
- ```
832
-
833
- ### Optional Rules
834
-
835
- This config also provides some optional plugins/rules for extended usage.
836
-
837
- #### `command`
838
-
839
- Powered by [`eslint-plugin-command`](https://github.com/antfu/eslint-plugin-command). It is not a typical rule for linting, but an on-demand micro-codemod tool that triggers by specific comments.
840
-
841
- For a few triggers, for example:
842
-
843
- - `/// to-function` - converts an arrow function to a normal function
844
- - `/// to-arrow` - converts a normal function to an arrow function
845
- - `/// to-for-each` - converts a for-in/for-of loop to `.forEach()`
846
- - `/// to-for-of` - converts a `.forEach()` to a for-of loop
847
- - `/// keep-sorted` - sorts an object/array/interface
848
- - ... etc. - refer to the [documentation](https://github.com/antfu/eslint-plugin-command#built-in-commands)
849
-
850
- You can add the trigger comment one line above the code you want to transform, for example (note the triple slash):
851
-
852
- <!-- eslint-skip -->
853
-
854
- ```ts
855
- /// to-function
856
- const foo = async (msg: string): void => {
857
- console.log(msg)
858
- }
859
- ```
860
-
861
- Will be transformed to this when you hit save with your editor or run `eslint --fix`:
862
-
863
- ```ts
864
- async function foo(msg: string): void {
865
- console.log(msg)
866
- }
867
- ```
868
-
869
- The command comments are usually one-off and will be removed along with the transformation.
870
-
871
- ### Type Aware Rules
872
-
873
- You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by passing the options object to the `typescript` config:
874
-
875
- ```js
876
- // eslint.config.js
877
- import eslintConfig from '@oliver139/eslint-config'
878
-
879
- export default eslintConfig({
880
- typescript: {
881
- tsconfigPath: 'tsconfig.json',
882
- },
883
- })
884
- ```
885
-
886
- ### Editor Specific Disables
887
-
888
- Auto-fixing for the following rules are disabled when ESLint is running in a code editor:
889
-
890
- - [`prefer-const`](https://eslint.org/docs/rules/prefer-const)
891
- - [`test/no-only-tests`](https://github.com/levibuzolic/eslint-plugin-no-only-tests)
892
- - [`unused-imports/no-unused-imports`](https://www.npmjs.com/package/eslint-plugin-unused-imports)
893
- - [`pnpm/json-enforce-catalog`](https://github.com/antfu/pnpm-workspace-utils/tree/main/packages/eslint-plugin-pnpm#rules)
894
- - [`pnpm/json-prefer-workspace-settings`](https://github.com/antfu/pnpm-workspace-utils/tree/main/packages/eslint-plugin-pnpm#rules)
895
- - [`pnpm/json-valid-catalog`](https://github.com/antfu/pnpm-workspace-utils/tree/main/packages/eslint-plugin-pnpm#rules)
896
-
897
- > Since v3.16.0, they are no longer disabled, but made non-fixable using [this helper](https://github.com/antfu/eslint-flat-config-utils#composerdisablerulesfix).
898
-
899
- This is to prevent unused imports from getting removed by the editor 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:
900
-
901
- ```js
902
- // eslint.config.js
903
- import eslintConfig from '@oliver139/eslint-config'
904
-
905
- export default eslintConfig({
906
- isInEditor: false
907
- })
908
- ```
909
-
910
- ### Lint Staged
911
-
912
- If you want to apply lint and auto-fix before every commit, you can add the following to your `package.json`:
913
-
914
- ```json
915
- {
916
- "simple-git-hooks": {
917
- "pre-commit": "pnpm lint-staged"
918
- },
919
- "lint-staged": {
920
- "*": "eslint --fix"
921
- }
922
- }
923
- ```
924
-
925
- and then
926
-
927
- ```bash
928
- npm i -D lint-staged simple-git-hooks
929
-
930
- // to active the hooks
931
- npx simple-git-hooks
932
- ```
933
-
934
- ## View what rules are enabled
935
-
936
- I built a visual tool to help you view what rules are enabled in your project and apply them to what files, [@eslint/config-inspector](https://github.com/eslint/config-inspector)
937
-
938
- Go to your project root that contains `eslint.config.js` and run:
939
-
940
- ```bash
941
- npx @eslint/config-inspector
942
- ```
943
-
944
- ## Versioning Policy
945
-
946
- 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.
947
-
948
- ### Changes Considered as Breaking Changes
949
-
950
- - Node.js version requirement changes
951
- - Huge refactors that might break the config
952
- - Plugins made major changes that might break the config
953
- - Changes that might affect most of the codebases
954
-
955
- ### Changes Considered as Non-breaking Changes
956
-
957
- - Enable/disable rules and plugins (that might become stricter)
958
- - Rules options changes
959
- - Version bumps of dependencies
960
-
961
- ## Badge
962
-
963
- If you enjoy this code style, and would like to mention it in your project, here is the badge you can use:
964
-
965
- ```md
966
- [![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config)
967
- ```
968
-
969
- [![code style](https://antfu.me/badge-code-style.svg)](https://github.com/antfu/eslint-config)
970
-
971
- ## FAQ
972
-
973
- ### Prettier?
974
-
975
- [Why I don't use Prettier](https://antfu.me/posts/why-not-prettier)
976
-
977
- 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.
978
-
979
- ### oxlint?
980
-
981
- We do have a plan to integrate [oxlint](https://github.com/oxc-project/oxc) in someway to speed up the linting process. However there are still some blocks we are waiting for. Track the progress [in this issue: **Oxlint Integration Plan**](https://github.com/antfu/eslint-config/issues/767).
982
-
983
- ### dprint?
984
-
985
- [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.
986
-
987
- Meanwhile, we do have dprint integrations for formatting other files such as `.md`. See [formatters](#formatters) for more details.
988
-
989
- ### How to format CSS?
990
-
991
- 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.
992
-
993
- ### Top-level Function Style, etc.
994
-
995
- I am a very opinionated person, so as this config. I prefer the top-level functions always using the function declaration over arrow functions; I prefer one-line if statements without braces and always wraps, and so on. I even wrote some custom rules to enforce them.
996
-
997
- I know they are not necessarily the popular opinions. If you really want to get rid of them, you can disable them with:
998
-
999
- ```ts
1000
- import eslintConfig from '@oliver139/eslint-config'
1001
-
1002
- export default eslintConfig({
1003
- lessOpinionated: true
1004
- })
1005
- ```
1006
-
1007
- ### I prefer XXX...
1008
-
1009
- 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.
1010
-
1011
- ## Check Also
1012
-
1013
- - [antfu/dotfiles](https://github.com/antfu/dotfiles) - My dotfiles
1014
- - [antfu/vscode-settings](https://github.com/antfu/vscode-settings) - My VS Code settings
1015
- - [antfu/starter-ts](https://github.com/antfu/starter-ts) - My starter template for TypeScript library
1016
- - [antfu/vitesse](https://github.com/antfu/vitesse) - My starter template for Vue & Vite app
1017
-
1018
- ## License
1019
-
1020
- [MIT](./LICENSE) License © 2019-PRESENT [Anthony Fu](https://github.com/antfu). Fork maintained by [Oliver Mak](https://github.com/oliver139).