mta-mcp 1.0.0
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 +818 -0
- package/agents/_TEMPLATE.md +153 -0
- package/agents/flutter.agent.md +222 -0
- package/agents/i18n.agent.md +78 -0
- package/agents/logicflow.agent.md +97 -0
- package/agents/vue3.agent.md +176 -0
- package/agents/wechat-miniprogram.agent.md +89 -0
- package/bin/mta.cjs +132 -0
- package/common/i18n.md +385 -0
- package/common/typescript-strict.md +186 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +6493 -0
- package/dist/index.js.map +1 -0
- package/package.json +81 -0
- package/standards/README.md +194 -0
- package/standards/core/code-generation.md +421 -0
- package/standards/core/code-style.md +308 -0
- package/standards/core/dart-base.md +572 -0
- package/standards/core/mandatory-rules.md +103 -0
- package/standards/core/typescript-base.md +179 -0
- package/standards/frameworks/flutter-ui-system.md +497 -0
- package/standards/frameworks/flutter.md +1268 -0
- package/standards/frameworks/pinia.md +172 -0
- package/standards/frameworks/vue3-composition.md +779 -0
- package/standards/frameworks/wechat-miniprogram.md +2177 -0
- package/standards/libraries/element-plus.md +1128 -0
- package/standards/libraries/i18n.md +360 -0
- package/standards/libraries/logicflow.md +1007 -0
- package/standards/patterns/api-layer.md +187 -0
- package/standards/patterns/component-design.md +200 -0
- package/standards/patterns/design-system-restoration.md +570 -0
- package/standards/patterns/vue-api-mock-layer.md +958 -0
- package/standards/patterns/vue-css-nesting.md +604 -0
- package/standards/troubleshooting-cases/flutter/textfield-vertical-centering.md +107 -0
- package/standards/workflows/design-restoration-guide.md +164 -0
- package/standards/workflows/large-project-split.md +359 -0
- package/standards/workflows/problem-diagnosis.md +280 -0
- package/standards/workflows/textfield-centering-guide.md +157 -0
- package/templates/README.md +144 -0
- package/templates/common/types/_CONFIG.md +12 -0
- package/templates/common/types/api.ts +39 -0
- package/templates/common/types/common.ts +70 -0
- package/templates/config-templates/agents-section.md +9 -0
- package/templates/config-templates/custom-section.md +6 -0
- package/templates/config-templates/header.md +29 -0
- package/templates/config-templates/workflow-minimal.md +44 -0
- package/templates/copilot-instructions-mcp-optimized.md +158 -0
- package/templates/vue/api-layer/_CONFIG.md +145 -0
- package/templates/vue/api-layer/index.ts +58 -0
- package/templates/vue/api-layer/mock/index.ts +122 -0
- package/templates/vue/api-layer/modules/_template.ts +109 -0
- package/templates/vue/api-layer/modules/index.ts +16 -0
- package/templates/vue/api-layer/request.ts +279 -0
- package/templates/vue/api-layer/types.ts +80 -0
- package/troubleshooting/README.md +368 -0
- package/troubleshooting/USAGE_GUIDE.md +289 -0
- package/troubleshooting/flutter/clip-/351/230/264/345/275/261/350/243/201/345/211/252.md +244 -0
- package/troubleshooting/flutter/component-/351/200/232/347/224/250/345/214/226/346/217/220/345/217/226.md +269 -0
- package/troubleshooting/flutter/input-/345/255/227/346/256/265/347/274/272/345/244/261.md +240 -0
- package/troubleshooting/flutter/input-/350/276/271/346/241/206/351/227/256/351/242/230.md +236 -0
- package/troubleshooting/flutter/layout-/345/260/272/345/257/270/344/270/215/345/214/271/351/205/215.md +214 -0
- package/troubleshooting/flutter/shadow-/351/200/217/345/207/272/351/227/256/351/242/230.md +172 -0
- package/troubleshooting/flutter/sketch-/345/210/227/350/241/250item/345/214/272/345/237/237.md +212 -0
- package/troubleshooting/flutter/sketch-/345/233/276/346/240/207/345/260/272/345/257/270.md +135 -0
- package/troubleshooting/flutter/sketch-/345/256/214/346/225/264/346/217/220/345/217/226.md +201 -0
- package/troubleshooting/flutter/sketch-/345/261/236/346/200/247/346/234/252/344/275/277/347/224/250.md +139 -0
- package/troubleshooting/flutter/sketch-/350/203/214/346/231/257/345/261/202/351/253/230/345/272/246.md +264 -0
- package/troubleshooting/flutter/svg-/346/234/252/345/261/205/344/270/255.md +120 -0
- package/troubleshooting/flutter/svg-/351/242/234/350/211/262/345/274/202/345/270/270.md +117 -0
- package/troubleshooting/flutter/tabbar-/345/212/250/347/224/273/345/220/214/346/255/245.md +107 -0
- package/troubleshooting/flutter/withopacity-/345/274/203/347/224/250.md +81 -0
- package/troubleshooting/vue3/cascader-/350/257/257/346/233/277/346/215/242.md +130 -0
- package/troubleshooting/vue3/drawer-input-/346/240/267/345/274/217.md +181 -0
- package/troubleshooting/vue3/table-/347/274/226/350/276/221/345/217/226/346/266/210.md +148 -0
- package/troubleshooting/vue3/table-/350/276/271/346/241/206/351/227/256/351/242/230.md +178 -0
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mta-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MTA - 智能项目分析与编码规范管理 MCP 服务器",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"mta": "./bin/mta.cjs",
|
|
16
|
+
"mta-mcp": "./bin/mta.cjs"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"bin/",
|
|
20
|
+
"dist/",
|
|
21
|
+
"standards/",
|
|
22
|
+
"agents/",
|
|
23
|
+
"common/",
|
|
24
|
+
"templates/",
|
|
25
|
+
"troubleshooting/",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"sync": "bash scripts/sync-resources.sh",
|
|
30
|
+
"build": "npm run sync && tsup",
|
|
31
|
+
"watch": "tsup --watch",
|
|
32
|
+
"start": "node bin/mta.js",
|
|
33
|
+
"dev": "tsup --watch",
|
|
34
|
+
"prepare": "npm run build",
|
|
35
|
+
"clean": "rm -rf dist build",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:watch": "vitest",
|
|
38
|
+
"test:coverage": "vitest run --coverage",
|
|
39
|
+
"prepublishOnly": "npm run sync && npm run build && npm test"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"mcp",
|
|
43
|
+
"model-context-protocol",
|
|
44
|
+
"copilot",
|
|
45
|
+
"github-copilot",
|
|
46
|
+
"claude",
|
|
47
|
+
"ai",
|
|
48
|
+
"agents",
|
|
49
|
+
"coding-standards",
|
|
50
|
+
"project-analysis",
|
|
51
|
+
"typescript",
|
|
52
|
+
"vue",
|
|
53
|
+
"react"
|
|
54
|
+
],
|
|
55
|
+
"author": "ForLear",
|
|
56
|
+
"license": "MIT",
|
|
57
|
+
"homepage": "https://github.com/ForLear/copilot-prompts#readme",
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/ForLear/copilot-prompts/issues"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@modelcontextprotocol/sdk": "^1.24.3",
|
|
63
|
+
"axios": "^1.6.5",
|
|
64
|
+
"fast-glob": "^3.3.2"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"@types/node": "^20.11.0",
|
|
68
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
69
|
+
"tsup": "^8.5.1",
|
|
70
|
+
"typescript": "^5.3.3",
|
|
71
|
+
"vitest": "^4.0.16"
|
|
72
|
+
},
|
|
73
|
+
"engines": {
|
|
74
|
+
"node": ">=16.0.0"
|
|
75
|
+
},
|
|
76
|
+
"repository": {
|
|
77
|
+
"type": "git",
|
|
78
|
+
"url": "https://github.com/ForLear/copilot-prompts.git",
|
|
79
|
+
"directory": "mcp-server"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# 规范目录索引
|
|
2
|
+
|
|
3
|
+
## ⚠️ 编写新规范的强制要求
|
|
4
|
+
|
|
5
|
+
**创建任何新的标准规范文件前,必须:**
|
|
6
|
+
|
|
7
|
+
1. **加载现有规范** - 先查看同类规范的编写方式
|
|
8
|
+
```
|
|
9
|
+
get_relevant_standards({ scenario: "规范编写" })
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
2. **确保每个规范文件包含强制工作流说明**
|
|
13
|
+
- 在文档开头添加 `## ⚠️ 强制工作流` 章节
|
|
14
|
+
- 说明何时必须先调用 `get_relevant_standards`
|
|
15
|
+
- 提供具体的调用示例
|
|
16
|
+
|
|
17
|
+
3. **验证规范结构**
|
|
18
|
+
- 包含清晰的使用场景说明
|
|
19
|
+
- 提供正确/错误示例对比
|
|
20
|
+
- 标注 Token 大小和预计加载时间
|
|
21
|
+
|
|
22
|
+
**示例模板:**
|
|
23
|
+
```markdown
|
|
24
|
+
# 新框架规范
|
|
25
|
+
|
|
26
|
+
## ⚠️ 强制工作流
|
|
27
|
+
**在使用此框架编写代码前,必须调用:**
|
|
28
|
+
\`\`\`
|
|
29
|
+
get_relevant_standards({ imports: ["framework-name"] })
|
|
30
|
+
\`\`\`
|
|
31
|
+
|
|
32
|
+
## 核心原则
|
|
33
|
+
...
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 📁 目录结构
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
standards/
|
|
42
|
+
├── core/ # 核心规范(总是包含)
|
|
43
|
+
│ ├── code-style.md # 代码风格规范
|
|
44
|
+
│ └── typescript-base.md # TypeScript 基础
|
|
45
|
+
│
|
|
46
|
+
├── frameworks/ # 框架规范(按需加载)
|
|
47
|
+
│ ├── vue3-composition.md # Vue 3 Composition API
|
|
48
|
+
│ └── pinia.md # Pinia 状态管理
|
|
49
|
+
│
|
|
50
|
+
├── libraries/ # 库规范(按需加载)
|
|
51
|
+
│ ├── element-plus.md # Element Plus
|
|
52
|
+
│ └── i18n.md # 国际化
|
|
53
|
+
│
|
|
54
|
+
├── patterns/ # 设计模式(智能推荐)
|
|
55
|
+
│ ├── api-layer.md # API 层设计
|
|
56
|
+
│ └── component-design.md # 组件设计模式
|
|
57
|
+
│
|
|
58
|
+
└── workflows/ # 工作流规范(场景触发)
|
|
59
|
+
└── large-project-split.md # 大型项目文档拆分
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## 🎯 规范分类
|
|
63
|
+
|
|
64
|
+
### Core (核心规范)
|
|
65
|
+
始终包含的基础规范
|
|
66
|
+
|
|
67
|
+
| 文件 | 大小 | 说明 |
|
|
68
|
+
|------|------|------|
|
|
69
|
+
| code-style.md | ~2KB | 命名、注释、代码组织 |
|
|
70
|
+
| typescript-base.md | ~3KB | TS 基础类型、函数、泛型 |
|
|
71
|
+
| dart-base.md | ~5KB | Dart 基础、空安全、异步编程 |
|
|
72
|
+
|
|
73
|
+
### Frameworks (框架规范)
|
|
74
|
+
根据项目框架按需加载
|
|
75
|
+
|
|
76
|
+
| 文件 | 技术栈 | 触发条件 |
|
|
77
|
+
|------|--------|----------|
|
|
78
|
+
| vue3-composition.md | Vue 3 | import vue |
|
|
79
|
+
| pinia.md | Pinia | import pinia |
|
|
80
|
+
| flutter.md | Flutter | import flutter, .dart 文件 |
|
|
81
|
+
| wechat-miniprogram.md | 微信小程序 | import wx, .js/.wxml/.wxss 文件 |
|
|
82
|
+
|
|
83
|
+
### Libraries (库规范)
|
|
84
|
+
根据使用的库按需加载
|
|
85
|
+
|
|
86
|
+
| 文件 | 库名称 | 触发条件 |
|
|
87
|
+
|------|--------|----------|
|
|
88
|
+
| element-plus.md | Element Plus | import element-plus |
|
|
89
|
+
| i18n.md | Vue I18n | import vue-i18n |
|
|
90
|
+
|
|
91
|
+
### Patterns (设计模式)
|
|
92
|
+
根据任务类型智能推荐
|
|
93
|
+
|
|
94
|
+
| 文件 | 场景 | 触发词 |
|
|
95
|
+
|------|------|--------|
|
|
96
|
+
| api-layer.md | API 调用 | API, axios, request |
|
|
97
|
+
| component-design.md | 组件设计 | component, props, emit |
|
|
98
|
+
| vue-css-nesting.md | Vue CSS 嵌套写法 | style, css, scoped |
|
|
99
|
+
| vue-api-mock-layer.md | Vue API Mock 层 | mock, api |
|
|
100
|
+
|
|
101
|
+
### Workflows (工作流规范)
|
|
102
|
+
根据项目规模和场景触发
|
|
103
|
+
|
|
104
|
+
| 文件 | 场景 | 触发条件 |
|
|
105
|
+
|------|------|----------|
|
|
106
|
+
| large-project-split.md | 大型项目文档拆分 | 页面数>30, 模块>5, 原型分析 |
|
|
107
|
+
| problem-diagnosis.md | 问题诊断与修复 | 修复 Bug、样式问题、异常排查 |
|
|
108
|
+
|
|
109
|
+
## 🔧 使用示例
|
|
110
|
+
|
|
111
|
+
### 场景 1: 创建 Vue 3 组件
|
|
112
|
+
**加载规范:**
|
|
113
|
+
- core/code-style.md
|
|
114
|
+
- core/typescript-base.md
|
|
115
|
+
- frameworks/vue3-composition.md
|
|
116
|
+
- patterns/component-design.md
|
|
117
|
+
|
|
118
|
+
**预计 Token:** ~8KB (vs 传统 20KB)
|
|
119
|
+
|
|
120
|
+
### 场景 2: Pinia Store
|
|
121
|
+
**加载规范:**
|
|
122
|
+
- core/typescript-base.md
|
|
123
|
+
- frameworks/vue3-composition.md
|
|
124
|
+
- frameworks/pinia.md
|
|
125
|
+
|
|
126
|
+
**预计 Token:** ~6KB (vs 传统 20KB)
|
|
127
|
+
|
|
128
|
+
### 场景 3: Element Plus 表单
|
|
129
|
+
**加载规范:**
|
|
130
|
+
- core/code-style.md
|
|
131
|
+
- frameworks/vue3-composition.md
|
|
132
|
+
- libraries/element-plus.md
|
|
133
|
+
- libraries/i18n.md
|
|
134
|
+
|
|
135
|
+
**预计 Token:** ~9KB (vs 传统 20KB)
|
|
136
|
+
|
|
137
|
+
## 📊 Token 节省对比
|
|
138
|
+
|
|
139
|
+
| 传统方式 | MCP 方案 | 节省 |
|
|
140
|
+
|---------|---------|------|
|
|
141
|
+
| 20KB (全量) | 6-10KB (按需) | 50-70% |
|
|
142
|
+
|
|
143
|
+
## 🎨 匹配规则
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// 规范匹配配置
|
|
147
|
+
export const standardsMapping = {
|
|
148
|
+
// 框架检测
|
|
149
|
+
frameworks: {
|
|
150
|
+
vue3: {
|
|
151
|
+
imports: ['vue', '@vue'],
|
|
152
|
+
files: ['.vue'],
|
|
153
|
+
standards: ['vue3-composition']
|
|
154
|
+
},
|
|
155
|
+
pinia: {
|
|
156
|
+
imports: ['pinia'],
|
|
157
|
+
standards: ['pinia']
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
// 库检测
|
|
162
|
+
libraries: {
|
|
163
|
+
'element-plus': {
|
|
164
|
+
imports: ['element-plus'],
|
|
165
|
+
standards: ['element-plus']
|
|
166
|
+
},
|
|
167
|
+
'vue-i18n': {
|
|
168
|
+
imports: ['vue-i18n'],
|
|
169
|
+
standards: ['i18n']
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
// 模式检测
|
|
174
|
+
patterns: {
|
|
175
|
+
api: {
|
|
176
|
+
keywords: ['api', 'axios', 'request', 'fetch'],
|
|
177
|
+
standards: ['api-layer']
|
|
178
|
+
},
|
|
179
|
+
component: {
|
|
180
|
+
keywords: ['component', 'props', 'emit'],
|
|
181
|
+
standards: ['component-design']
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
// 工作流检测
|
|
186
|
+
workflows: {
|
|
187
|
+
'large-project': {
|
|
188
|
+
keywords: ['原型', '设计稿', '页面拆分', '模块规划', '开发计划', '大型项目'],
|
|
189
|
+
conditions: ['页面数>30', '模块>5'],
|
|
190
|
+
standards: ['large-project-split']
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
# 代码生成规范(v1.3.0 更新)
|
|
2
|
+
|
|
3
|
+
## 核心原则
|
|
4
|
+
|
|
5
|
+
生成代码时必须遵循以下原则:
|
|
6
|
+
|
|
7
|
+
### 1. 禁止创建 Markdown 文档(v1.3.0 强制规则)
|
|
8
|
+
|
|
9
|
+
**⚠️ 除非用户明确要求,否则不要创建任何 .md 文件**
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
❌ 禁止自动创建:
|
|
13
|
+
- USAGE.md
|
|
14
|
+
- GUIDE.md
|
|
15
|
+
- CHANGES.md
|
|
16
|
+
- CHANGELOG.md
|
|
17
|
+
- API.md
|
|
18
|
+
- NOTES.md
|
|
19
|
+
- INSTRUCTIONS.md
|
|
20
|
+
- SETUP.md
|
|
21
|
+
- 任何其他文档文件
|
|
22
|
+
|
|
23
|
+
✅ 允许创建(仅在明确要求时):
|
|
24
|
+
- README.md(项目初始化时或用户明确要求)
|
|
25
|
+
- 用户明确说"生成文档"、"创建说明"、"写一个配置文档"
|
|
26
|
+
- 修改现有的文档文件
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**原因:**
|
|
30
|
+
- 代码注释已经足够说明用途
|
|
31
|
+
- 避免文档与代码不同步
|
|
32
|
+
- 减少维护负担
|
|
33
|
+
- 保持项目目录清洁
|
|
34
|
+
|
|
35
|
+
**正确做法:**
|
|
36
|
+
```typescript
|
|
37
|
+
// ✅ 在代码中添加详细注释
|
|
38
|
+
/**
|
|
39
|
+
* 用户服务类
|
|
40
|
+
*
|
|
41
|
+
* 使用方法:
|
|
42
|
+
* 1. 导入:import { UserService } from './userService'
|
|
43
|
+
* 2. 创建实例:const service = new UserService()
|
|
44
|
+
* 3. 调用方法:await service.getUser(userId)
|
|
45
|
+
*/
|
|
46
|
+
class UserService {
|
|
47
|
+
// 实现...
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 2. 注释格式规范(v1.3.0 强制规则)
|
|
52
|
+
|
|
53
|
+
**⚠️ TypeScript/JavaScript 中单行注释必须使用 `//`**
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// ✅ 正确:单行注释使用 //
|
|
57
|
+
// 使用缓存避免重复请求
|
|
58
|
+
const cachedData = cache.get(key)
|
|
59
|
+
|
|
60
|
+
// ✅ 正确:多行文档注释使用 /** */
|
|
61
|
+
/**
|
|
62
|
+
* 获取用户信息
|
|
63
|
+
* @param userId 用户ID
|
|
64
|
+
* @returns 用户信息对象
|
|
65
|
+
*/
|
|
66
|
+
function getUserInfo(userId: number) {}
|
|
67
|
+
|
|
68
|
+
// ❌ 错误:单行注释不要使用 /** */
|
|
69
|
+
/** 使用缓存避免重复请求 */
|
|
70
|
+
const cachedData = cache.get(key)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**注释必须清晰且有意义:**
|
|
74
|
+
```typescript
|
|
75
|
+
// ✅ 好:说明为什么
|
|
76
|
+
// 使用 WeakMap 避免内存泄漏
|
|
77
|
+
const cache = new WeakMap()
|
|
78
|
+
|
|
79
|
+
// ❌ 坏:重复代码
|
|
80
|
+
// 创建 WeakMap
|
|
81
|
+
const cache = new WeakMap()
|
|
82
|
+
|
|
83
|
+
// ❌ 坏:无意义的注释
|
|
84
|
+
// 定义变量
|
|
85
|
+
const count = 0
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 3. 重要代码必须添加注释
|
|
89
|
+
|
|
90
|
+
所有重要代码部分都需要注释:
|
|
91
|
+
|
|
92
|
+
#### 必须注释的场景
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
// 1. 复杂算法
|
|
96
|
+
// 使用快速排序算法,平均时间复杂度 O(n log n)
|
|
97
|
+
function quickSort(arr: number[]): number[] {
|
|
98
|
+
if (arr.length <= 1) return arr
|
|
99
|
+
|
|
100
|
+
const pivot = arr[0]
|
|
101
|
+
const left = arr.slice(1).filter(x => x <= pivot)
|
|
102
|
+
const right = arr.slice(1).filter(x => x > pivot)
|
|
103
|
+
|
|
104
|
+
return [...quickSort(left), pivot, ...quickSort(right)]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 2. 业务规则
|
|
108
|
+
// 新用户享有 7 天免费试用期
|
|
109
|
+
function getTrialEndDate(user: User): Date {
|
|
110
|
+
const createdDate = new Date(user.createdAt)
|
|
111
|
+
createdDate.setDate(createdDate.getDate() + 7)
|
|
112
|
+
return createdDate
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 3. 性能优化
|
|
116
|
+
// 使用节流避免频繁触发搜索请求
|
|
117
|
+
const throttledSearch = throttle((keyword: string) => {
|
|
118
|
+
searchAPI(keyword)
|
|
119
|
+
}, 300)
|
|
120
|
+
|
|
121
|
+
// 4. 边界条件处理
|
|
122
|
+
function divide(a: number, b: number): number {
|
|
123
|
+
// 防止除零错误
|
|
124
|
+
if (b === 0) {
|
|
125
|
+
throw new Error('除数不能为零')
|
|
126
|
+
}
|
|
127
|
+
return a / b
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 5. 重要状态变更
|
|
131
|
+
// 标记订单为已完成,触发后续流程
|
|
132
|
+
function completeOrder(orderId: string) {
|
|
133
|
+
updateOrderStatus(orderId, 'completed')
|
|
134
|
+
notifyUser(orderId)
|
|
135
|
+
updateInventory(orderId)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 6. API 集成
|
|
139
|
+
// 调用第三方支付接口,超时时间 30 秒
|
|
140
|
+
async function processPayment(order: Order): Promise<PaymentResult> {
|
|
141
|
+
return await paymentGateway.charge({
|
|
142
|
+
amount: order.total,
|
|
143
|
+
timeout: 30000
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// 7. 数据转换
|
|
148
|
+
// 将后端返回的下划线命名转换为驼峰命名
|
|
149
|
+
function transformResponse(data: any) {
|
|
150
|
+
return Object.keys(data).reduce((acc, key) => {
|
|
151
|
+
const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())
|
|
152
|
+
acc[camelKey] = data[key]
|
|
153
|
+
return acc
|
|
154
|
+
}, {})
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### 可选注释的场景
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// 简单明了的代码可以不注释
|
|
162
|
+
function add(a: number, b: number): number {
|
|
163
|
+
return a + b
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// getter/setter 通常不需要注释
|
|
167
|
+
get fullName(): string {
|
|
168
|
+
return `${this.firstName} ${this.lastName}`
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 自解释的变量名不需要注释
|
|
172
|
+
const isUserLoggedIn = checkLoginStatus()
|
|
173
|
+
const hasPermission = user.role === 'admin'
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 3. 注释风格:去 AI 化
|
|
177
|
+
|
|
178
|
+
#### 禁止使用
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
// ❌ 表情符号
|
|
182
|
+
// 🎉 欢迎使用这个超棒的函数!
|
|
183
|
+
// ✨ 神奇的代码来了~
|
|
184
|
+
// 🔥 超级厉害的实现!
|
|
185
|
+
// ⚠️ 请注意这里哦~
|
|
186
|
+
|
|
187
|
+
// ❌ 过度热情的语气
|
|
188
|
+
// 哇!这个功能太酷了!
|
|
189
|
+
// 太棒了!让我们开始吧~
|
|
190
|
+
// 注意啦!这里超级重要!
|
|
191
|
+
|
|
192
|
+
// ❌ 口语化表达
|
|
193
|
+
// 这里稍微有点复杂哈
|
|
194
|
+
// 大家注意下这个地方~
|
|
195
|
+
// 这块儿需要好好看看
|
|
196
|
+
|
|
197
|
+
// ❌ AI 式套话
|
|
198
|
+
// 让我来帮你实现这个功能
|
|
199
|
+
// 我将为您创建一个...
|
|
200
|
+
// 下面是一个很好的例子
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### 正确使用
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
// ✅ 专业简洁
|
|
207
|
+
// 验证用户输入
|
|
208
|
+
// 处理异步请求
|
|
209
|
+
// 更新缓存数据
|
|
210
|
+
|
|
211
|
+
// ✅ 说明原因
|
|
212
|
+
// 使用 Map 提高查询性能
|
|
213
|
+
// 避免循环依赖
|
|
214
|
+
// 兼容 IE11
|
|
215
|
+
|
|
216
|
+
// ✅ 描述技术细节
|
|
217
|
+
// 实现深拷贝,避免引用共享
|
|
218
|
+
// 使用二进制搜索,时间复杂度 O(log n)
|
|
219
|
+
// 采用观察者模式解耦组件
|
|
220
|
+
|
|
221
|
+
// ✅ 警告潜在问题
|
|
222
|
+
// 注意:此操作会修改原数组
|
|
223
|
+
// 仅在开发环境使用
|
|
224
|
+
// 依赖外部配置文件
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### 4. 注释的数量和质量
|
|
228
|
+
|
|
229
|
+
#### 平衡注释密度
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
// ❌ 过少注释
|
|
233
|
+
function processData(data) {
|
|
234
|
+
const filtered = data.filter(x => x.status === 'active')
|
|
235
|
+
const sorted = filtered.sort((a, b) => b.priority - a.priority)
|
|
236
|
+
const grouped = sorted.reduce((acc, item) => {
|
|
237
|
+
const key = item.category
|
|
238
|
+
if (!acc[key]) acc[key] = []
|
|
239
|
+
acc[key].push(item)
|
|
240
|
+
return acc
|
|
241
|
+
}, {})
|
|
242
|
+
return Object.entries(grouped).map(([category, items]) => ({
|
|
243
|
+
category,
|
|
244
|
+
items,
|
|
245
|
+
total: items.reduce((sum, i) => sum + i.value, 0)
|
|
246
|
+
}))
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ✅ 适当注释
|
|
250
|
+
function processData(data: Item[]): CategorySummary[] {
|
|
251
|
+
// 只处理激活状态的数据
|
|
252
|
+
const filtered = data.filter(x => x.status === 'active')
|
|
253
|
+
|
|
254
|
+
// 按优先级降序排序
|
|
255
|
+
const sorted = filtered.sort((a, b) => b.priority - a.priority)
|
|
256
|
+
|
|
257
|
+
// 按类别分组
|
|
258
|
+
const grouped = sorted.reduce((acc, item) => {
|
|
259
|
+
const key = item.category
|
|
260
|
+
if (!acc[key]) acc[key] = []
|
|
261
|
+
acc[key].push(item)
|
|
262
|
+
return acc
|
|
263
|
+
}, {} as Record<string, Item[]>)
|
|
264
|
+
|
|
265
|
+
// 生成每个类别的汇总信息
|
|
266
|
+
return Object.entries(grouped).map(([category, items]) => ({
|
|
267
|
+
category,
|
|
268
|
+
items,
|
|
269
|
+
total: items.reduce((sum, i) => sum + i.value, 0)
|
|
270
|
+
}))
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ❌ 过度注释(废话)
|
|
274
|
+
function calculateTotal(items: Item[]): number {
|
|
275
|
+
// 初始化总和为 0
|
|
276
|
+
let total = 0
|
|
277
|
+
|
|
278
|
+
// 遍历每个项目
|
|
279
|
+
for (const item of items) {
|
|
280
|
+
// 将项目的价格加到总和
|
|
281
|
+
total += item.price
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 返回计算出的总和
|
|
285
|
+
return total
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ✅ 简洁明了
|
|
289
|
+
function calculateTotal(items: Item[]): number {
|
|
290
|
+
return items.reduce((sum, item) => sum + item.price, 0)
|
|
291
|
+
}
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## 实践指南
|
|
295
|
+
|
|
296
|
+
### 代码生成工作流
|
|
297
|
+
|
|
298
|
+
1. **分析需求** - 理解用户要求
|
|
299
|
+
2. **编写代码** - 实现功能逻辑
|
|
300
|
+
3. **添加注释** - 为重要部分添加说明
|
|
301
|
+
4. **自查** - 确认没有创建不必要的文档文件
|
|
302
|
+
5. **去 AI 化** - 检查注释风格是否专业
|
|
303
|
+
|
|
304
|
+
### 检查清单
|
|
305
|
+
|
|
306
|
+
生成代码前检查:
|
|
307
|
+
|
|
308
|
+
- [ ] 是否只创建了必要的代码文件?
|
|
309
|
+
- [ ] 是否避免了创建 .md 文档?
|
|
310
|
+
- [ ] 重要逻辑是否有注释说明?
|
|
311
|
+
- [ ] 注释是否去除了表情符号?
|
|
312
|
+
- [ ] 注释是否使用专业简洁的语言?
|
|
313
|
+
- [ ] 注释是否说明了"为什么"而非"是什么"?
|
|
314
|
+
|
|
315
|
+
### 示例对比
|
|
316
|
+
|
|
317
|
+
#### 错误示例
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
// 生成了不必要的文档
|
|
321
|
+
文件结构:
|
|
322
|
+
├── UserService.ts
|
|
323
|
+
├── USAGE.md ❌ 不要创建
|
|
324
|
+
└── API_GUIDE.md ❌ 不要创建
|
|
325
|
+
|
|
326
|
+
// UserService.ts
|
|
327
|
+
// 🎉 用户服务类来啦~
|
|
328
|
+
class UserService {
|
|
329
|
+
// 超级厉害的登录方法!
|
|
330
|
+
login() {}
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
#### 正确示例
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
// 只生成必要的代码文件
|
|
338
|
+
文件结构:
|
|
339
|
+
└── UserService.ts
|
|
340
|
+
|
|
341
|
+
// UserService.ts
|
|
342
|
+
/**
|
|
343
|
+
* 用户服务类
|
|
344
|
+
* 处理用户认证、信息管理等核心业务逻辑
|
|
345
|
+
*/
|
|
346
|
+
class UserService {
|
|
347
|
+
/**
|
|
348
|
+
* 用户登录
|
|
349
|
+
* @param credentials 登录凭证
|
|
350
|
+
* @returns 登录结果,包含用户信息和 token
|
|
351
|
+
* @throws 当凭证无效时抛出 AuthenticationError
|
|
352
|
+
*/
|
|
353
|
+
async login(credentials: Credentials): Promise<LoginResult> {
|
|
354
|
+
// 验证凭证格式
|
|
355
|
+
this.validateCredentials(credentials)
|
|
356
|
+
|
|
357
|
+
// 调用认证服务
|
|
358
|
+
const result = await authService.authenticate(credentials)
|
|
359
|
+
|
|
360
|
+
// 缓存用户信息
|
|
361
|
+
this.cacheUserInfo(result.user)
|
|
362
|
+
|
|
363
|
+
return result
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// 验证登录凭证格式
|
|
367
|
+
private validateCredentials(credentials: Credentials): void {
|
|
368
|
+
if (!credentials.username || !credentials.password) {
|
|
369
|
+
throw new ValidationError('用户名和密码不能为空')
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// 缓存用户信息到本地存储
|
|
374
|
+
private cacheUserInfo(user: User): void {
|
|
375
|
+
localStorage.setItem('user', JSON.stringify(user))
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## 常见问题
|
|
381
|
+
|
|
382
|
+
### Q: 什么时候可以创建文档?
|
|
383
|
+
|
|
384
|
+
A: 仅在以下情况:
|
|
385
|
+
- 用户明确说"创建一个 README"
|
|
386
|
+
- 项目初始化需要项目说明
|
|
387
|
+
- 修改现有的文档文件
|
|
388
|
+
|
|
389
|
+
### Q: 简单的代码也要注释吗?
|
|
390
|
+
|
|
391
|
+
A: 不需要。如果代码本身就很清晰,不要添加废话注释。
|
|
392
|
+
|
|
393
|
+
### Q: 如何判断注释是否"AI 化"?
|
|
394
|
+
|
|
395
|
+
A: 检查是否包含:
|
|
396
|
+
- 表情符号(🎉 ✨ 🔥 ⚠️ 等)
|
|
397
|
+
- 过度热情的语气("太棒了"、"超级"、"哇")
|
|
398
|
+
- AI 式套话("让我来"、"我将为您")
|
|
399
|
+
|
|
400
|
+
### Q: 英文项目的注释也要遵循这些规则吗?
|
|
401
|
+
|
|
402
|
+
A: 是的。无论中英文,都要保持专业简洁的风格。
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
// ✅ 英文注释示例
|
|
406
|
+
// Cache results to improve performance
|
|
407
|
+
// Handle edge case when array is empty
|
|
408
|
+
// Validate input before processing
|
|
409
|
+
|
|
410
|
+
// ❌ 避免
|
|
411
|
+
// 🎉 Awesome function here!
|
|
412
|
+
// Let me help you with this amazing feature!
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
## 总结
|
|
416
|
+
|
|
417
|
+
核心要点:
|
|
418
|
+
1. **不要创建文档** - 除非明确要求
|
|
419
|
+
2. **充分注释** - 重要代码必须说明
|
|
420
|
+
3. **专业风格** - 去除表情符号和 AI 化语气
|
|
421
|
+
4. **简洁明了** - 说明原因而非重复代码
|