scan-debug-skill 1.0.6 → 1.0.8

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.
@@ -1,80 +0,0 @@
1
- # 前端框架规范 (Vue/React)
2
-
3
- ## 规则详情
4
- * **架构分离 (Architecture Separation)**:
5
- * **业务逻辑抽离**:复杂的业务逻辑(如数据处理、API 请求组合)必须从组件中抽离,封装到独立的 `Service` 类或工具函数中(此为建议非强制)。
6
- * **组件职责**:UI 组件应仅负责视图渲染、用户交互响应和简单的状态管理(此为建议非强制)。
7
- * **列表渲染**:`v-for` (Vue) 或 `map` (React) 循环渲染时,必须提供唯一的 `key`,且**避免使用数组索引 (index) 作为 key**(除非列表是静态且不排序的)。
8
- * **Props 验证**:组件接收的 Props 必须定义类型验证和默认值。
9
- * **副作用清理**:在组件销毁生命周期(`beforeUnmount`, `useEffect cleanup`)中,必须清理定时器、事件监听器等副作用。
10
-
11
- ## 示例:架构分离与副作用清理
12
-
13
- **场景**:组件内包含了大量的数据获取和处理逻辑,且使用了定时器。
14
-
15
- **修复后代码 (React 示例)**:
16
-
17
- ```javascript
18
- // 1. 业务逻辑抽离 (UserService.js)
19
- import { api } from './api';
20
-
21
- export class UserService {
22
- /**
23
- * 轮询获取用户状态
24
- * @param {string} userId - 用户ID
25
- * @returns {Promise<Object>} - 用户状态
26
- */
27
- static async fetchStatus(userId) {
28
- try {
29
- return await api.get(`/user/${userId}/status`);
30
- } catch (error) {
31
- console.error('UserService: 获取状态失败');
32
- return null;
33
- }
34
- }
35
- }
36
-
37
- // 2. 组件逻辑 (UserStatus.jsx)
38
- import React, { useEffect, useState } from 'react';
39
- import { UserService } from './UserService';
40
-
41
- export function UserStatus({ userId }) {
42
- const [status, setStatus] = useState(null);
43
-
44
- useEffect(() => {
45
- let timerId = null;
46
-
47
- const loadData = async () => {
48
- // 调用抽离的业务逻辑
49
- const data = await UserService.fetchStatus(userId);
50
- if (data) setStatus(data);
51
- };
52
-
53
- // 初始加载
54
- loadData();
55
-
56
- // 设置轮询
57
- timerId = setInterval(loadData, 5000);
58
-
59
- // Compliant: 清理副作用
60
- return () => {
61
- if (timerId) clearInterval(timerId);
62
- };
63
- }, [userId]);
64
-
65
- return <div>状态: {status}</div>;
66
- }
67
- ```
68
-
69
- ## 示例:Vue/React 列表 Key 问题
70
-
71
- **场景**:Sonar 提示 "Use a unique value for the key attribute"。
72
-
73
- **修复后代码**:
74
- ```html
75
- <!-- Compliant: 使用数据的唯一 ID 作为 key -->
76
- <div v-for="item in list" :key="item.id">
77
- <!-- 显示用户名称 -->
78
- {{ item.name }}
79
- </div>
80
- ```