@cgzair/eslint-config-cgz 2.0.4 → 2.0.6

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.
Files changed (2) hide show
  1. package/README.md +61 -268
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -2,311 +2,104 @@
2
2
 
3
3
  ## 简介
4
4
 
5
- 本包提供了《信息技术部前端编码手册》配套的 ESLint 可共享配置,提供了多套配置文件以支持 JavaScript、TypeScript、React、Vue、Node.js 等多种项目类型。
5
+ 本包提供了《信息技术部前端编码手册》配套的 ESLint 可共享配置(拥抱 ESLint v9 Flat Config 扁平化配置),支持 JavaScript、TypeScript、React、Vue、Uni-App、Node.js、ES5 等多种项目类型。
6
6
 
7
- ### JavaScript 项目
7
+ **本包的依赖(如各个插件和解析器)已经内置在 dependencies 中,你无需在项目中手动安装插件或解析器,直接安装本包与 `eslint` 即可。**
8
8
 
9
- [coreUrl]: https://www.npmjs.com/package/@babel/core/v/7.16.7
10
- [eslintParserUrl]: https://www.npmjs.com/package/@babel/eslint-parser/v/7.16.5
11
- [importUrl]: https://www.npmjs.com/package/eslint-plugin-import/v/2.25.4
12
- [presetReactUrl]: https://www.npmjs.com/package/@babel/preset-react/v/7.16.7
13
- [reactUrl]: https://www.npmjs.com/package/@babel/preset-react/v/7.16.7
14
- [reactHooksUrl]: https://www.npmjs.com/package/eslint-plugin-react-hooks/v/4.3.0
15
- [a11yUrl]: https://www.npmjs.com/package/eslint-plugin-jsx-a11y/v/6.5.1
16
- [vueEslintParserUrl]: https://www.npmjs.com/package/vue-eslint-parser/v/8.3.0
17
- [eslintPluginVueUrl]: https://www.npmjs.com/package/eslint-plugin-vue/v/8.5.0
18
- [eslintConfigEggUrl]: https://www.npmjs.com/package/eslint-config-egg/v/11.0.1
19
- [tslintParserUrl]: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser
20
- [tslintPluginUrl]: https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin
21
-
22
- | 项目类型 | 配置文件 | 依赖 |
23
- | --- | --- | --- |
24
- | 一般项目 | @cgzair/eslint-config-cgz | [`@babel/core`][coreUrl]@^7.16.7 <br/> [`@babel/eslint-parser`][eslintParserUrl]@^7.16.5 <br/> [`eslint-plugin-import`][importUrl]@^2.25.4 |
25
- | React 项目 | @cgzair/eslint-config-cgz/react <br /> @cgzair/eslint-config-cgz/jsx-a11y(可选) | [`@babel/core`][coreUrl]@^7.16.7 <br/>[`@babel/eslint-parser`][eslintParserUrl]@^7.16.5 <br/> [`@babel/preset-react`][presetReactUrl]@^7.16.7 <br/> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`eslint-plugin-react`][reactUrl]@^7.16.7 <br /> [`eslint-plugin-react-hooks`][reactHooksUrl]@^4.3.0 <br /> [`eslint-plugin-jsx-a11y`][a11yUrl]@^6.5.1(可选) |
26
- | Vue 项目 | @cgzair/eslint-config-cgz/vue | [`@babel/core`][coreUrl]@^7.16.7 <br/> [`@babel/eslint-parser`][eslintParserUrl]@^7.16.5 <br/> [`@babel/preset-react`][presetReactUrl]@^7.16.7 <br/> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`vue-eslint-parser`][vueEslintParserUrl]@^8.3.0 <br/> [`eslint-plugin-vue`][eslintPluginVueUrl]@^8.5.0 |
27
- | Uni-App 项目 | @cgzair/eslint-config-cgz/uni-app | [`@babel/core`][coreUrl]@^7.16.7 <br/> [`@babel/eslint-parser`][eslintParserUrl]@^7.16.5 <br/> [`@babel/preset-react`][presetReactUrl]@^7.16.7 <br/> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`vue-eslint-parser`][vueEslintParserUrl]@^8.3.0 <br/> [`eslint-plugin-vue`][eslintPluginVueUrl]@^8.5.0 |
28
- | Node.js 项目 | @cgzair/eslint-config-cgz/node | [`@babel/core`][coreUrl]@^7.16.7 <br/> [`@babel/eslint-parser`][eslintParserUrl]@^7.16.5 <br/> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`eslint-config-egg`][eslintConfigEggUrl]@^11.0.1 |
29
- | ES5 项目 | @cgzair/eslint-config-cgz/es5 | 无 |
30
-
31
- ### TypeScript 项目
32
-
33
- | 项目类型 | 配置文件 | 依赖 |
34
- | --- | --- | --- |
35
- | 一般项目 | @cgzair/eslint-config-cgz/typescript | [`@typescript-eslint/parser`][tslintParserUrl]@^5.13.0 <br/> [`@typescript-eslint/eslint-plugin`][tslintPluginUrl]@^5.13.0 <br /> [`eslint-plugin-import`][importUrl]@^2.25.4 |
36
- | React 项目 | @cgzair/eslint-config-cgz/typescript/react <br /> @cgzair/eslint-config-cgz/jsx-a11y(可选) | [`@typescript-eslint/parser`][tslintParserUrl]@^5.13.0 <br/> [`@typescript-eslint/eslint-plugin`][tslintPluginUrl]@^5.13.0 <br /> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`eslint-plugin-react`][reactUrl]@^7.16.7 <br /> [`eslint-plugin-react-hooks`][reactHooksUrl]@^4.3.0 <br /> [`eslint-plugin-jsx-a11y`][a11yUrl]@^6.5.1(可选) |
37
- | Vue 项目 | @cgzair/eslint-config-cgz/typescript/vue | [`@typescript-eslint/parser`][tslintParserUrl]@^5.13.0 <br/> [`@typescript-eslint/eslint-plugin`][tslintPluginUrl]@^5.13.0 <br /> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`vue-eslint-parser`][vueEslintParserUrl]@^8.3.0 <br /> [`eslint-plugin-vue`][eslintPluginVueUrl]@^8.5.0 |
38
- | Uni-App 项目 | @cgzair/eslint-config-cgz/typescript/uni-app | [`@typescript-eslint/parser`][tslintParserUrl]@^5.13.0 <br/> [`@typescript-eslint/eslint-plugin`][tslintPluginUrl]@^5.13.0 <br /> [`eslint-plugin-import`][importUrl]@^2.25.4 <br /> [`vue-eslint-parser`][vueEslintParserUrl]@^8.3.0 <br /> [`eslint-plugin-vue`][eslintPluginVueUrl]@^8.5.0 |
39
- | Node.js 项目 | @cgzair/eslint-config-cgz/typescript/node | [`@typescript-eslint/parser`][tslintParserUrl]@^5.13.0 <br/> [`@typescript-eslint/eslint-plugin`][tslintPluginUrl]@^5.13.0 <br /> [`eslint-plugin-import`][importUrl]@^2.25.4 <br/> [`eslint-config-egg`][eslintConfigEggUrl]@^11.0.1 |
40
-
41
- ## 安装与使用
42
-
43
- 推荐使用《信息技术部前端编码手册》配套的 CLI & 工具包fabric接入本包。如果想自己手动接入,可以在上面表格中找到自己项目类型对应的配置文件,在 [ESLint 配置](https://eslint.org/docs/user-guide/configuring) 中 [extends](https://eslint.org/docs/user-guide/configuring#extending-configuration-files),并安装所需依赖。下面对各项目类型的安装和使用进行详细介绍:
44
-
45
- ### JavaScript
46
-
47
- 鉴于历史项目居多,可以优先考虑接入 `JavaScript` 类型的项目。
48
-
49
- #### 一般项目
50
-
51
- 针对未使用 React 或 Vue 的一般 JavaScript 项目,使用 ESLint 原生规则和 [`eslint-plugin-import`][importUrl] 规则,使用 [`@babel/eslint-parser`][eslintParserUrl] 作为 [parser](https://eslint.org/docs/user-guide/configuring#specifying-parser),是本包的默认配置。
52
-
53
- ##### 安装
54
-
55
- ```sh
56
- pnpm add -D @cgzair/eslint-config-cgz @babel/eslint-parser@^7.16.5 @babel/core@^7.16.7 eslint-plugin-import@^2.25.4
57
- ```
58
-
59
- ##### 使用
60
-
61
- ```json
62
- {
63
- "extends": "@cgzair/eslint-config-cgz"
64
- }
65
- ```
66
-
67
- #### React 项目
68
-
69
- 针对 JS React 项目,继承了默认配置,并启用了 [`eslint-plugin-react`][reactUrl] 和 [`eslint-plugin-react-hooks`][reactHooksUrl] 的规则。
70
-
71
- ##### 安装
72
-
73
- ```sh
74
- pnpm add -D @cgzair/eslint-config-cgz @babel/core@^7.16.7 @babel/eslint-parser@^7.16.5 eslint-plugin-import@^2.25.4 eslint-plugin-react@^7.16.7 eslint-plugin-react@^4.3.0
75
- ```
76
-
77
- ##### 使用
78
-
79
- ```json
80
- {
81
- "extends": "@cgzair/eslint-config-cgz/react"
82
- }
83
- ```
84
-
85
- ##### a11y 支持
86
-
87
- 本包也提供了 JSX a11y 的检查规则,但没有在 React 规则包中默认引入,你可以按如下步骤手动启用:
88
-
89
- 安装 `eslint-plugin-jsx-a11y` 插件:
9
+ ## 安装
90
10
 
91
11
  ```bash
92
- pnpm add -D eslint-plugin-jsx-a11y@^6.5.1
12
+ pnpm add @cgzair/eslint-config-cgz eslint -D
93
13
  ```
94
14
 
95
- 在配置中引入 `@cgzair/eslint-config-cgz/jsx-a11y`:
15
+ ## 使用
96
16
 
97
- ```json
98
- {
99
- "extends": ["@cgzair/eslint-config-cgz/react", "@cgzair/eslint-config-cgz/jsx-a11y"]
100
- }
101
- ```
17
+ 在项目根目录下创建扁平化配置文件 `eslint.config.mjs`:
102
18
 
103
- #### Vue 项目
19
+ ```javascript
20
+ import cgz, { defineConfig } from "@cgzair/eslint-config-cgz";
104
21
 
105
- 针对 JS Vue 的项目,继承了默认配置,并启用了 [eslint-plugin-vue][eslintPluginVueUrl] 插件的规则,使用 [vue-eslint-parser][vueEslintParserUrl] 作为 parser。
22
+ export default defineConfig([
23
+ {
24
+ ignores: ["**/lib/**", "**/node_modules/**", "dist/**"],
25
+ },
26
+ // 1. 引入规则配置(按项目类型选择,详情见下文)
27
+ ...cgz.common,
106
28
 
107
- ##### 安装
29
+ // 2. Prettier 冲突解决配置(建议置于数组最末尾)
30
+ cgz.prettier,
108
31
 
109
- ```sh
110
- pnpm add -D @cgzair/eslint-config-cgz vue-eslint-parser@^8.3.0 @babel/core@^7.16.7 @babel/eslint-parser@^7.16.5 eslint-plugin-import@^2.25.4 eslint-plugin-vue@^8.5.0
32
+ {
33
+ // 3. 自定义规则配置
34
+ rules: {
35
+ "no-console": "warn",
36
+ },
37
+ },
38
+ ]);
111
39
  ```
112
40
 
113
- ##### 使用
41
+ ## 配置项清单
114
42
 
115
- ```json
116
- {
117
- "extends": "@cgzair/eslint-config-cgz/vue"
118
- }
119
- ```
43
+ `@cgzair/eslint-config-cgz` 导出的配置对象 `cgz` 提供了以下配置项,它们全部为 `FlatConfig[]` 数组或 `FlatConfig` 对象:
120
44
 
121
- #### Uni-App 项目
45
+ ### 1. JavaScript 项目
122
46
 
123
- 针对 JS Uni-App 的项目,继承了 @cgzair/eslint-config-cgz/vue 全部配置,并增加了小程序环境特有的全局变量。
47
+ | 配置项 | 描述 |
48
+ | ------------- | ------------------------------------------------------- |
49
+ | `cgz.common` | 针对一般 JavaScript 项目的基础规范规则 |
50
+ | `cgz.react` | 针对 JavaScript React 项目的规则规范(包含 Hooks 规则) |
51
+ | `cgz.vue` | 针对 JavaScript Vue 项目的规则规范 |
52
+ | `cgz.uniApp` | 针对 JavaScript Uni-App 项目的规则规范 |
53
+ | `cgz.node` | 针对 JavaScript Node.js 项目的规则规范 |
54
+ | `cgz.es5` | 针对传统 ES5 及以下环境老项目的配置 |
55
+ | `cgz.jsxA11y` | 独立的 JSX 无障碍(Accessibility)辅助规则(可选) |
124
56
 
125
- ##### 安装
57
+ ### 2. TypeScript 项目
126
58
 
127
- ```sh
128
- pnpm add -D @cgzair/eslint-config-cgz vue-eslint-parser@^8.3.0 @babel/core@^7.16.7 @babel/eslint-parser@^7.16.5 eslint-plugin-import@^2.25.4 eslint-plugin-vue@^8.5.0
129
- ```
59
+ | 配置项 | 描述 |
60
+ | ---------------- | ------------------------------------------------------- |
61
+ | `cgz.typescript` | 针对一般 TypeScript 项目的基础规范规则 |
62
+ | `cgz.ts.react` | 针对 TypeScript React 项目的规则规范(包含 Hooks 规则) |
63
+ | `cgz.ts.vue` | 针对 TypeScript Vue 项目的规则规范 |
64
+ | `cgz.ts.uniApp` | 针对 TypeScript Uni-App 项目的规则规范 |
65
+ | `cgz.ts.node` | 针对 TypeScript Node.js 项目的规则规范 |
130
66
 
131
- ##### 使用
67
+ > **提示**:在使用 TypeScript 相关配置时,请确保项目根目录下已安装 `typescript` 依赖并配置了 `tsconfig.json`。
132
68
 
133
- ```json
134
- {
135
- "extends": "@cgzair/eslint-config-cgz/uni-app"
136
- }
137
- ```
69
+ ### 3. Prettier 冲突解决
138
70
 
139
- #### Node.js 项目
71
+ - `cgz.prettier`:用于关闭所有与 Prettier 格式化冲突的 ESLint 规则。**请务必将其置于配置数组的最后一位**。
140
72
 
141
- 针对 Node.js 项目,继承了默认配置和 [egg-config-egg] 的 node 规则(`https://github.com/eggjs/eslint-config-egg/blob/master/lib/rules/node.js`),规则由 ESLint 原生规则和 [eslint-plugin-node][eslintConfigEggUrl] 提供。
73
+ ---
142
74
 
143
- ##### 安装
75
+ ## 配合特定配置使用示例
144
76
 
145
- ```sh
146
- pnpm add -D @cgzair/eslint-config-cgz @babel/core@^7.16.7 @babel/eslint-parser@^7.16.5 eslint-plugin-import@^2.25.4 eslint-config-egg@^11.0.1
147
- ```
77
+ ### TS React + Prettier 项目配置示例
148
78
 
149
- ##### 使用
79
+ ```javascript
80
+ import cgz, { defineConfig } from "@cgzair/eslint-config-cgz";
150
81
 
151
- ```json
152
- {
153
- "extends": "@cgzair/eslint-config-cgz/node"
154
- }
82
+ export default defineConfig([...cgz.ts.react, cgz.prettier]);
155
83
  ```
156
84
 
157
- #### ES5 项目
85
+ ### TS Vue + JSX a11y 项目配置示例
158
86
 
159
- 针对仍在使用 ES5 及之前版本 JS 的老项目,使用 ESLint 原生规则。
87
+ ```javascript
88
+ import cgz, { defineConfig } from "@cgzair/eslint-config-cgz";
160
89
 
161
- ##### 安装
162
-
163
- ```sh
164
- pnpm add -D @cgzair/eslint-config-cgz
90
+ export default defineConfig([...cgz.ts.vue, ...cgz.jsxA11y, cgz.prettier]);
165
91
  ```
166
92
 
167
- ##### 使用
93
+ ## IDE 插件配套集成
168
94
 
169
- ```json
170
- {
171
- "extends": "@cgzair/eslint-config-cgz/es5"
172
- }
173
- ```
174
-
175
- ### TypeScript
176
-
177
- 所有的 TypeScript 项目启用 [`@typescript-eslint/eslint-plugin`][tslintPluginUrl] 插件的规则,并使用 [`@typescript-eslint/parser`][tslintParserUrl] 作为 parser。
178
-
179
- #### 一般项目
180
-
181
- 针对未使用 React 或 Vue 的一般 TypeScript 项目,继承本配置。
182
-
183
- ##### 安装
184
-
185
- ```sh
186
- pnpm add -D @cgzair/eslint-config-cgz eslint-plugin-import@^2.25.4 @typescript-eslint/eslint-plugin@^5.13.0 @typescript-eslint/parser@^5.13.0
187
- ```
188
-
189
- ##### 使用
190
-
191
- ```json
192
- {
193
- "extends": "@cgzair/eslint-config-cgz/typescript"
194
- }
195
- ```
95
+ 推荐使用 `vscode` 作为开发工具,然后安装 `ESLint` 插件,安装地址:<https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint>。
196
96
 
197
- 需保证项目已安装 `typescript` 依赖,另外如果项目的 TS 配置文件不是 `./tsconfig.json`,则需要设置 `.eslintrc` 中的 [parserOptions.project](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/parser#parseroptionsproject) 字段 ,例如:
97
+ `.vscode/settings.json` 中确保开启了保存自动修复功能:
198
98
 
199
99
  ```json
200
100
  {
201
- "extends": "@cgzair/eslint-config-cgz/typescript",
202
- "parserOptions": {
203
- "project": "./tsconfig.eslint.json"
101
+ "editor.codeActionsOnSave": {
102
+ "source.fixAll.eslint": "explicit"
204
103
  }
205
104
  }
206
105
  ```
207
-
208
- #### React 项目
209
-
210
- 针对 TS React 项目,继承了 JS React 的配置。
211
-
212
- ##### 安装
213
-
214
- ```sh
215
- pnpm add -D @cgzair/eslint-config-cgz eslint-plugin-import@^2.25.4 eslint-plugin-react@^7.16.7 eslint-plugin-react-hooks@^4.3.0 @typescript-eslint/eslint-plugin@^5.13.0 @typescript-eslint/parser@^5.13.0
216
- ```
217
-
218
- ##### 使用
219
-
220
- ```json
221
- {
222
- "extends": "@cgzair/eslint-config-cgz/typescript/react"
223
- }
224
- ```
225
-
226
- ##### a11y 支持
227
-
228
- 本包也提供了 JSX a11y 的检查规则,但没有在 TS React 规则包中默认引入,你可以按如下步骤手动启用:
229
-
230
- 安装 `eslint-plugin-jsx-a11y` 插件:
231
-
232
- ```bash
233
- pnpm add -D eslint-plugin-jsx-a11y@^6.5.1
234
- ```
235
-
236
- 在配置中引入 `@cgzair/eslint-config-cgz/jsx-a11y`:
237
-
238
- ```json
239
- {
240
- "extends": ["@cgzair/eslint-config-cgz/typescript/react", "@cgzair/eslint-config-cgz/jsx-a11y"]
241
- }
242
- ```
243
-
244
- #### Vue 项目
245
-
246
- 针对 TS Vue 项目,继承了 JS Vue 的配置。
247
-
248
- ##### 安装
249
-
250
- ```sh
251
- pnpm add -D @cgzair/eslint-config-cgz eslint-plugin-import@^2.25.4 eslint-plugin-vue@^8.5.0 vue-eslint-parser@^8.3.0 @typescript-eslint/eslint-plugin@^5.13.0 @typescript-eslint/parser@^5.13.0
252
- ```
253
-
254
- ##### 使用
255
-
256
- ```json
257
- {
258
- "extends": "@cgzair/eslint-config-cgz/typescript/vue"
259
- }
260
- ```
261
-
262
- #### Uni-App 项目
263
-
264
- 针对 TS Uni-App 项目,继承了 JS Uni-App 的配置。
265
-
266
- ##### 安装
267
-
268
- ```sh
269
- pnpm add -D @cgzair/eslint-config-cgz eslint-plugin-import@^2.25.4 eslint-plugin-vue@^8.5.0 vue-eslint-parser@^8.3.0 @typescript-eslint/eslint-plugin@^5.13.0 @typescript-eslint/parser@^5.13.0
270
- ```
271
-
272
- ##### 使用
273
-
274
- ```json
275
- {
276
- "extends": "@cgzair/eslint-config-cgz/typescript/uni-app"
277
- }
278
- ```
279
-
280
- #### Node.js 项目
281
-
282
- 针对未使用 React 和 Vue 的 TypeScript(Node) 项目,继承了 JS Node.js 配置。
283
-
284
- ##### 安装
285
-
286
- ```sh
287
- pnpm add -D @cgzair/eslint-config-cgz eslint-plugin-import@^2.25.4 @typescript-eslint/eslint-plugin@^5.13.0 @typescript-eslint/parser@^5.13.0 eslint-config-egg@^11.0.1
288
- ```
289
-
290
- ##### 使用
291
-
292
- ```json
293
- {
294
- "extends": "@cgzair/eslint-config-cgz/typescript/node"
295
- }
296
- ```
297
-
298
- ### 配合 Prettier 使用
299
-
300
- 手动安装 [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier)@8.5.0,注意,请确保安装版本一致,否则可能出现不兼容的错误。
301
-
302
- ```sh
303
- pnpm add -D eslint-config-prettier@8.5.0
304
- ```
305
-
306
- 并修改 eslintrc 的 `extends` 如下:
307
-
308
- ```json
309
- {
310
- "extends": ["@cgzair/eslint-config-cgz/typescript/react", "prettier"]
311
- }
312
- ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cgzair/eslint-config-cgz",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "ESLint shareable config for cgz frontend Guidelines",
5
5
  "main": "main.js",
6
6
  "keywords": [
@@ -68,5 +68,5 @@
68
68
  "pnpm": ">=9.0.0"
69
69
  },
70
70
  "license": "MIT",
71
- "gitHead": "516c41f893f18edaf5b49db8eab6e7541ba70c31"
71
+ "gitHead": "950a840ad96068b6eaae19e0d2322ba24ef5c864"
72
72
  }