eslint-plugin-light 1.0.4 → 1.0.5

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## <small>1.0.5 (2025-05-16)</small>
2
+
3
+ * feat(eslint-plugin-light): add no-import-all-in-one-api rule ([f92123e](https://github.com/novlan1/plugin-light/commits/f92123e))
4
+ * docs: update docs ([0721738](https://github.com/novlan1/plugin-light/commits/0721738))
5
+
6
+
7
+
1
8
  ## <small>1.0.4 (2025-04-18)</small>
2
9
 
3
10
  * chore: update license ([5ba3e91](https://github.com/novlan1/plugin-light/commits/5ba3e91))
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  ## ESLint Plugin Light
2
2
 
3
-
4
3
  <p align="center">
5
4
  <img src="https://img.shields.io/npm/dw/eslint-plugin-light">
6
5
  <img src="https://img.shields.io/npm/unpacked-size/eslint-plugin-light">
@@ -10,25 +9,25 @@
10
9
  <img src="https://img.shields.io/github/created-at/novlan1/plugin-light">
11
10
  </p>
12
11
 
13
- Simple Eslint plugin.
12
+ 简单、易用的 ESLint 插件库。
14
13
 
15
- ### 1. Installation
14
+ ### 1. 安装
16
15
 
17
- You need to install [ESLint](https://eslint.org/) first.
16
+ 首先要安装 [ESLint](https://eslint.org/)
18
17
 
19
18
  ```sh
20
- npm i eslint -D
19
+ pnpm i eslint -D
21
20
  ```
22
21
 
23
- Next, install `eslint-plugin-light`.
22
+ 然后安装本插件 `eslint-plugin-light`。
24
23
 
25
24
  ```sh
26
25
  npm i eslint-plugin-light -D
27
26
  ```
28
27
 
29
- ### 2. Usage
28
+ ### 2. 使用
30
29
 
31
- Add `light` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
30
+ `.eslintrc` 配置文件的 `plugins` 中增加本插件 `eslint-plugin-light`,或者省略 `eslint-plugin-` 前缀。
32
31
 
33
32
  ```json
34
33
  {
@@ -38,8 +37,7 @@ Add `light` to the plugins section of your `.eslintrc` configuration file. You c
38
37
  }
39
38
  ```
40
39
 
41
-
42
- Then configure the rules you want to use under the rules section.
40
+ 然后配置你想使用的规则。
43
41
 
44
42
  ```json
45
43
  {
@@ -49,7 +47,7 @@ Then configure the rules you want to use under the rules section.
49
47
  }
50
48
  ```
51
49
 
52
- or use extends:
50
+ 也可以使用本工具提供的扩展。
53
51
 
54
52
  ```json
55
53
  {
@@ -57,16 +55,14 @@ or use extends:
57
55
  }
58
56
  ```
59
57
 
58
+ ### 3. 规则
60
59
 
61
- ### 3. Supported Rules
62
-
63
- #### 3.1. light/valid-vue-comp-import
60
+ #### 3.1. valid-vue-comp-import
64
61
 
65
- 禁止从`js`文件中加载`Vue`组件
62
+ 禁止从 `js` 文件中加载 `Vue` 组件。
66
63
 
67
64
  比如,
68
65
 
69
-
70
66
  1. 导入地址是`js/ts`文件
71
67
 
72
68
  ```js
@@ -84,8 +80,6 @@ import SomeComp from 'src/local-component/ui/pages/user/account-manage/xxx.vue';
84
80
 
85
81
  注意上面的`xxx.vue`是从`index.js`中分析得到的原始文件路径。
86
82
 
87
-
88
-
89
83
  2. 导入一个目录,但目录存在`index.js`,这时候不管存不存在`index.vue`,`uni-app`转换都会失败
90
84
 
91
85
  ```js
@@ -118,9 +112,9 @@ import CComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-
118
112
  import DComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-new/comp/d.vue';
119
113
  ```
120
114
 
121
- #### 3.2. light/no-plus-turn-number
115
+ #### 3.2. no-plus-turn-number
122
116
 
123
- 禁止在`vue`的`template`中用`+`号转换字符串为数字
117
+ 禁止在 `vue` 的 `template` 中用 `+` 号转换字符串为数字
124
118
 
125
119
  比如:
126
120
 
@@ -130,7 +124,7 @@ import DComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-
130
124
  />
131
125
  ```
132
126
 
133
- 如果加了`--fix`,会被转化成:
127
+ 如果加了 `--fix`,会被转化成:
134
128
 
135
129
  ```html
136
130
  <ScheduleItem
@@ -138,7 +132,7 @@ import DComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-
138
132
  />
139
133
  ```
140
134
 
141
- #### 3.3 no-complex-key
135
+ #### 3.3. no-complex-key
142
136
 
143
137
  不要在`vue`模板中使用复杂的`key`。包括:
144
138
 
@@ -178,7 +172,7 @@ getData() {
178
172
  `uni-app`中,`key`重复的话,会造成挂载在组件上面的事件参数为`undefined`,从而不成功。
179
173
 
180
174
 
181
- #### 3.4 json-parse-try-catch
175
+ #### 3.4. json-parse-try-catch
182
176
 
183
177
  `JSON.parse` 应该加 `try catch`。
184
178
 
@@ -190,7 +184,6 @@ JSON.parse(JSON.stringify(abc));
190
184
 
191
185
  可以配置 `strict` 参数为 `true`,开启检查。
192
186
 
193
-
194
187
  ```js
195
188
  // .eslintrc.js
196
189
 
@@ -203,3 +196,54 @@ module.exports = {
203
196
  },
204
197
  }
205
198
  ```
199
+
200
+ #### 3.5. no-import-all-in-one-api
201
+
202
+ 使用 `src/api` 子仓库时, 不应该导入全部接口,而应该按需引入。
203
+
204
+ Bad case:
205
+
206
+ ```js
207
+ // bad
208
+ import { pubg_fateClient } from 'src/api/git.a.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate.http';
209
+ ```
210
+
211
+ Good case:
212
+
213
+ ```js
214
+ // good
215
+ import { QueryGameListHomePageClient } from 'src/api/git.a.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate/QueryGameListHomePage.http';
216
+ ```
217
+
218
+ Usage:
219
+
220
+ ```js
221
+ // .eslintrc.js
222
+
223
+ module.exports = {
224
+ plugins: [
225
+ 'light',
226
+ ],
227
+ rules: {
228
+ 'light/no-import-all-in-one-api': 2,
229
+ },
230
+ }
231
+ ```
232
+
233
+ 下面是一个案例,根据这个规则,对一个线上项目进行改造。仅仅改动了[几个文件的几行引入语句](https://git.a.com/pmd-mobile/match/gp-next/commit/20e0050fe10ed767dcb5d77dca76a9b51c23820e),就减少了主包 `50KB`,效果立竿见影。
234
+
235
+ 之前:
236
+
237
+ <img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/5/own_mike_p8Sr7F4zjtry4mtR.png" width="600">
238
+
239
+ 之后:
240
+
241
+ <img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/5/own_mike_MJ8Q5QXEyamnGZXz.png" width="600">
242
+
243
+ 改动的几个文件:
244
+
245
+ <img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/5/own_mike_rfMPwBYtZmhXrthT.png" width="600">
246
+
247
+ ### 4. 更新日志
248
+
249
+ [点此查看](./CHANGELOG.md)
@@ -0,0 +1,50 @@
1
+ const CORE_REG = /src\/api\/[\w.-_/]+\/[a-z][\w-]+.http(.ts)?$/;
2
+
3
+
4
+ module.exports = {
5
+ meta: {
6
+ type: 'problem',
7
+ docs: {
8
+ description: '禁止从指定的文件中导入',
9
+ category: 'Possible Errors',
10
+ recommended: true,
11
+ },
12
+ schema: [
13
+ {
14
+ type: 'object',
15
+ properties: {
16
+ excludes: {
17
+ type: 'array',
18
+ items: {
19
+ type: 'string',
20
+ },
21
+ },
22
+ },
23
+ additionalProperties: false,
24
+ },
25
+ ],
26
+ messages: {
27
+ restrictedImport: '不允许从 {{filePath}} 导入。',
28
+ },
29
+ },
30
+ create(context) {
31
+ // const projectRoot = context.settings.projectRoot ?? process.cwd();
32
+ const options = context.options[0] || {};
33
+ const excludes = options.excludes || [];
34
+
35
+ return {
36
+ ImportDeclaration(node) {
37
+ const sourceValue = node.source.value;
38
+
39
+ if (CORE_REG.test(sourceValue)
40
+ && !excludes.includes(sourceValue)
41
+ ) {
42
+ context.report({
43
+ node,
44
+ message: `不允许从 ${sourceValue} 导入全部接口,应按需导入`,
45
+ });
46
+ }
47
+ },
48
+ };
49
+ },
50
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-light",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Simple Eslint Plugin",
5
5
  "keywords": [
6
6
  "eslint",