@antfu/eslint-config 1.0.0-beta.2 → 1.0.0-beta.22

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
@@ -5,12 +5,13 @@
5
5
  - Single quotes, no semi
6
6
  - Auto fix for formatting (aimed to be used standalone **without** Prettier)
7
7
  - Designed to work with TypeScript, Vue out-of-box
8
- - Lint also for json, yaml, markdown
8
+ - Lints also for json, yaml, markdown
9
9
  - Sorted imports, dangling commas
10
10
  - Reasonable defaults, best practices, only one-line of config
11
+ - Respects `.gitignore` by default
11
12
  - [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new), compose easily!
12
13
  - Using [ESLint Stylistic](https://github.com/eslint-stylistic/eslint-stylistic)
13
- - **Style principle**: Minimal for reading, stable for diff
14
+ - **Style principle**: Minimal for reading, stable for diff, consistent
14
15
 
15
16
  > [!IMPORTANT]
16
17
  > The main branch is for v1.0-beta, which rewrites to ESLint Flat config, check [#250](https://github.com/antfu/eslint-config/pull/250) for more details.
@@ -20,11 +21,13 @@
20
21
  ### Install
21
22
 
22
23
  ```bash
23
- pnpm add -D eslint @antfu/eslint-config
24
+ pnpm i -D eslint @antfu/eslint-config
24
25
  ```
25
26
 
26
27
  ### Create config file
27
28
 
29
+ With [`"type": "module"`](https://nodejs.org/api/packages.html#type) in `package.json` (recommended):
30
+
28
31
  ```js
29
32
  // eslint.config.js
30
33
  import antfu from '@antfu/eslint-config'
@@ -32,7 +35,16 @@ import antfu from '@antfu/eslint-config'
32
35
  export default antfu()
33
36
  ```
34
37
 
35
- > You don't need `.eslintignore` normally as it has been provided by the preset.
38
+ With CJS:
39
+
40
+ ```js
41
+ // eslint.config.js
42
+ const antfu = require('@antfu/eslint-config').default
43
+
44
+ module.exports = antfu()
45
+ ```
46
+
47
+ > Note that `.eslintignore` no longer works in Flat config, see [customization](#customization) for more details.
36
48
 
37
49
  ### Add script for package.json
38
50
 
@@ -51,26 +63,26 @@ For example:
51
63
 
52
64
  Install [VS Code ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
53
65
 
54
- Add the following settings to your `settings.json`:
66
+ Add the following settings to your `.vscode/settings.json`:
55
67
 
56
68
  ```jsonc
57
69
  {
58
- // Enable the flat config support
70
+ // Enable the ESlint flat config support
59
71
  "eslint.experimental.useFlatConfig": true,
60
72
 
61
- // Disable the default formatter
73
+ // Disable the default formatter, use eslint instead
62
74
  "prettier.enable": false,
63
75
  "editor.formatOnSave": false,
64
76
 
65
77
  // Auto fix
66
78
  "editor.codeActionsOnSave": {
67
- "source.fixAll.eslint": true,
68
- "source.organizeImports": false
79
+ "source.fixAll": "explicit",
80
+ "source.organizeImports": "never"
69
81
  },
70
82
 
71
83
  // Silent the stylistic rules in you IDE, but still auto fix them
72
84
  "eslint.rules.customizations": [
73
- { "rule": "@stylistic/*", "severity": "off" },
85
+ { "rule": "style/*", "severity": "off" },
74
86
  { "rule": "*-indent", "severity": "off" },
75
87
  { "rule": "*-spacing", "severity": "off" },
76
88
  { "rule": "*-spaces", "severity": "off" },
@@ -81,10 +93,7 @@ Add the following settings to your `settings.json`:
81
93
  { "rule": "*semi", "severity": "off" }
82
94
  ],
83
95
 
84
- // The following is optional.
85
- // It's better to put under project setting `.vscode/settings.json`
86
- // to avoid conflicts with working with different eslint configs
87
- // that does not support all formats.
96
+ // Enable eslint for all supported languages
88
97
  "eslint.validate": [
89
98
  "javascript",
90
99
  "javascriptreact",
@@ -102,7 +111,7 @@ Add the following settings to your `settings.json`:
102
111
 
103
112
  ## Customization
104
113
 
105
- 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.
114
+ 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.
106
115
 
107
116
  Normally you only need to import the `antfu` preset:
108
117
 
@@ -113,22 +122,39 @@ import antfu from '@antfu/eslint-config'
113
122
  export default antfu()
114
123
  ```
115
124
 
116
- You can configure each feature individually, for example:
125
+ And that's it! Or you can configure each integration individually, for example:
117
126
 
118
127
  ```js
119
128
  // eslint.config.js
120
129
  import antfu from '@antfu/eslint-config'
121
130
 
122
131
  export default antfu({
123
- stylistic: true, // enable stylistic formatting rules
132
+ // Enable stylistic formatting rules
133
+ // stylistic: true,
134
+
135
+ // Or customize the stylistic rules
136
+ stylistic: {
137
+ indent: 2, // 4, or 'tab'
138
+ quotes: 'single', // or 'double'
139
+ },
140
+
141
+ // TypeScript and Vue are auto-detected, you can also explicitly enable them:
124
142
  typescript: true,
125
143
  vue: true,
144
+
145
+ // Disable jsonc and yaml support
126
146
  jsonc: false,
127
- yml: false,
147
+ yaml: false,
148
+
149
+ // `.eslintignore` is no longer supported in Flat config, use `ignores` instead
150
+ ignores: [
151
+ './fixtures',
152
+ // ...globs
153
+ ]
128
154
  })
129
155
  ```
130
156
 
131
- The `antfu` factory functions also accepts arbitrary numbers of constom configs overrides:
157
+ The `antfu` factory function also accepts any number of arbitrary custom config overrides:
132
158
 
133
159
  ```js
134
160
  // eslint.config.js
@@ -142,6 +168,7 @@ export default antfu(
142
168
  // From the second arguments they are ESLint Flat Configs
143
169
  // you can have multiple configs
144
170
  {
171
+ files: ['**/*.ts'],
145
172
  rules: {},
146
173
  },
147
174
  {
@@ -150,7 +177,7 @@ export default antfu(
150
177
  )
151
178
  ```
152
179
 
153
- Going more advanced, you can also import the very fine-grained configs and compose them as you wish:
180
+ Going more advanced, you can also import fine-grained configs and compose them as you wish:
154
181
 
155
182
  ```js
156
183
  // eslint.config.js
@@ -159,36 +186,32 @@ import {
159
186
  ignores,
160
187
  imports,
161
188
  javascript,
162
- javascriptStylistic,
163
189
  jsdoc,
164
190
  jsonc,
165
191
  markdown,
166
192
  node,
167
193
  sortPackageJson,
168
194
  sortTsconfig,
195
+ stylistic,
169
196
  typescript,
170
- typescriptStylistic,
171
197
  unicorn,
172
198
  vue,
173
- yml,
199
+ yaml,
174
200
  } from '@antfu/eslint-config'
175
201
 
176
202
  export default [
177
- ...ignores,
203
+ ...ignores(),
178
204
  ...javascript(),
179
- ...comments,
180
- ...node,
181
- ...jsdoc,
182
- ...imports,
183
- ...unicorn,
184
- ...javascriptStylistic,
185
-
205
+ ...comments(),
206
+ ...node(),
207
+ ...jsdoc(),
208
+ ...imports(),
209
+ ...unicorn(),
186
210
  ...typescript(),
187
- ...typescriptStylistic,
188
-
211
+ ...stylistic(),
189
212
  ...vue(),
190
- ...jsonc,
191
- ...yml,
213
+ ...jsonc(),
214
+ ...yaml(),
192
215
  ...markdown(),
193
216
  ]
194
217
  ```
@@ -199,17 +222,19 @@ Check out the [configs](https://github.com/antfu/eslint-config/blob/main/src/con
199
222
 
200
223
  ## Plugins Renaming
201
224
 
202
- Since flat config support explicit provides the plugin names, we renamed some plugins to make them more consistent and hide the implementation details.
225
+ Since flat config requires us to explicitly provide the plugin names (instead of mandatory convention from npm package name), we renamed some plugins to make overall scope more consistent and easier to write.
203
226
 
204
- | Original Prefix | New Prefix | Source Plugin |
205
- | --------------- | ---------- | ------------- |
206
- | `i/*` | `import/*` | [eslint-plugin-i](https://github.com/un-es/eslint-plugin-i) |
207
- | `n/*` | `node` | [eslint-plugin-n](https://github.com/eslint-community/eslint-plugin-n)
208
- | `@typescript-eslint/*` | `ts/*` | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) |
209
- | `@stylistic/js` | `style/*` | [@stylistic/eslint-plugin-js](https://github.com/eslint-stylistic/eslint-stylistic) |
210
- | `@stylistic/ts` | `style-ts/` | [@stylistic/eslint-plugin-ts](https://github.com/eslint-stylistic/eslint-stylistic) |
227
+ | New Prefix | Original Prefix | Source Plugin |
228
+ | --- | --- | --- |
229
+ | `import/*` | `i/*` | [eslint-plugin-i](https://github.com/un-es/eslint-plugin-i) |
230
+ | `node/*` | `n/*` | [eslint-plugin-n](https://github.com/eslint-community/eslint-plugin-n) |
231
+ | `yaml/*` | `yml/*` | [eslint-plugin-yml](https://github.com/ota-meshi/eslint-plugin-yml) |
232
+ | `ts/*` | `@typescript-eslint/*` | [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) |
233
+ | `style/*` | `@stylistic/*` | [@stylistic/eslint-plugin](https://github.com/eslint-stylistic/eslint-stylistic) |
234
+ | `test/*` | `vitest/*` | [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest) |
235
+ | `test/*` | `no-only-tests/*` | [eslint-plugin-no-only-tests](https://github.com/levibuzolic/eslint-plugin-no-only-tests) |
211
236
 
212
- When you want to overrides rules, or disable them inline, you need to update to the new prefix:
237
+ When you want to override rules, or disable them inline, you need to update to the new prefix:
213
238
 
214
239
  ```diff
215
240
  -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
@@ -217,6 +242,52 @@ When you want to overrides rules, or disable them inline, you need to update to
217
242
  type foo = { bar: 2 }
218
243
  ```
219
244
 
245
+ ### Rules Overrides
246
+
247
+ 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:
248
+
249
+ ```js
250
+ // eslint.config.js
251
+ import antfu from '@antfu/eslint-config'
252
+
253
+ export default antfu(
254
+ { vue: true, typescript: true },
255
+ {
256
+ // Remember to specify the file glob here, otherwise it might cause the vue plugin to handle non-vue files
257
+ files: ['**/*.vue'],
258
+ rules: {
259
+ 'vue/operator-linebreak': ['error', 'before'],
260
+ },
261
+ },
262
+ {
263
+ // Without `files`, they are general rules for all files
264
+ rules: {
265
+ 'style/semi': ['error', 'never'],
266
+ },
267
+ }
268
+ )
269
+ ```
270
+
271
+ We also provided an `overrides` options to make it easier:
272
+
273
+ ```js
274
+ // eslint.config.js
275
+ import antfu from '@antfu/eslint-config'
276
+
277
+ export default antfu({
278
+ overrides: {
279
+ vue: {
280
+ 'vue/operator-linebreak': ['error', 'before'],
281
+ },
282
+ typescript: {
283
+ 'ts/consistent-type-definitions': ['error', 'interface'],
284
+ },
285
+ yaml: {},
286
+ // ...
287
+ }
288
+ })
289
+ ```
290
+
220
291
  ### Type Aware Rules
221
292
 
222
293
  You can optionally enable the [type aware rules](https://typescript-eslint.io/linting/typed-linting/) by passing the options object to the `typescript` config: