scan-debug-skill 1.0.5 → 1.0.7

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/README.md CHANGED
@@ -1,26 +1,64 @@
1
- # scan-debug-skill
2
-
3
- 一套用于代码扫描与调试技能(Scan & Debug Skill)。
4
- 主要应用于sonar和奇安信的常见问题快速修复
5
-
6
- ## 使用方法
7
-
8
- ### 通过 npx 安装
9
-
10
- 在您的项目根目录下运行以下命令:
11
-
12
- ```bash
13
- npx scan-debug-skill
14
- ```
15
-
16
- 运行后,脚本会提示您选择安装目标:
17
-
18
- 1. **Trae**:安装到 `.trae/scan-debug-skill`
19
- 2. **Cursor**:安装到 `.cursor/scan-debug-skill`
20
- 3. **Custom**:安装到指定目录
21
-
22
- ### 包含的内容
23
-
24
- - CSS 最佳实践与规范
25
- - HTML 语义化与安全规范
26
- - JS/TS 编码规范与安全指南
1
+ # scan-debug-skill
2
+
3
+ 一套用于代码扫描与调试技能(Scan & Debug Skill)。
4
+ 主要应用于 SonarQube 和奇安信的常见问题快速修复。
5
+
6
+ A set of code scanning and debugging skills (Scan & Debug Skill).
7
+ Primarily used for quick fixes of common SonarQube and Qianxin issues.
8
+
9
+ ## 使用方法 / Usage
10
+
11
+ ### 通过 npx 安装 / Install via npx
12
+
13
+ 在您的项目根目录下运行以下命令:
14
+ Run the following command in your project root directory:
15
+
16
+ ```bash
17
+ npx scan-debug-skill
18
+ ```
19
+
20
+ 运行后,脚本会提示您选择安装目标:
21
+ After running, the script will prompt you to select an installation target:
22
+
23
+ 1. **Trae**:安装到 `.trae/scan-debug-skill` (Install to `.trae/scan-debug-skill`)
24
+ 2. **Cursor**:安装到 `.cursor/scan-debug-skill` (Install to `.cursor/scan-debug-skill`)
25
+ 3. **Custom**:安装到指定目录 (Install to a specified directory)
26
+
27
+ ### 包含的内容 / Contents
28
+
29
+ - CSS 最佳实践与规范 / CSS Best Practices & Standards
30
+ - HTML 语义化与安全规范 / HTML Semantics & Security Standards
31
+ - JS/TS 编码规范与安全指南 / JS/TS Coding Standards & Security Guidelines
32
+
33
+ ### AI 辅助编码 / AI Assisted Coding
34
+
35
+ 在编辑器中使用 AI 生成或修复代码时,可以通过以下提示词引用此规范:
36
+ When using AI to generate or fix code in the editor, you can reference this standard with the following prompts:
37
+
38
+ - "请根据 `scan-debug-skill` 规范修复当前文件的 Sonar 问题"
39
+ "Please fix Sonar issues in the current file according to `scan-debug-skill` standards"
40
+ - "生成一段 API 请求代码,要求符合 `scan-debug-skill` 中的 JS 安全规范"
41
+ "Generate API request code that complies with the JS security standards in `scan-debug-skill`"
42
+
43
+ ### 项目规则引用 / Project Rules Reference
44
+
45
+ 您可以在 `.cursorrules` 或 `.trae/rules` 文件中添加以下规则,以确保 AI 始终遵循此规范:
46
+ You can add the following rules to your `.cursorrules` or `.trae/rules` file to ensure AI always follows this standard:
47
+ ```
48
+ - When fixing issues, refer to the specific rules in the `scan-debug-skill` documentation.
49
+ ```
50
+
51
+ ## 注意事项 / Notes
52
+
53
+ ⚠️ **资源消耗提醒 (Resource Consumption Warning)**
54
+
55
+ 对整个项目进行全量扫描并自动修复可能会消耗大量的 AI 编码请求次数(Tokens)。
56
+ Scanning and automatically fixing the entire project may consume a large amount of AI coding requests (Tokens).
57
+
58
+ **建议做法 (Recommended Practice)**:
59
+ 1. **按需引用**:在生成新代码时,直接引用本技能规范(如上文 "AI 辅助编码" 所示),从源头保证代码质量。
60
+ 2. **局部修复**:针对单个文件或特定文件夹进行扫描和修复,避免全量操作,以节约资源。
61
+
62
+ **Recommendation**:
63
+ 1. **Reference on Demand**: When generating new code, directly reference this skill standard (as shown in "AI Assisted Coding" above) to ensure code quality from the start.
64
+ 2. **Partial Fix**: Scan and fix specific files or folders instead of the entire project to save resources.
package/SKILL.md CHANGED
@@ -22,8 +22,8 @@ author: [scan-debug-skill](https://github.com/k73333333/ScanDebugSkill)
22
22
  ## 目录结构
23
23
 
24
24
  * **[JS/TS 修复技能](./js/SKILL.md)**
25
- * 包含 JavaScript、TypeScript、Vue、React 的修复规则。
26
- * 涉及:注释规范、最佳实践、框架规范、安全合规。
25
+ * 包含 JavaScript、TypeScript 的修复规则。
26
+ * 涉及:注释规范、最佳实践、安全合规。
27
27
 
28
28
  * **[CSS 修复技能](./css/SKILL.md)**
29
29
  * 包含 CSS、SASS、LESS 的样式修复规则。
@@ -38,6 +38,17 @@ author: [scan-debug-skill](https://github.com/k73333333/ScanDebugSkill)
38
38
 
39
39
  请点击上述链接进入相应的技能目录查看详细指南。
40
40
 
41
+ ### 引用指南
42
+
43
+ 在编辑器中使用 AI 助手生成或修复代码时,可以通过以下方式引用本规范:
44
+
45
+ 1. **代码生成**:在生成代码时,显式要求遵循 `scan-debug-skill` 规范。
46
+ > "生成一段 API 请求代码,请遵循 `scan-debug-skill` 中的 JS 安全规范。"
47
+ 2. **问题修复**:在修复代码问题时,要求 AI 参考本技能集。
48
+ > "请按照 `scan-debug-skill` 修复当前代码的 Sonar 问题。"
49
+ 3. **项目规则配置**:在项目的 `.cursorrules` 或 `.trae/rules` 文件中配置全局引用,使 AI 默认遵循此规范。
50
+ > "Follow the `scan-debug-skill` standards for all code generation and fixes."
51
+
41
52
  ## 扫描结果概览输出规则
42
53
 
43
54
  当用户提供扫描报告或提及多个代码问题时,应首先以表格形式输出扫描结果概览,遵循以下规范:
package/bin/install.js CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  /*
4
- * @Author: fukaidong fukaidong@aspirecn.com
4
+ * @Author: fukaidong
5
5
  * @Date: 2026-02-25 15:50:10
6
- * @LastEditors: fukaidong fukaidong@aspirecn.com
7
- * @LastEditTime: 2026-02-25 16:15:00
6
+ * @LastEditors: fukaidong qiji777@yeah.net
7
+ * @LastEditTime: 2026-02-26 18:33:02
8
8
  * @Description: scan-debug-skill 安装脚本 (ScanDebugSkill Install Script)
9
9
  * 包含更新处理逻辑:安装前清理旧版本核心目录,确保无残留文件。
10
10
  */
@@ -6,6 +6,24 @@
6
6
  * **单位统一**:
7
7
  * 当值为 `0` 时,省略单位(如 `0px` -> `0`)。
8
8
  * 避免使用绝对单位(如 `cm`, `in`),除非在打印样式表中。
9
+ * **命名规范**:推荐使用 **BEM (Block Element Modifier)** 命名规则,提高样式的可维护性和复用性。
10
+ * Block: `.block`
11
+ * Element: `.block__element`
12
+ * Modifier: `.block--modifier`
13
+
14
+ ## 示例:BEM 命名规范
15
+ **场景**:样式类名杂乱,层级嵌套过深,难以维护。
16
+
17
+ **修复后代码**:
18
+ ```css
19
+ /* Non-Compliant: 嵌套过深,命名无结构 */
20
+ .list .item .active { ... }
21
+
22
+ /* Compliant: BEM 命名 */
23
+ .user-list { } /* Block */
24
+ .user-list__item { } /* Element */
25
+ .user-list__item--active { } /* Modifier */
26
+ ```
9
27
 
10
28
  ## 示例:省略零值单位
11
29
  **场景**:Sonar 提示 "Unexpected unit"。
package/js/SKILL.md CHANGED
@@ -19,9 +19,7 @@ description: JavaScript/TypeScript 代码 SonarQube 扫描问题修复指南,
19
19
  ### 基础规范
20
20
  - 注释与文档规范 (JSDoc, 详细逻辑注释, 变量注释) → 详见 [comments-documentation](reference/comments-documentation.md)
21
21
  - JS/TS 通用最佳实践 (ES6 Class, 异步处理, 日志规范) → 详见 [js-ts-best-practices](reference/js-ts-best-practices.md)
22
-
23
- ### 框架规范
24
- - Vue/React 框架特定规范 (架构分离, List Key, Props, Side Effects) → 详见 [framework-vue-react](reference/framework-vue-react.md)
22
+ - 代码命名与工程规范 (目录命名, 变量命名, Hooks) → 详见 [naming-conventions](reference/naming-conventions.md)
25
23
 
26
24
  ### 安全合规
27
25
  - 安全漏洞与合规性 (XSS, 敏感信息, 堆栈泄露) → 详见 [security-compliance](reference/security-compliance.md)
@@ -19,6 +19,16 @@
19
19
  * **魔法数字 (Magic Numbers)**:避免直接使用无意义的数字(0 和 1 除外),应将其定义为具名常量。
20
20
  * **冗余条件判断**:当 `if` 和 `else` 分支执行逻辑完全一致时,应移除条件判断,直接执行公共逻辑。
21
21
  * **空值检查 (Null Safety)**:在访问对象的深层属性前,必须进行非空检查,推荐使用可选链操作符 (`?.`) 和空值合并操作符 (`??`),避免 "Cannot read property of undefined" 错误。
22
+ * **性能优化**:
23
+ * **正则表达式预编译**:避免在循环内创建 `RegExp` 实例,应提取到循环外部或模块作用域。
24
+ * **避免嵌套循环**:3 层以上的嵌套循环(O(n³))必须优化,建议使用 `flatMap` 或 Map 索引。
25
+ * **字符串拼接**:循环内避免使用 `+=` 拼接大量字符串,应使用数组 `push` 后 `join`。
26
+ * **异常处理**:
27
+ * **禁止 Finally Return**:`finally` 块中严禁使用 `return`,否则会覆盖 `try/catch` 中的异常抛出。
28
+ * **流程控制**:禁止使用异常(try-catch)来处理正常的业务流程控制(如判断 JSON 格式,应先用正则预判)。
29
+ * **状态管理**:
30
+ * **状态回滚**:前端乐观更新(Optimistic UI)时,若请求失败必须回滚状态。
31
+ * **避免全局锁**:避免使用单一的全局 `loading` 状态控制并发请求,应使用独立的 loading 变量。
22
32
 
23
33
  ## 示例:认知复杂度与魔法数字
24
34
 
@@ -104,3 +114,42 @@ function processOrders(orders) {
104
114
  }
105
115
  }
106
116
  ```
117
+
118
+ ## 示例:性能优化与异常处理
119
+
120
+ **场景**:Sonar 提示 "Regular expressions should not be created in a loop" 或 "Jump statements should not occur in finally blocks"。
121
+
122
+ **修复后代码**:
123
+ ```javascript
124
+ // 1. 正则预编译
125
+ // Non-Compliant: 循环内重复创建正则
126
+ // items.forEach(item => { if (new RegExp('^\\d{4}').test(item)) ... })
127
+
128
+ // Compliant: 提取为常量
129
+ const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
130
+ items.forEach(item => {
131
+ if (DATE_PATTERN.test(item.date)) { /* ... */ }
132
+ });
133
+
134
+ // 2. 字符串拼接优化
135
+ // Non-Compliant: 使用 += 拼接
136
+ // let html = ""; list.forEach(i => html += `<li>${i}</li>`);
137
+
138
+ // Compliant: 使用 Array.join
139
+ const html = list.map(i => `<li>${i}</li>`).join("");
140
+
141
+ // 3. 禁止 Finally Return
142
+ async function submitData() {
143
+ try {
144
+ await api.post('/submit');
145
+ } catch (error) {
146
+ console.error('提交失败', error);
147
+ return Promise.reject(error);
148
+ } finally {
149
+ // Non-Compliant: return true; // 这会吞掉上面的 reject
150
+
151
+ // Compliant: 仅做清理工作
152
+ this.loading = false;
153
+ }
154
+ }
155
+ ```
@@ -0,0 +1,78 @@
1
+ # 代码命名与工程规范
2
+
3
+ ## 规则详情
4
+
5
+ * **目录与文件命名**:
6
+ * 统一使用 `kebab-case`(短横线连接),例如 `src/components/user-list.js`、`src/hooks/use-user-info.ts`。
7
+ * 组件/类文件可使用 `PascalCase`(大驼峰)或 `kebab-case`,但项目中应保持统一。
8
+ * **组件/类命名**:
9
+ * 文件名:`UserList.ts` 或 `user-list.ts`。
10
+ * 类名:`UserList` (PascalCase)。
11
+ * 使用时:`new UserList()` (PascalCase)。
12
+ * **变量与函数命名**:
13
+ * 变量/函数:使用 `camelCase`(小驼峰),例如 `userInfo`, `fetchData`。
14
+ * 常量:使用 `UPPER_CASE`(全大写下划线),例如 `MAX_COUNT`, `API_BASE_URL`。
15
+ * **方法名**:必须使用动宾结构,例如 `getUserList` (获取), `handleSubmit` (处理), `isFinished` (判断)。
16
+ * **布尔变量**:推荐以 `is`, `has`, `should` 开头,例如 `isVisible`, `hasError`。
17
+ * **类型与接口命名**:
18
+ * 统一使用 `PascalCase`(大驼峰),例如 `interface UserInfo {}`, `type ApiResponse = {}`。
19
+ * 不要使用下划线命名法(如 `user_info`)。
20
+ * **Hooks 命名**:
21
+ * 文件名:`use-auth.ts` (kebab-case)。
22
+ * 导出函数:`useAuth` (camelCase,以 use 开头)。
23
+
24
+ ## 示例:命名规范
25
+
26
+ **场景**:Sonar 提示 "Rename this file to match the regular expression..." 或 "Identifier 'xxx' does not match usage"。
27
+
28
+ **修复后代码**:
29
+ ```typescript
30
+ // 文件名: src/hooks/use-user-info.ts
31
+
32
+ // Non-Compliant: 接口命名不规范
33
+ // interface user_info {}
34
+
35
+ // Compliant: 接口使用 PascalCase
36
+ export interface UserInfo {
37
+ id: number;
38
+ name: string;
39
+ }
40
+
41
+ // Non-Compliant: 函数名无动宾结构
42
+ // function data() {}
43
+
44
+ // Compliant: 函数名动宾结构,变量小驼峰
45
+ export function useUserInfo() {
46
+ const isLoading = ref(false);
47
+
48
+ async function fetchUserData() {
49
+ // ...
50
+ }
51
+
52
+ return {
53
+ isLoading,
54
+ fetchUserData
55
+ };
56
+ }
57
+ ```
58
+
59
+ ## 示例:目录结构分层
60
+
61
+ **场景**:代码耦合度高,API 请求散落在组件中。
62
+
63
+ **修复后代码**:
64
+ ```javascript
65
+ // ❌ Non-Compliant: API 请求写在组件/UI层内部
66
+ // src/components/UserList.ts
67
+ const res = await axios.post("/api/user/list", data);
68
+
69
+ // ✅ Compliant: API 调用集中在 src/api/,UI层只调用服务
70
+ // src/api/user.ts
71
+ export function fetchUserList(data) {
72
+ return request.post('/api/user/list', data);
73
+ }
74
+
75
+ // src/components/UserList.ts
76
+ import { fetchUserList } from "@/api/user";
77
+ const res = await fetchUserList(data);
78
+ ```
@@ -3,7 +3,7 @@
3
3
  ## 规则详情
4
4
  * **XSS 防范**:
5
5
  * 严禁使用 `eval()`、`new Function()`。
6
- * 慎用 `v-html` (Vue) `dangerouslySetInnerHTML` (React),必须确保内容已经过 DOMPurify 等库的清洗。
6
+ * 处理用户输入的 HTML 内容时,必须确保内容已经过 DOMPurify 等库的清洗,避免直接插入未过滤的 HTML。
7
7
  * **敏感信息**:代码中禁止硬编码密码、Token、密钥(AK/SK)、内网 IP 等敏感信息,应通过环境变量或配置接口获取。
8
8
  * **日志安全**:
9
9
  * 禁止在生产环境日志中输出敏感信息(如密码、Token 等)。
@@ -23,7 +23,7 @@
23
23
 
24
24
  // Compliant: 使用环境变量
25
25
  // 说明:确保环境变量在构建或运行时已正确注入
26
- const API_KEY = process.env.VUE_APP_API_KEY;
26
+ const API_KEY = process.env.API_KEY;
27
27
  ```
28
28
 
29
29
  ## 示例:日志安全与异常处理
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "scan-debug-skill",
3
- "version": "1.0.5",
4
- "description": "一套用代码扫描与调试技能(Scan & Debug Skill)。主要应用于sonar和奇安信的常见问题快速修复",
3
+ "version": "1.0.7",
4
+ "description": "A set of code scanning and debugging skills, primarily for quick fixes of common SonarQube and Qianxin issues. | 一套代码扫描与调试技能,主要应用于 SonarQube 和奇安信常见问题的快速修复。",
5
5
  "bin": {
6
6
  "scan-debug-skill": "./bin/install.js"
7
7
  },
@@ -12,7 +12,10 @@
12
12
  "trae",
13
13
  "skill",
14
14
  "debug",
15
- "scan"
15
+ "scan",
16
+ "sonar 扫描和问题修复",
17
+ "奇安信 扫描和问题修复",
18
+ "代码扫描修复skill"
16
19
  ],
17
20
  "author": "",
18
21
  "license": "MIT",
@@ -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
- ```