eslint-plugin-light 1.0.14 → 1.0.16

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,60 @@
1
+ ## <small>1.0.22 (2026-03-24)</small>
2
+
3
+ * feat: add no-complex-style-class rule ([2cc25ee](https://github.com/novlan1/plugin-light/commits/2cc25ee))
4
+ * feat: optmize eslint rules ([b3e85c6](https://github.com/novlan1/plugin-light/commits/b3e85c6))
5
+ * chore: update deps ([6828b0c](https://github.com/novlan1/plugin-light/commits/6828b0c))
6
+ * chore: update package.json ([338b104](https://github.com/novlan1/plugin-light/commits/338b104))
7
+
8
+
9
+
10
+ ## <small>1.0.21 (2026-01-04)</small>
11
+
12
+ * feat(eslint-plugin-light): add no-src-imports-in-components rule ([b960951](https://github.com/novlan1/plugin-light/commits/b960951))
13
+
14
+
15
+
16
+ ## <small>1.0.20 (2025-12-18)</small>
17
+
18
+ * feat(project-config-pixui): add try-catch for version-info ([775fb7b](https://github.com/novlan1/plugin-light/commits/775fb7b))
19
+
20
+
21
+
22
+ ## <small>1.0.19 (2025-12-12)</small>
23
+
24
+ * chore: update docs ([dc64df7](https://github.com/novlan1/plugin-light/commits/dc64df7))
25
+ * chore: update docs ([bd72447](https://github.com/novlan1/plugin-light/commits/bd72447))
26
+ * chore: update t-comm@2.0.16 ([8c5d294](https://github.com/novlan1/plugin-light/commits/8c5d294))
27
+ * chore: use updateManifestCore of t-comm ([1c149ab](https://github.com/novlan1/plugin-light/commits/1c149ab))
28
+ * chore(eslint-plugin-light): add no-non-index-import ([64a7b0b](https://github.com/novlan1/plugin-light/commits/64a7b0b))
29
+ * docs: update docs ([43f864d](https://github.com/novlan1/plugin-light/commits/43f864d))
30
+ * docs: update docs ([c88907f](https://github.com/novlan1/plugin-light/commits/c88907f))
31
+
32
+
33
+
34
+ ## <small>1.0.18 (2025-08-08)</small>
35
+
36
+ * feat(eslint): add no-decimal-in-brackets rule ([a4c2b0d](https://github.com/novlan1/plugin-light/commits/a4c2b0d))
37
+
38
+
39
+
40
+ ## <small>1.0.17 (2025-07-23)</small>
41
+
42
+ * feat(eslint-plugin): add no-direct-img-src rule ([4223182](https://github.com/novlan1/plugin-light/commits/4223182))
43
+
44
+
45
+
46
+ ## <small>1.0.16 (2025-07-18)</small>
47
+
48
+ * feat(eslint): add no-todo-comment rule ([8b555b7](https://github.com/novlan1/plugin-light/commits/8b555b7))
49
+
50
+
51
+
52
+ ## <small>1.0.15 (2025-06-16)</small>
53
+
54
+ * feat(eslint-plugin-light): add img v-lazy ([5807b47](https://github.com/novlan1/plugin-light/commits/5807b47))
55
+
56
+
57
+
1
58
  ## <small>1.0.14 (2025-06-11)</small>
2
59
 
3
60
  * feat(eslint-plugin-light): 优化classname-per-line ([fa99594](https://github.com/novlan1/plugin-light/commits/fa99594))
package/README.md CHANGED
@@ -5,13 +5,17 @@
5
5
  <img src="https://img.shields.io/npm/unpacked-size/eslint-plugin-light">
6
6
  <img src="https://img.shields.io/npm/v/eslint-plugin-light">
7
7
  <img src="https://img.shields.io/npm/l/eslint-plugin-light">
8
- <img src="https://img.shields.io/github/last-commit/novlan1/plugin-light">
9
- <img src="https://img.shields.io/github/created-at/novlan1/plugin-light">
8
+ <img src="https://img.shields.io/badge/TypeScript-strict-blue?logo=typescript&logoColor=white" alt="TypeScript strict">
9
+ <img src="https://img.shields.io/badge/created-2022--01-blue?logo=github&logoColor=white">
10
10
  </p>
11
11
 
12
12
  简单、易用的 ESLint 插件库。
13
13
 
14
- ## 1. 安装
14
+ ## 1. 作者
15
+
16
+ **novlan1**
17
+
18
+ ## 2. 安装
15
19
 
16
20
  首先要安装 [ESLint](https://eslint.org/)。
17
21
 
@@ -25,7 +29,7 @@ pnpm i eslint -D
25
29
  npm i eslint-plugin-light -D
26
30
  ```
27
31
 
28
- ## 2. 使用
32
+ ## 3. 使用
29
33
 
30
34
  在 `.eslintrc` 配置文件的 `plugins` 中增加本插件 `eslint-plugin-light`,或者省略 `eslint-plugin-` 前缀。
31
35
 
@@ -55,9 +59,9 @@ npm i eslint-plugin-light -D
55
59
  }
56
60
  ```
57
61
 
58
- ## 3. 规则
62
+ ## 4. 规则
59
63
 
60
- ### 3.1. valid-vue-comp-import
64
+ ### 4.1. valid-vue-comp-import
61
65
 
62
66
  禁止从 `js` 文件中加载 `Vue` 组件。
63
67
 
@@ -112,7 +116,7 @@ import CComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-
112
116
  import DComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-new/comp/d.vue';
113
117
  ```
114
118
 
115
- ### 3.2. no-plus-turn-number
119
+ ### 4.2. no-plus-turn-number
116
120
 
117
121
  禁止在 `vue` 的 `template` 中用 `+` 号转换字符串为数字
118
122
 
@@ -132,7 +136,7 @@ import DComp from 'src/local-component/module/tip-match/tip-match-schedule-tree-
132
136
  />
133
137
  ```
134
138
 
135
- ### 3.3. no-complex-key
139
+ ### 4.3. no-complex-key
136
140
 
137
141
  不要在`vue`模板中使用复杂的`key`。包括:
138
142
 
@@ -172,7 +176,7 @@ getData() {
172
176
  `uni-app`中,`key`重复的话,会造成挂载在组件上面的事件参数为`undefined`,从而不成功。
173
177
 
174
178
 
175
- ### 3.4. json-parse-try-catch
179
+ ### 4.4. json-parse-try-catch
176
180
 
177
181
  `JSON.parse` 应该加 `try catch`。
178
182
 
@@ -197,7 +201,7 @@ module.exports = {
197
201
  }
198
202
  ```
199
203
 
200
- ### 3.5. no-import-all-in-one-api
204
+ ### 4.5. no-import-all-in-one-api
201
205
 
202
206
  使用 `src/api` 子仓库时, 不应该导入全部接口,而应该按需引入。
203
207
 
@@ -205,14 +209,14 @@ Bad case:
205
209
 
206
210
  ```js
207
211
  // bad
208
- import { pubg_fateClient } from 'src/api/git.a.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate.http';
212
+ import { pubg_fateClient } from 'src/api/git.aow.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate.http';
209
213
  ```
210
214
 
211
215
  Good case:
212
216
 
213
217
  ```js
214
218
  // good
215
- import { QueryGameListHomePageClient } from 'src/api/git.a.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate/QueryGameListHomePage.http';
219
+ import { QueryGameListHomePageClient } from 'src/api/git.aow.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate/QueryGameListHomePage.http';
216
220
  ```
217
221
 
218
222
  Usage:
@@ -242,14 +246,14 @@ module.exports = {
242
246
  rules: {
243
247
  'light/no-import-all-in-one-api': [2, {
244
248
  excludes: [
245
- 'src/api/git.a.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate.http',
249
+ 'src/api/git.aow.com/itrpcprotocol/esport/esport_cgi/pubg_fate/pubg_fate.http',
246
250
  ]
247
251
  }],
248
252
  },
249
253
  }
250
254
  ```
251
255
 
252
- 下面是一个案例,根据这个规则,对一个线上项目进行改造。仅仅改动了[几个文件的几行引入语句](https://git.a.com/pmd-mobile/match/gp-next/commit/20e0050fe10ed767dcb5d77dca76a9b51c23820e),就减少了主包 `50KB`,效果立竿见影。
256
+ 下面是一个案例,根据这个规则,对一个线上项目进行改造。仅仅改动了[几个文件的几行引入语句](https://git.aow.com/pmd-mobile/match/gp-next/commit/20e0050fe10ed767dcb5d77dca76a9b51c23820e),就减少了主包 `50KB`,效果立竿见影。
253
257
 
254
258
  之前:
255
259
 
@@ -263,7 +267,7 @@ module.exports = {
263
267
 
264
268
  <img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/5/own_mike_rfMPwBYtZmhXrthT.png" width="600">
265
269
 
266
- ### 3.6. no-js-file
270
+ ### 4.6. no-js-file
267
271
 
268
272
  运行时文件不允许使用 `js/jsx`,只允许 `ts/tsx`。子工程的 `config.js` 会自动排除。
269
273
 
@@ -291,7 +295,7 @@ module.exports = {
291
295
  | include | 检查的列表,`glob` 模式 | `['src/{project,local-component,local-logic}/**/*.{js,jsx}']` |
292
296
  | exclude | 排除的列表,`glob` 模式 | `['src/project/*/config.js']` |
293
297
 
294
- ### 3.7. valid-file-name
298
+ ### 4.7. valid-file-name
295
299
 
296
300
  文件命名格式只允许使用 [kebab-case](https://developer.mozilla.org/en-US/docs/Glossary/Kebab_case)。子工程的 `App.vue` 会自动排除。
297
301
 
@@ -321,7 +325,7 @@ module.exports = {
321
325
  | include | 检查的列表,`glob` 模式 | `['src/**/*']` |
322
326
  | exclude | 排除的列表,`glob` 模式 | `['src/project/*/App.vue']` |
323
327
 
324
- ### 3.8. no-multiple-script
328
+ ### 4.8. no-multiple-script
325
329
 
326
330
  禁止 Vue 文件中存在多个 `<script>`。
327
331
 
@@ -342,8 +346,7 @@ module.exports = {
342
346
 
343
347
  <img src="https://mike-1255355338.cos.ap-guangzhou.myqcloud.com/article/2025/5/own_mike_w7ADssRftSYE6myr.png" width="900">
344
348
 
345
-
346
- ### 3.9. valid-spelling
349
+ ### 4.9. valid-spelling
347
350
 
348
351
  校验正确的拼写,比如不能用"帐号",要使用"账号",不能使用"登陆",要使用"登录"。
349
352
 
@@ -368,7 +371,7 @@ module.exports = {
368
371
  | -------- | ------------------------ | -------------------------------- |
369
372
  | spelling | 错误拼写和正确拼写的映射 | `{ 帐号: '账号', 登陆: '登录' }` |
370
373
 
371
- ### 3.10. valid-shaohou
374
+ ### 4.10. valid-shaohou
372
375
 
373
376
  校验正确的"稍候"和"稍后",稍候后面不加词,稍后后面需要加词,比如"请稍候/请稍后再试"。
374
377
 
@@ -387,7 +390,7 @@ module.exports = {
387
390
  }
388
391
  ```
389
392
 
390
- ### 3.11. classname-per-line
393
+ ### 4.11. classname-per-line
391
394
 
392
395
  限制 Vue 中每行只有`1`个CSS类名,可以配置阈值。
393
396
 
@@ -412,6 +415,461 @@ module.exports = {
412
415
  | ------ | -------------------------------- | ------ |
413
416
  | counts | 检查阈值,小于阈值时,可以在一行 | `3` |
414
417
 
415
- ## 4. 更新日志
418
+ ### 4.12. img-v-lazy
419
+
420
+ 强制 `img` 标签使用 `v-lazy` 而不是 `:src`,可用于 Vue 项目中。
421
+
422
+ Usage:
423
+
424
+ ```js
425
+ // .eslintrc.js
426
+
427
+ module.exports = {
428
+ plugins: [
429
+ 'light',
430
+ ],
431
+ rules: {
432
+ 'light/img-v-lazy': 2,
433
+ },
434
+ }
435
+ ```
436
+
437
+ ### 4.13. no-direct-img-src
438
+
439
+ 在 `jsx/tsx` 中禁止直接使用 `img src`,必须使用封装的 [`CdnImage` 组件](https://novlan1.github.io/docs/press-pix/zh-CN/cdn-image.html),可用于 React 项目中。
440
+
441
+ 与 [img-v-lazy 规则](#_4-12-img-v-lazy) 类似,都是为了防止直接使用 COS 图片,而不是 CDN 图片。
442
+
443
+ Usage:
444
+
445
+ ```js
446
+ // .eslintrc.js
447
+
448
+ module.exports = {
449
+ plugins: [
450
+ 'light',
451
+ ],
452
+ rules: {
453
+ 'light/no-direct-img-src': 2,
454
+
455
+ // 传入参数
456
+ 'light/no-direct-img-src': [2, {
457
+ componentName: 'MyCdnImage',
458
+ }],
459
+ },
460
+ }
461
+ ```
462
+
463
+ 配置选项:
464
+
465
+ | 字段 | 说明 | 默认值 |
466
+ | ------------- | ---------------------- | ---------- |
467
+ | componentName | 提示词中的自定义组件名 | `CdnImage` |
468
+
469
+ ### 4.14. no-todo-comment
470
+
471
+ 不允许存在 TODO。可防止调试代码被意外带到线上。
472
+
473
+ Usage:
474
+
475
+ ```js
476
+ // .eslintrc.js
477
+
478
+ module.exports = {
479
+ plugins: [
480
+ 'light',
481
+ ],
482
+ rules: {
483
+ 'light/no-todo-comment': 2,
484
+ },
485
+ }
486
+ ```
487
+
488
+ 配置选项:
489
+
490
+ | 字段 | 说明 | 默认值 |
491
+ | ------- | ---------- | ------- |
492
+ | keyword | 检查关键词 | `TODO:` |
493
+
494
+ ### 4.15. no-non-index-import
495
+
496
+ 限制只能从 `pmd-*` 包的 `lib` 目录下的模块第一层 `index` 引入,禁止从非 `index` 文件引入。
497
+
498
+ 目的是为了保证模块导入的规范性,避免直接导入包内的具体实现文件,便于包的维护和重构。
499
+
500
+ 允许的导入格式:
501
+
502
+ ```js
503
+ // ✅ 直接导入模块(隐式index)
504
+ import Toast from 'pmd-widget/lib/toast';
505
+ import EventBus from 'pmd-tools/lib/e-bus';
506
+
507
+ // ✅ 显式导入index文件
508
+ import Toast from 'pmd-widget/lib/toast/index';
509
+ import EventBus from 'pmd-tools/lib/e-bus/index';
510
+
511
+ // ✅ 导入目录(隐式index)
512
+ import * as Utils from 'pmd-tools/lib/utils/';
513
+ ```
514
+
515
+ 禁止的导入格式:
516
+
517
+ ```js
518
+ // ❌ 直接导入非index文件
519
+ import ToastComponent from 'pmd-widget/lib/toast/Toast.vue';
520
+ import helper from 'pmd-tools/lib/utils/helper.js';
521
+
522
+ // ❌ 多级目录的非index文件
523
+ import DeepComponent from 'pmd-widget/lib/toast/components/Modal.vue';
524
+ ```
525
+
526
+ Usage:
527
+
528
+ ```js
529
+ // .eslintrc.js
530
+
531
+ module.exports = {
532
+ plugins: [
533
+ 'light',
534
+ ],
535
+ rules: {
536
+ 'light/no-non-index-import': 2,
537
+ },
538
+ }
539
+ ```
540
+
541
+ 配置选项:
542
+
543
+ | 字段 | 说明 | 默认值 |
544
+ | ------- | ------------------------------ | ------- |
545
+ | exclude | 排除的导入路径前缀数组 | `[]` |
546
+
547
+ 例如,排除特定包的检查:
548
+
549
+ ```js
550
+ // .eslintrc.js
551
+
552
+ module.exports = {
553
+ plugins: [
554
+ 'light',
555
+ ],
556
+ rules: {
557
+ 'light/no-non-index-import': [2, {
558
+ exclude: [
559
+ 'pmd-special/lib',
560
+ ]
561
+ }],
562
+ },
563
+ }
564
+ ```
565
+
566
+ ### 4.16. no-decimal-in-brackets
567
+
568
+ 禁止在方括号类名中使用小数点。背景:
569
+
570
+ >小程序中不能使用 `pr-[.28rem]` 这种,可以使用 `pr-1.12`,同时在`tailwind.config.js` 中配置下 `theme.extend.padding = {1.12: '.28rem'}`。
571
+ >
572
+ >原因是`uni-app`会把`class`中的`[.`解析成`\[\`放到`wxss`中,导致编译错误。
573
+
574
+ Usage:
575
+
576
+ ```js
577
+ // .eslintrc.js
578
+
579
+ module.exports = {
580
+ plugins: [
581
+ 'light',
582
+ ],
583
+ rules: {
584
+ 'light/no-decimal-in-brackets': 2,
585
+ },
586
+ }
587
+ ```
588
+
589
+ 配置选项:
590
+
591
+ | 字段 | 说明 | 默认值 |
592
+ | ------ | -------- | ----------------------------------- |
593
+ | regexp | 匹配正则 | `/\[[^\](]*\.\d+[a-zA-Z]+[^\]]*\]/` |
594
+
595
+ ### 4.17. valid-pmd-import
596
+
597
+ 禁止在 `packages/xx` 目录下引入对应的 `pmd-xx` 包,需要使用相对路径。
598
+
599
+ 这个规则主要用于 monorepo 项目中,防止包内部引入自己的发布版本,而是使用相对路径引入源码,避免循环依赖和版本不一致问题。
600
+
601
+ 比如,在 `packages/components` 目录下:
602
+
603
+ ```js
604
+ // ❌ 错误:引入自己的包名
605
+ import { Button } from 'pmd-components';
606
+ const utils = require('pmd-components/lib/utils');
607
+
608
+ // ✅ 正确:使用相对路径
609
+ import { Button } from '../src';
610
+ const utils = require('../../src/utils');
611
+ ```
612
+
613
+ 如果加了 `--fix`,会被自动转换为相对路径。
614
+
615
+ Usage:
616
+
617
+ ```js
618
+ // .eslintrc.js
619
+
620
+ module.exports = {
621
+ plugins: [
622
+ 'light',
623
+ ],
624
+ rules: {
625
+ 'light/valid-pmd-import': 2,
626
+ },
627
+ }
628
+ ```
629
+
630
+ 配置选项:
631
+
632
+ | 字段 | 说明 | 默认值 |
633
+ | ------- | ---------------- | ------- |
634
+ | baseDir | 包源码的基础目录 | `'src'` |
635
+
636
+ 例如,如果你的包源码不在 `src` 目录:
637
+
638
+ ```js
639
+ // .eslintrc.js
640
+
641
+ module.exports = {
642
+ plugins: [
643
+ 'light',
644
+ ],
645
+ rules: {
646
+ 'light/valid-pmd-import': [2, {
647
+ baseDir: 'lib'
648
+ }],
649
+ },
650
+ }
651
+ ```
652
+
653
+ 该规则会检查以下导入方式:
654
+
655
+ - ES6 `import` 语句
656
+ - CommonJS `require()` 调用
657
+ - 动态 `import()` 表达式
658
+ - `export ... from` 语句
659
+
660
+ ### 4.18. no-src-imports-in-components
661
+
662
+ #### 4.18.1. 规则说明
663
+
664
+ 禁止在指定目录下使用特定前缀的导入路径,强制使用相对路径。这个规则主要用于确保某些目录(如 `src/components`)中的代码可以被独立发布为 npm 包,不依赖项目的绝对路径导入。
665
+
666
+ #### 4.18.2. 为什么需要这个规则?
667
+
668
+ 当你的项目中某些目录(如 `src/components`)需要被发布为独立的 npm 包时,这些目录中的代码不应该使用项目特定的绝对路径(如 `src/...`),而应该使用相对路径。这样可以确保:
669
+
670
+ 1. 代码的可移植性
671
+ 2. 包的独立性
672
+ 3. 避免发布后的路径解析问题
673
+
674
+ #### 4.18.3. 配置选项
675
+
676
+ ```typescript
677
+ {
678
+ restrictedPaths: string[], // 需要限制的目录路径列表
679
+ bannedPrefixes: string[], // 禁止使用的导入路径前缀列表
680
+ message?: string // 自定义错误消息(可选)
681
+ }
682
+ ```
683
+
684
+ #### 4.18.4. 使用示例
685
+
686
+ ##### 4.18.4.1. 基本用法(使用默认配置)
687
+
688
+ ```typescript
689
+ // .eslintrc.js
690
+ module.exports = {
691
+ rules: {
692
+ 'light/no-src-imports-in-components': 'error',
693
+ },
694
+ };
695
+ ```
696
+
697
+ 默认配置:
698
+
699
+ - `restrictedPaths`: `['src/components']`
700
+ - `bannedPrefixes`: `['src']`
701
+
702
+ ##### 4.18.4.2. 自定义配置
703
+
704
+ ```typescript
705
+ // .eslintrc.js
706
+ module.exports = {
707
+ rules: {
708
+ 'light/no-src-imports-in-components': ['error', {
709
+ // 指定多个需要限制的目录
710
+ restrictedPaths: ['src/components', 'src/lib', 'packages/ui'],
711
+
712
+ // 指定多个禁止的导入前缀
713
+ bannedPrefixes: ['src', '@/', '~/'],
714
+
715
+ // 可选:自定义错误消息
716
+ message: '该目录将被发布为独立包,请使用相对路径导入',
717
+ }],
718
+ },
719
+ };
720
+ ```
721
+
722
+ ##### 4.18.4.3. 只检查特定目录
723
+
724
+ ```typescript
725
+ // .eslintrc.js
726
+ module.exports = {
727
+ rules: {
728
+ 'light/no-src-imports-in-components': ['error', {
729
+ restrictedPaths: ['src/components'],
730
+ bannedPrefixes: ['src'],
731
+ }],
732
+ },
733
+ };
734
+ ```
735
+
736
+ #### 4.18.5. 错误示例
737
+
738
+ 假设配置为:
739
+
740
+ ```typescript
741
+ {
742
+ restrictedPaths: ['src/components'],
743
+ bannedPrefixes: ['src']
744
+ }
745
+ ```
746
+
747
+ 在 `src/components/Button/Button.tsx` 中:
748
+
749
+ ```typescript
750
+ // ❌ 错误:使用了 src 开头的导入
751
+ import { back } from 'src/app/route/route';
752
+ import { utils } from 'src/utils/helper';
753
+
754
+ // ✅ 正确:使用相对路径
755
+ import { back } from '../../app/route/route';
756
+ import { utils } from '../../utils/helper';
757
+
758
+ // ✅ 正确:导入 npm 包
759
+ import React from 'react';
760
+ import { Button } from 'antd';
761
+ ```
762
+
763
+ #### 4.18.6. 配置参数详解
764
+
765
+ ##### 4.18.6.1. restrictedPaths
766
+
767
+ 类型:`string[]`
768
+ 默认值:`['src/components']`
769
+
770
+ 指定需要应用此规则的目录列表。规则会检查文件路径是否包含这些目录。
771
+
772
+ 示例:
773
+
774
+ ```typescript
775
+ restrictedPaths: ['src/components', 'src/lib', 'packages/ui']
776
+ ```
777
+
778
+ ##### 4.18.6.2. bannedPrefixes
779
+
780
+ 类型:`string[]`
781
+ 默认值:`['src']`
782
+
783
+ 指定禁止使用的导入路径前缀列表。任何以这些前缀开头的导入都会被标记为错误。
784
+
785
+ 示例:
786
+
787
+ ```typescript
788
+ bannedPrefixes: ['src', '@/', '~/']
789
+ ```
790
+
791
+ ##### 4.18.6.3. message
792
+
793
+ 类型:`string`
794
+ 默认值:无(使用默认错误消息)
795
+
796
+ 自定义错误消息。如果不设置,将使用默认消息模板。
797
+
798
+ 示例:
799
+
800
+ ```typescript
801
+ message: '该目录将被发布为独立的 npm 包,请使用相对路径导入'
802
+ ```
803
+
804
+ ### 4.19. no-complex-style-class
805
+
806
+ 禁止在 Vue 模板的 `:style` 或 `:class` 绑定中**直接**使用函数调用、模板字符串等复杂表达式。
807
+
808
+ 背景:
809
+
810
+ > uni-app Vue2 小程序中,`:style` 和 `:class` 的**顶层表达式**不支持函数调用和模板字符串等语法。例如 `:style="tool._style(xxx)"` 或 `:class="\`${prefix}-item\`"` 在小程序中会解析失败。
811
+ >
812
+ > 应使用计算属性或字符串拼接 `'' + xxx` 代替。
813
+ >
814
+ > 注意:在数组元素、对象属性值、条件表达式分支中使用函数调用是**允许**的,例如 `:class="[getClass(), 'base']"` 和 `:style="flag ? getStyle() : ''"` 在小程序中可以正常工作。
815
+
816
+ Usage:
817
+
818
+ ```js
819
+ // .eslintrc.js
820
+
821
+ module.exports = {
822
+ plugins: [
823
+ 'light',
824
+ ],
825
+ rules: {
826
+ 'light/no-complex-style-class': 2,
827
+ },
828
+ }
829
+ ```
830
+
831
+ 错误示例:
832
+
833
+ ```html
834
+ <!-- ❌ 顶层函数调用 -->
835
+ <div :style="tool._style(xxx)" />
836
+ <div :class="getClass(item)" />
837
+
838
+ <!-- ❌ 顶层模板字符串 -->
839
+ <div :style="`color: ${color}`" />
840
+ <div :class="`${prefix}-item`" />
841
+ ```
842
+
843
+ 正确示例:
844
+
845
+ ```html
846
+ <!-- ✅ 变量引用 -->
847
+ <div :style="myStyle" />
848
+ <div :class="item.className" />
849
+
850
+ <!-- ✅ 对象表达式(值为变量) -->
851
+ <div :style="{ color: myColor }" />
852
+ <div :class="{ active: isActive }" />
853
+
854
+ <!-- ✅ 字面量数组 -->
855
+ <div :class="['a', 'b']" />
856
+
857
+ <!-- ✅ 条件表达式(值为变量) -->
858
+ <div :style="flag ? styleA : styleB" />
859
+
860
+ <!-- ✅ 数组元素中的函数调用 -->
861
+ <div :class="[utils.getClass(classPrefix, 'medium'), tClassImage]" />
862
+
863
+ <!-- ✅ 条件表达式分支中的函数调用 -->
864
+ <div :style="inChat ? imageStyle(item) : ''" />
865
+
866
+ <!-- ✅ 数组 + 条件混合 -->
867
+ <div :class="[classPrefix, inChat ? classPrefix + '--chatting' : '', getFileTypeClass(inChat, files)]" />
868
+
869
+ <!-- ✅ 对象属性值中的函数调用 -->
870
+ <div :style="{ color: getColor() }" />
871
+ ```
872
+
873
+ ## 5. 更新日志
416
874
 
417
875
  [点此查看](./CHANGELOG.md)
@@ -2,6 +2,7 @@ module.exports = {
2
2
  extends: require.resolve('./base'),
3
3
  rules: {
4
4
  'light/no-complex-key': 1,
5
+ 'light/no-complex-style-class': 1,
5
6
  'light/no-import-vant': 2,
6
7
  },
7
8
  };