eslint-plugin-light 1.0.15 → 1.0.17

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,15 @@
1
+ ## <small>1.0.17 (2025-07-23)</small>
2
+
3
+ * feat(eslint-plugin): add no-direct-img-src rule ([4223182](https://github.com/novlan1/plugin-light/commits/4223182))
4
+
5
+
6
+
7
+ ## <small>1.0.16 (2025-07-18)</small>
8
+
9
+ * feat(eslint): add no-todo-comment rule ([8b555b7](https://github.com/novlan1/plugin-light/commits/8b555b7))
10
+
11
+
12
+
1
13
  ## <small>1.0.15 (2025-06-16)</small>
2
14
 
3
15
  * feat(eslint-plugin-light): add img v-lazy ([5807b47](https://github.com/novlan1/plugin-light/commits/5807b47))
package/README.md CHANGED
@@ -414,7 +414,7 @@ module.exports = {
414
414
 
415
415
  ### 3.12. img-v-lazy
416
416
 
417
- 强制 `img` 标签使用 `v-lazy` 而不是 `:src`。
417
+ 强制 `img` 标签使用 `v-lazy` 而不是 `:src`,可用于 Vue 项目中。
418
418
 
419
419
  Usage:
420
420
 
@@ -431,6 +431,63 @@ module.exports = {
431
431
  }
432
432
  ```
433
433
 
434
+ ### 3.13. no-direct-img-src
435
+
436
+ 在 `jsx/tsx` 中禁止直接使用 `img src`,必须使用封装的 [`CdnImage` 组件](https://novlan1.github.io/docs/press-pix/zh-CN/cdn-image.html),可用于 React 项目中。
437
+
438
+ 与 [img-v-lazy 规则](#_3-12-img-v-lazy) 类似,都是为了防止直接使用 COS 图片,而不是 CDN 图片。
439
+
440
+ Usage:
441
+
442
+ ```js
443
+ // .eslintrc.js
444
+
445
+ module.exports = {
446
+ plugins: [
447
+ 'light',
448
+ ],
449
+ rules: {
450
+ 'light/no-direct-img-src': 2,
451
+
452
+ // 传入参数
453
+ 'light/no-direct-img-src': [2, {
454
+ componentName: 'MyCdnImage',
455
+ }],
456
+ },
457
+ }
458
+ ```
459
+
460
+ 配置选项:
461
+
462
+ | 字段 | 说明 | 默认值 |
463
+ | ------------- | ---------------------- | ---------- |
464
+ | componentName | 提示词中的自定义组件名 | `CdnImage` |
465
+
466
+ ### 3.14. no-todo-comment
467
+
468
+ 不允许存在 TODO。可防止调试代码被意外带到线上。
469
+
470
+ Usage:
471
+
472
+ ```js
473
+ // .eslintrc.js
474
+
475
+ module.exports = {
476
+ plugins: [
477
+ 'light',
478
+ ],
479
+ rules: {
480
+ 'light/no-todo-comment': 2,
481
+ },
482
+ }
483
+ ```
484
+
485
+ 配置选项:
486
+
487
+ | 字段 | 说明 | 默认值 |
488
+ | ------- | ---------- | ------- |
489
+ | keyword | 检查关键词 | `TODO:` |
490
+
434
491
  ## 4. 更新日志
435
492
 
436
493
  [点此查看](./CHANGELOG.md)
@@ -0,0 +1,43 @@
1
+ const DEFAULT_COMPONENT_NAME = 'CdnImage';
2
+
3
+ module.exports = {
4
+ meta: {
5
+ type: 'problem',
6
+ docs: {
7
+ description: '禁止直接使用 img src,必须使用封装的自定义组件',
8
+ category: 'Best Practices',
9
+ recommended: true,
10
+ },
11
+ fixable: 'code',
12
+ schema: [
13
+ {
14
+ type: 'object',
15
+ properties: {
16
+ componentName: {
17
+ type: 'string',
18
+ default: DEFAULT_COMPONENT_NAME,
19
+ },
20
+ },
21
+ additionalProperties: false,
22
+ },
23
+ ],
24
+ },
25
+ create(context) {
26
+ return {
27
+ JSXOpeningElement(node) {
28
+ if (node.name.name === 'img') {
29
+ const srcAttribute = node.attributes.find(attr => attr.type === 'JSXAttribute' && attr.name.name === 'src');
30
+ const options = context.options[0] || {};
31
+ const componentName = options.componentName ?? DEFAULT_COMPONENT_NAME;
32
+
33
+ if (srcAttribute) {
34
+ context.report({
35
+ node,
36
+ message: `禁止直接使用 img src,必须使用封装的 ${componentName} 组件`,
37
+ });
38
+ }
39
+ }
40
+ },
41
+ };
42
+ },
43
+ };
@@ -0,0 +1,42 @@
1
+ const DEFAULT_KEYWORD = 'TODO:';
2
+
3
+ module.exports = {
4
+ meta: {
5
+ type: 'problem',
6
+ docs: {
7
+ description: '禁止使用未规范的TODO注释',
8
+ category: 'Best Practices',
9
+ recommended: true,
10
+ },
11
+ schema: [
12
+ {
13
+ type: 'object',
14
+ properties: {
15
+ keyword: {
16
+ type: 'string',
17
+ default: DEFAULT_KEYWORD,
18
+ },
19
+ },
20
+ additionalProperties: false,
21
+ },
22
+ ],
23
+ },
24
+ create(context) {
25
+ const sourceCode = context.getSourceCode();
26
+ const comments = sourceCode.getAllComments();
27
+
28
+ comments.forEach((comment) => {
29
+ const options = context.options[0] || {};
30
+ const keyword = options.keyword ?? DEFAULT_KEYWORD;
31
+
32
+ if (comment.value.includes(keyword)) {
33
+ context.report({
34
+ node: comment,
35
+ message: '不允许 TODO 存在',
36
+ });
37
+ }
38
+ });
39
+
40
+ return {};
41
+ },
42
+ };
@@ -0,0 +1,39 @@
1
+ const { RuleTester } = require('eslint');
2
+
3
+ const rule = require('../rules/no-direct-img-src');
4
+
5
+
6
+ const ruleTester = new RuleTester({
7
+ parserOptions: {
8
+ ecmaVersion: 'latest',
9
+ sourceType: 'module',
10
+ ecmaFeatures: {
11
+ jsx: true,
12
+ },
13
+ },
14
+ });
15
+
16
+ const ERROR_MESSAGE = '禁止直接使用 img src,必须使用封装的 CdnImage 组件';
17
+
18
+
19
+ ruleTester.run('no-direct-img-src', rule, {
20
+ valid: [
21
+ {
22
+ code: '<CdnImage src="image.png" alt="example" />',
23
+ },
24
+ {
25
+ code: '<div><CdnImage src="image.jpg" /></div>',
26
+ },
27
+ ],
28
+ invalid: [
29
+ {
30
+ code: '<img src="image.png" alt="example" />',
31
+ errors: [{ message: ERROR_MESSAGE }],
32
+ },
33
+ {
34
+ code: '<img src={require("./image.jpg")} className="img" />',
35
+ errors: [{ message: ERROR_MESSAGE }],
36
+ },
37
+ ],
38
+ });
39
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-light",
3
- "version": "1.0.15",
3
+ "version": "1.0.17",
4
4
  "description": "Simple Eslint Plugin",
5
5
  "keywords": [
6
6
  "eslint",