@lincy/eslint-config 3.3.0 → 3.4.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,11 +1,11 @@
1
- # @lincy/eslint-config
1
+ # @lincy/eslint-config Eslint扁平化规则
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/@lincy/eslint-config?color=444&label=)](https://npmjs.com/package/@lincy/eslint-config)
4
4
 
5
5
  - 单引号,无结尾分号
6
6
  - 自动格式化
7
7
  - 专为与 TypeScript、Vue(2/3) 一起使用而设计,开箱即用
8
- - Lint 也适用于 json、yaml、markdown
8
+ - 也适用于 json、yaml、markdown、react
9
9
  - import导入排序, 对象字⾯量项尾逗号
10
10
  - 合理的默认值,最佳实践,只需一行配置
11
11
  - [ESLint Flat config](https://eslint.org/docs/latest/use/configure/configuration-files-new)
@@ -52,20 +52,20 @@ For example:
52
52
 
53
53
  ```jsonc
54
54
  {
55
- // Enable the flat config support
55
+ // 开启eslint扁平化配置
56
56
  "eslint.experimental.useFlatConfig": true,
57
57
 
58
- // Disable the default formatter
58
+ // 禁用默认的格式化
59
59
  "prettier.enable": false,
60
60
  "editor.formatOnSave": false,
61
61
 
62
- // Auto fix
62
+ // 开启自动修复
63
63
  "editor.codeActionsOnSave": {
64
64
  "source.fixAll.eslint": true,
65
65
  "source.organizeImports": false
66
66
  },
67
67
 
68
- // Silent the stylistic rules in you IDE, but still auto fix them
68
+ // IDE 中静默样式规则,但仍会自动修复它们
69
69
  "eslint.rules.customizations": [
70
70
  { "rule": "@stylistic/*", "severity": "off" },
71
71
  { "rule": "*-indent", "severity": "off" },
@@ -78,10 +78,7 @@ For example:
78
78
  { "rule": "*semi", "severity": "off" }
79
79
  ],
80
80
 
81
- // The following is optional.
82
- // It's better to put under project setting `.vscode/settings.json`
83
- // to avoid conflicts with working with different eslint configs
84
- // that does not support all formats.
81
+ // 为所有支持的语言启用 eslint
85
82
  "eslint.validate": [
86
83
  "javascript",
87
84
  "javascriptreact",
@@ -102,6 +99,8 @@ For example:
102
99
  通常你只需要导入 `lincy` 预设:
103
100
 
104
101
  #### esm
102
+ 如果 package.json 中开启了`"type": "module",`
103
+
105
104
  ```js
106
105
  // eslint.config.js
107
106
  import lincy from '@lincy/eslint-config'
@@ -113,6 +112,8 @@ export default lincy()
113
112
  ```
114
113
 
115
114
  #### cjs
115
+ 如果 package.json 中没有开启`"type": "module",`
116
+
116
117
  ```js
117
118
  // eslint.config.js
118
119
  const lincy = require('@lincy/eslint-config').lincy
@@ -143,6 +144,18 @@ export default lincy({
143
144
  test: false, // 默认值: true
144
145
  // 是否启用 markdown 规则
145
146
  markdown: false, // 默认值: true
147
+ // 覆盖规则
148
+ overrides: {},
149
+
150
+ // 工厂函数第一个参数默认为各规则的开关, 但是也可以作为追加规则使用, 当包含以下键名将会自动整合到一个规则里
151
+ files: [],
152
+ ignores: [],
153
+ languageOptions: {},
154
+ linterOptions: {},
155
+ processor: {},
156
+ plugins: {},
157
+ rules: [],
158
+ settings: {}
146
159
  })
147
160
  ```
148
161
 
@@ -160,7 +173,7 @@ const autoImport = JSON.parse(
160
173
 
161
174
  export default lincy(
162
175
  {
163
- // Configures for config
176
+ // 配置, 参考上一个代码块
164
177
  },
165
178
  // 启用 unocss
166
179
  {
@@ -181,13 +194,6 @@ export default lincy(
181
194
  },
182
195
  },
183
196
  },
184
- // 自定义排除文件(夹)
185
- {
186
- ignores: [
187
- '**/assets',
188
- '**/static',
189
- ],
190
- },
191
197
  // 你还可以继续配置多个 ESLint Flat Configs
192
198
  {
193
199
  files: ['**/*.ts'],
@@ -237,13 +243,13 @@ export default [
237
243
  ]
238
244
  ```
239
245
 
240
- 查看 [configs](https://github.com/lincenying/eslint-config/blob/main/src/configs) 和 [factory](https://github.com/lincenying/eslint-config/blob/ main/src/factory.ts)了解更多详细信息。
246
+ 查看 [configs](https://github.com/lincenying/eslint-config/blob/main/src/configs) 和 [factory](https://github.com/lincenying/eslint-config/blob/main/src/factory.ts)了解更多详细信息。
241
247
 
242
- > Thanks to [sxzz/eslint-config](https://github.com/sxzz/eslint-config) for the inspiration and reference.
248
+ > Thanks to [sxzz/eslint-config](https://github.com/sxzz/eslint-config) and [antfu/eslint-config](https://github.com/antfu/eslint-config) for the inspiration and reference.
243
249
 
244
250
  ## Plugins Renaming
245
251
 
246
- 由于平面配置支持显式提供了插件名称,因此我们重命名了一些插件以使它们更加一致并隐藏实现细节。
252
+ 由于扁平化配置支持显式提供了插件名称,因此我们重命名了一些插件以使它们更加一致并隐藏实现细节。
247
253
 
248
254
  | New Prefix | Original Prefix | Source Plugin |
249
255
  | --- | --- | --- |
@@ -267,10 +273,21 @@ type foo = { bar: 2 }
267
273
 
268
274
  ```js
269
275
  // eslint.config.js
270
- import antfu from '@antfu/eslint-config'
276
+ import lincy from '@lincy/eslint-config'
271
277
 
272
- export default antfu(
273
- { vue: true, typescript: true },
278
+ export default lincy(
279
+ {
280
+ isInEditor: true,
281
+ vue: true,
282
+ typescript: true,
283
+ stylistic: true,
284
+ gitignore: true,
285
+ test: true,
286
+ jsonc: true,
287
+ yaml: true,
288
+ markdown: true,
289
+ overrides: {}
290
+ },
274
291
  {
275
292
  // 记得在这里指定文件 glob,否则可能会导致 vue 插件处理非 vue 文件
276
293
  files: ['**/*.vue'],
@@ -291,26 +308,37 @@ export default antfu(
291
308
 
292
309
  ```js
293
310
  // eslint.config.js
294
- import antfu from '@antfu/eslint-config'
311
+ import lincy from '@lincy/eslint-config'
295
312
 
296
- export default antfu({
313
+ export default lincy({
314
+ stylistic: {
315
+ indent: 4
316
+ },
297
317
  overrides: {
318
+ // 重写vue规则
298
319
  vue: {
299
320
  'vue/operator-linebreak': ['error', 'before'],
300
321
  },
322
+ // 重写ts规则
301
323
  typescript: {
302
324
  'ts/consistent-type-definitions': ['error', 'interface'],
303
325
  },
326
+ // 重写js规则
304
327
  javascript: {},
305
328
  stylistic: {
306
- 'antfu/consistent-list-newline': 'off',
329
+ 'antfu/consistent-list-newline': 'error',
307
330
  },
308
331
  yaml: {},
332
+ jsonc: {},
333
+ markdown: {},
334
+ test: {},
335
+
336
+ // 追加自定义排除文件(夹)
309
337
  ignores: [
310
338
  '**/assets',
311
339
  '**/static',
312
340
  ]
313
- // ...
341
+ // ...
314
342
  }
315
343
  })
316
344
  ```
@@ -330,6 +358,68 @@ export default lincy({
330
358
  })
331
359
  ```
332
360
 
361
+ ### React
362
+ react拓展规则
363
+
364
+ 安装react插件
365
+
366
+ ```bash
367
+ npm i -D eslint-plugin-react eslint-plugin-react-hooks
368
+ ```
369
+
370
+ ```js
371
+ // eslint.config.js
372
+ import lincy from '@lincy/eslint-config'
373
+ import pluginReact from 'eslint-plugin-react'
374
+ import pluginReactHooks from 'eslint-plugin-react-hooks'
375
+
376
+ export default lincy(
377
+ {
378
+ vue: false,
379
+ },
380
+ // react 相关规则
381
+ {
382
+ files: ['**/*.{jsx,tsx}'],
383
+ settings: {
384
+ react: {
385
+ version: '17.0',
386
+ },
387
+ },
388
+ plugins: {
389
+ 'react': pluginReact,
390
+ 'react-hooks': pluginReactHooks,
391
+ },
392
+ languageOptions: {
393
+ parserOptions: {
394
+ ecmaFeatures: {
395
+ jsx: true,
396
+ },
397
+ },
398
+ },
399
+ rules: {
400
+ ...pluginReact.configs.all.rules,
401
+ ...pluginReactHooks.configs.recommended.rules,
402
+ 'react/jsx-filename-extension': 'off', // 禁止可能包含 JSX 文件扩展名
403
+ 'react/jsx-first-prop-new-line': 'off', // 强制 JSX 中第一个属性的正确位置
404
+ 'react/jsx-no-bind': 'off', // .bind()JSX 属性中禁止使用箭头函数
405
+ 'react/forbid-component-props': 'off', // 禁止组件上使用某些 props
406
+ 'react/jsx-max-props-per-line': ['error', { maximum: 4 }], // 在 JSX 中的单行上强制执行最多 props 数量
407
+ 'react/jsx-no-literals': 'off', // 禁止在 JSX 中使用字符串文字
408
+ 'react/jsx-one-expression-per-line': 'off', // 每行一个 JSX 元素
409
+ 'react/no-danger': 'off', // 禁止使用 dangerouslySetInnerHTML
410
+ 'react/jsx-max-depth': 'off', // 强制 JSX 最大深度
411
+ 'react/jsx-newline': 'off', // 在 jsx 元素和表达式之后换行
412
+ 'react/require-default-props': 'off', // 为每个非必需 prop 强制执行 defaultProps 定义
413
+ 'react/jsx-props-no-spreading': 'off', // 强制任何 JSX 属性都不会传播
414
+ 'react/no-unsafe': 'off', // 禁止使用不安全的生命周期方法
415
+ 'jsx-quotes': ['error', 'prefer-double'], // 强制在 JSX 属性中一致使用双引号或单引号
416
+ 'react/react-in-jsx-scope': 'off', // 使用 JSX 时需要引入 React
417
+ 'react/hook-use-state': 'off', // useState 钩子值和 setter 变量的解构和对称命名
418
+ },
419
+ },
420
+ )
421
+ ```
422
+
333
423
  ### Lint Staged
334
424
 
335
425
  如果你想在每次提交之前应用 lint 和自动修复,你可以将以下内容添加到你的 `package.json` 中:
@@ -0,0 +1,40 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ var __accessCheck = (obj, member, msg) => {
8
+ if (!member.has(obj))
9
+ throw TypeError("Cannot " + msg);
10
+ };
11
+ var __privateGet = (obj, member, getter) => {
12
+ __accessCheck(obj, member, "read from private field");
13
+ return getter ? getter.call(obj) : member.get(obj);
14
+ };
15
+ var __privateAdd = (obj, member, value) => {
16
+ if (member.has(obj))
17
+ throw TypeError("Cannot add the same private member more than once");
18
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
19
+ };
20
+ var __privateSet = (obj, member, value, setter) => {
21
+ __accessCheck(obj, member, "write to private field");
22
+ setter ? setter.call(obj, value) : member.set(obj, value);
23
+ return value;
24
+ };
25
+ var __privateWrapper = (obj, member, setter, getter) => ({
26
+ set _(value) {
27
+ __privateSet(obj, member, value, setter);
28
+ },
29
+ get _() {
30
+ return __privateGet(obj, member, getter);
31
+ }
32
+ });
33
+
34
+ export {
35
+ __publicField,
36
+ __privateGet,
37
+ __privateAdd,
38
+ __privateSet,
39
+ __privateWrapper
40
+ };