pdd-skills 3.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 +1478 -0
- package/bin/pdd.js +354 -0
- package/config/bpmn-rules.yaml +166 -0
- package/config/checkstyle.xml +105 -0
- package/config/eslint.config.js +48 -0
- package/config/pmd.xml +91 -0
- package/config/prd-rules.yaml +113 -0
- package/config/ruff.toml +45 -0
- package/config/sqlfluff.cfg +82 -0
- package/hooks/hook-executor.js +332 -0
- package/index.js +43 -0
- package/lib/api-routes.js +750 -0
- package/lib/api-server.js +408 -0
- package/lib/cache/cache-config.js +209 -0
- package/lib/cache/system-cache.js +852 -0
- package/lib/config-manager.js +373 -0
- package/lib/generate.js +528 -0
- package/lib/grpc/grpc-routes.js +1134 -0
- package/lib/grpc/grpc-server.js +912 -0
- package/lib/grpc/proto-definitions.js +1033 -0
- package/lib/init.js +172 -0
- package/lib/iteration/auto-fixer.js +1025 -0
- package/lib/iteration/auto-reviewer.js +923 -0
- package/lib/iteration/controller.js +577 -0
- package/lib/list.js +130 -0
- package/lib/mcp-server.js +548 -0
- package/lib/openclaw/api-integration.js +535 -0
- package/lib/openclaw/cli-integration.js +567 -0
- package/lib/openclaw/data-sync.js +845 -0
- package/lib/openclaw/openclaw-adapter.js +783 -0
- package/lib/plugin/example-plugins/code-stats/index.js +332 -0
- package/lib/plugin/example-plugins/code-stats/plugin.json +1 -0
- package/lib/plugin/example-plugins/custom-linter/index.js +472 -0
- package/lib/plugin/example-plugins/custom-linter/plugin.json +1 -0
- package/lib/plugin/example-plugins/hello-world/index.js +86 -0
- package/lib/plugin/example-plugins/hello-world/plugin.json +1 -0
- package/lib/plugin/plugin-manager.js +655 -0
- package/lib/plugin/plugin-sdk.js +565 -0
- package/lib/plugin/sandbox.js +627 -0
- package/lib/quality/rules/maintainability.js +418 -0
- package/lib/quality/rules/performance.js +498 -0
- package/lib/quality/rules/readability.js +441 -0
- package/lib/quality/rules/robustness.js +504 -0
- package/lib/quality/rules/security.js +444 -0
- package/lib/quality/scorer.js +576 -0
- package/lib/report.js +669 -0
- package/lib/sdk-base.js +301 -0
- package/lib/sdk-js.js +446 -0
- package/lib/sdk-python/README.md +546 -0
- package/lib/sdk-python/examples/basic_usage.py +450 -0
- package/lib/sdk-python/pdd_sdk/__init__.py +180 -0
- package/lib/sdk-python/pdd_sdk/client.py +1170 -0
- package/lib/sdk-python/pdd_sdk/events.py +423 -0
- package/lib/sdk-python/pdd_sdk/exceptions.py +158 -0
- package/lib/sdk-python/pdd_sdk/models.py +518 -0
- package/lib/sdk-python/pdd_sdk/utils.py +759 -0
- package/lib/token/budget-alert.js +367 -0
- package/lib/token/budget-manager.js +485 -0
- package/lib/update.js +54 -0
- package/lib/utils/logger.js +88 -0
- package/lib/verify.js +741 -0
- package/lib/version.js +52 -0
- package/lib/vm/README.md +102 -0
- package/lib/vm/dashboard/api-routes.js +669 -0
- package/lib/vm/dashboard/server.js +391 -0
- package/lib/vm/dashboard/sse.js +358 -0
- package/lib/vm/dashboard/static/css/dashboard.css +1378 -0
- package/lib/vm/dashboard/static/index.html +118 -0
- package/lib/vm/dashboard/static/js/app.js +949 -0
- package/lib/vm/dashboard/static/js/charts.js +913 -0
- package/lib/vm/dashboard/static/js/kanban-view.js +1053 -0
- package/lib/vm/dashboard/static/js/pipeline-view.js +463 -0
- package/lib/vm/dashboard/static/js/quality-view.js +598 -0
- package/lib/vm/dashboard/static/js/system-view.js +1021 -0
- package/lib/vm/data-provider.js +1191 -0
- package/lib/vm/event-bus.js +402 -0
- package/lib/vm/hooks/extract-hook.js +307 -0
- package/lib/vm/hooks/generate-hook.js +374 -0
- package/lib/vm/hooks/hook-interface.js +458 -0
- package/lib/vm/hooks/report-hook.js +331 -0
- package/lib/vm/hooks/verify-hook.js +454 -0
- package/lib/vm/models.js +1003 -0
- package/lib/vm/reconciler.js +855 -0
- package/lib/vm/scanner.js +988 -0
- package/lib/vm/state-schema.js +955 -0
- package/lib/vm/state-store.js +733 -0
- package/lib/vm/tui/components/card.js +339 -0
- package/lib/vm/tui/components/progress-bar.js +368 -0
- package/lib/vm/tui/components/sparkline.js +327 -0
- package/lib/vm/tui/components/status-light.js +294 -0
- package/lib/vm/tui/components/table.js +370 -0
- package/lib/vm/tui/input.js +335 -0
- package/lib/vm/tui/renderer.js +548 -0
- package/lib/vm/tui/screens/kanban-screen.js +397 -0
- package/lib/vm/tui/screens/overview-screen.js +357 -0
- package/lib/vm/tui/screens/quality-screen.js +336 -0
- package/lib/vm/tui/screens/system-screen.js +379 -0
- package/lib/vm/tui/tui.js +805 -0
- package/package.json +1 -0
- package/scripts/cso-analyzer.js +198 -0
- package/scripts/eval-runner.js +359 -0
- package/scripts/i18n-checker.js +109 -0
- package/scripts/linter/activiti-linter.js +272 -0
- package/scripts/linter/prd-linter.js +162 -0
- package/scripts/linter/report-generator.js +207 -0
- package/scripts/linter/run-linters.js +285 -0
- package/scripts/linter/sql-linter.js +166 -0
- package/scripts/token-analyzer.js +162 -0
- package/scripts/vm-test.js +180 -0
- package/skills/core/official-doc-writer/LICENSE +21 -0
- package/skills/core/official-doc-writer/README.md +232 -0
- package/skills/core/official-doc-writer/SKILL.md +475 -0
- package/skills/core/official-doc-writer/_meta.json +1 -0
- package/skills/core/official-doc-writer/document_generator.py +580 -0
- package/skills/core/official-doc-writer/evals/default-evals.json +1 -0
- package/skills/core/official-doc-writer/examples.md +150 -0
- package/skills/core/official-doc-writer/fonts/FONTS_LIST.md +45 -0
- package/skills/core/official-doc-writer/fonts/README.md +141 -0
- package/skills/core/official-doc-writer/fonts/SIMFANG.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMHEI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMKAI.TTF +0 -0
- package/skills/core/official-doc-writer/fonts/SIMSUN.TTC +0 -0
- package/skills/core/official-doc-writer/fonts//346/226/271/346/255/243/345/260/217/346/240/207/345/256/213GBK.TTF +0 -0
- package/skills/core/official-doc-writer/references/GBT_9704-2012_/345/205/232/346/224/277/346/234/272/345/205/263/345/205/254/346/226/207/346/240/274/345/274/217.md +422 -0
- package/skills/core/official-doc-writer/scripts/__pycache__/generate_official_doc.cpython-313.pyc +0 -0
- package/skills/core/official-doc-writer/scripts/dialog_manager.py +564 -0
- package/skills/core/official-doc-writer/scripts/generate_official_doc.py +252 -0
- package/skills/core/official-doc-writer/scripts/install_fonts.py +390 -0
- package/skills/core/official-doc-writer/scripts/smart_prompts.py +363 -0
- package/skills/core/pdd-ba/SKILL.md +305 -0
- package/skills/core/pdd-ba/_meta.json +1 -0
- package/skills/core/pdd-ba/evals/default-evals.json +1 -0
- package/skills/core/pdd-code-reviewer/SKILL.md +378 -0
- package/skills/core/pdd-code-reviewer/_meta.json +1 -0
- package/skills/core/pdd-code-reviewer/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-change/SKILL.md +350 -0
- package/skills/core/pdd-doc-change/_meta.json +1 -0
- package/skills/core/pdd-doc-change/evals/default-evals.json +1 -0
- package/skills/core/pdd-doc-gardener/SKILL.md +248 -0
- package/skills/core/pdd-doc-gardener/_meta.json +1 -0
- package/skills/core/pdd-doc-gardener/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/SKILL.md +360 -0
- package/skills/core/pdd-entropy-reduction/_meta.json +1 -0
- package/skills/core/pdd-entropy-reduction/evals/default-evals.json +1 -0
- package/skills/core/pdd-entropy-reduction/references/entropy-report-template.md +287 -0
- package/skills/core/pdd-entropy-reduction/references/golden-principles.md +573 -0
- package/skills/core/pdd-entropy-reduction/scripts/entropy_scan.py +712 -0
- package/skills/core/pdd-extract-features/SKILL.md +320 -0
- package/skills/core/pdd-extract-features/_meta.json +1 -0
- package/skills/core/pdd-extract-features/evals/default-evals.json +1 -0
- package/skills/core/pdd-generate-spec/SKILL.md +418 -0
- package/skills/core/pdd-generate-spec/_meta.json +1 -0
- package/skills/core/pdd-generate-spec/evals/default-evals.json +1 -0
- package/skills/core/pdd-implement-feature/SKILL.md +332 -0
- package/skills/core/pdd-implement-feature/_meta.json +1 -0
- package/skills/core/pdd-implement-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/SKILL.md +540 -0
- package/skills/core/pdd-main/_meta.json +1 -0
- package/skills/core/pdd-main/evals/default-evals.json +1 -0
- package/skills/core/pdd-main/evals/evals.json +215 -0
- package/skills/core/pdd-verify-feature/SKILL.md +474 -0
- package/skills/core/pdd-verify-feature/_meta.json +1 -0
- package/skills/core/pdd-verify-feature/evals/default-evals.json +1 -0
- package/skills/core/pdd-vm/evals/default-evals.json +1 -0
- package/skills/core/traffic-accident-assessor/LICENSE +29 -0
- package/skills/core/traffic-accident-assessor/SKILL.md +439 -0
- package/skills/core/traffic-accident-assessor/evals/evals.json +1 -0
- package/skills/core/traffic-accident-assessor/references/accident-types.md +369 -0
- package/skills/core/traffic-accident-assessor/references/liability-rules.md +287 -0
- package/skills/core/traffic-accident-assessor/references/traffic-laws.md +226 -0
- package/skills/core/traffic-accident-assessor/references//351/253/230/345/260/224/345/244/253/350/257/264/346/230/216/344/271/246.pdf +32576 -106
- package/skills/core/traffic-accident-assessor/scripts/generate_official_statement.py +588 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_report.py +495 -0
- package/skills/core/traffic-accident-assessor/scripts/generate_statement.py +528 -0
- package/skills/core/traffic-accident-assessor.zip +0 -0
- package/skills/entropy/expert-arch-enforcer/SKILL.md +292 -0
- package/skills/entropy/expert-arch-enforcer/_meta.json +1 -0
- package/skills/entropy/expert-arch-enforcer/evals/default-evals.json +1 -0
- package/skills/entropy/expert-auto-refactor/SKILL.md +327 -0
- package/skills/entropy/expert-auto-refactor/_meta.json +1 -0
- package/skills/entropy/expert-auto-refactor/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/SKILL.md +468 -0
- package/skills/entropy/expert-code-quality/_meta.json +1 -0
- package/skills/entropy/expert-code-quality/evals/default-evals.json +1 -0
- package/skills/entropy/expert-code-quality/evals/evals.json +109 -0
- package/skills/entropy/expert-code-quality/references/code-smells.md +605 -0
- package/skills/entropy/expert-code-quality/references/design-patterns.md +1111 -0
- package/skills/entropy/expert-code-quality/references/refactoring-catalog.md +1281 -0
- package/skills/entropy/expert-code-quality/references/solid-principles.md +524 -0
- package/skills/entropy/expert-entropy-auditor/SKILL.md +276 -0
- package/skills/entropy/expert-entropy-auditor/_meta.json +1 -0
- package/skills/entropy/expert-entropy-auditor/evals/default-evals.json +1 -0
- package/skills/expert/expert-activiti/SKILL.md +497 -0
- package/skills/expert/expert-activiti/_meta.json +1 -0
- package/skills/expert/expert-mysql/SKILL.md +832 -0
- package/skills/expert/expert-mysql/_meta.json +1 -0
- package/skills/expert/expert-performance/SKILL.md +379 -0
- package/skills/expert/expert-performance/_meta.json +1 -0
- package/skills/expert/expert-performance/evals/default-evals.json +1 -0
- package/skills/expert/expert-ruoyi/SKILL.md +472 -0
- package/skills/expert/expert-ruoyi/_meta.json +1 -0
- package/skills/expert/expert-security/SKILL.md +1341 -0
- package/skills/expert/expert-security/_meta.json +1 -0
- package/skills/expert/expert-security/evals/default-evals.json +1 -0
- package/skills/expert/software-architect/SKILL.md +350 -0
- package/skills/expert/software-architect/_meta.json +1 -0
- package/skills/expert/software-engineer/SKILL.md +437 -0
- package/skills/expert/software-engineer/_meta.json +1 -0
- package/skills/expert/software-engineer/architecture.md +130 -0
- package/skills/expert/software-engineer/patterns.md +151 -0
- package/skills/expert/software-engineer/testing.md +135 -0
- package/skills/expert/system-architect/SKILL.md +628 -0
- package/skills/expert/system-architect/_meta.json +1 -0
- package/skills/expert/system-architect/assets/templates/ARCHITECTURE.md +25 -0
- package/skills/expert/system-architect/assets/templates/README.md +44 -0
- package/skills/expert/system-architect/references/js-ts-standards.md +18 -0
- package/skills/expert/system-architect/references/python-standards.md +19 -0
- package/skills/expert/system-architect/references/scaffolding.md +61 -0
- package/skills/expert/system-architect/references/security-checklist.md +21 -0
- package/skills/openspec/openspec-apply-change/SKILL.md +156 -0
- package/skills/openspec/openspec-apply-change/_meta.json +1 -0
- package/skills/openspec/openspec-archive-change/SKILL.md +114 -0
- package/skills/openspec/openspec-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-bulk-archive-change/SKILL.md +246 -0
- package/skills/openspec/openspec-bulk-archive-change/_meta.json +1 -0
- package/skills/openspec/openspec-continue-change/SKILL.md +118 -0
- package/skills/openspec/openspec-continue-change/_meta.json +1 -0
- package/skills/openspec/openspec-explore/SKILL.md +288 -0
- package/skills/openspec/openspec-explore/_meta.json +1 -0
- package/skills/openspec/openspec-ff-change/SKILL.md +101 -0
- package/skills/openspec/openspec-ff-change/_meta.json +1 -0
- package/skills/openspec/openspec-new-change/SKILL.md +74 -0
- package/skills/openspec/openspec-new-change/_meta.json +1 -0
- package/skills/openspec/openspec-onboard/SKILL.md +554 -0
- package/skills/openspec/openspec-onboard/_meta.json +1 -0
- package/skills/openspec/openspec-sync-specs/SKILL.md +138 -0
- package/skills/openspec/openspec-sync-specs/_meta.json +1 -0
- package/skills/openspec/openspec-verify-change/SKILL.md +168 -0
- package/skills/openspec/openspec-verify-change/_meta.json +1 -0
- package/skills/pr/pdd-multi-review/SKILL.md +534 -0
- package/skills/pr/pdd-multi-review/_meta.json +1 -0
- package/skills/pr/pdd-pr-batch/SKILL.md +303 -0
- package/skills/pr/pdd-pr-batch/_meta.json +1 -0
- package/skills/pr/pdd-pr-create/SKILL.md +344 -0
- package/skills/pr/pdd-pr-create/_meta.json +1 -0
- package/skills/pr/pdd-pr-merge/SKILL.md +286 -0
- package/skills/pr/pdd-pr-merge/_meta.json +1 -0
- package/skills/pr/pdd-pr-review/SKILL.md +217 -0
- package/skills/pr/pdd-pr-review/_meta.json +1 -0
- package/skills/pr/pdd-task-manager/SKILL.md +636 -0
- package/skills/pr/pdd-task-manager/_meta.json +1 -0
- package/skills/pr/pdd-template-engine/SKILL.md +306 -0
- package/skills/pr/pdd-template-engine/_meta.json +1 -0
- package/templates/behavior-shaping/iron-law-template.md +87 -0
- package/templates/behavior-shaping/rationalization-template.md +62 -0
- package/templates/behavior-shaping/red-flags-template.md +70 -0
- package/templates/bilingual-template.md +139 -0
- package/templates/config/default.yaml +47 -0
- package/templates/project/default/README.md +31 -0
- package/templates/project/frontend/README.md +46 -0
- package/templates/project/java/README.md +48 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// lib/quality/rules/performance.js - 性能规则集
|
|
2
|
+
// 评估代码性能: 算法效率、内存使用、I/O操作、循环优化等
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 性能规则集
|
|
6
|
+
*
|
|
7
|
+
* 规则列表:
|
|
8
|
+
* - loopEfficiency: 循环效率(避免嵌套循环、减少循环内操作)
|
|
9
|
+
* - memoryAllocation: 内存分配优化
|
|
10
|
+
* - ioOperation: I/O操作效率
|
|
11
|
+
* - stringConcatenation: 字符串拼接优化
|
|
12
|
+
* - asyncPattern: 异步模式最佳实践
|
|
13
|
+
* - dataStructure: 数据结构选择
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const rules = [
|
|
17
|
+
{
|
|
18
|
+
name: 'loopEfficiency',
|
|
19
|
+
description: '循环效率: 避免深层嵌套循环,循环内避免重复计算',
|
|
20
|
+
maxScore: 25,
|
|
21
|
+
check(code, ext) {
|
|
22
|
+
const lines = code.split('\n');
|
|
23
|
+
const issues = [];
|
|
24
|
+
|
|
25
|
+
// 检测嵌套循环
|
|
26
|
+
let maxNestingDepth = 0;
|
|
27
|
+
let currentNesting = 0;
|
|
28
|
+
let loopPositions = [];
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < lines.length; i++) {
|
|
31
|
+
const line = lines[i].trim();
|
|
32
|
+
|
|
33
|
+
if (/\b(for|while|do)\s*\(/.test(line) || /\.forEach|\.map|\.filter|\.reduce|\.some|\.every/.test(line)) {
|
|
34
|
+
currentNesting++;
|
|
35
|
+
loopPositions.push({ line: i + 1, depth: currentNesting });
|
|
36
|
+
maxNestingDepth = Math.max(maxNestingDepth, currentNesting);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 简化的块结束检测
|
|
40
|
+
const closeBraces = (line.match(/\}/g) || []).length;
|
|
41
|
+
currentNesting = Math.max(0, currentNesting - closeBraces);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (maxNestingDepth >= 3) {
|
|
45
|
+
issues.push({
|
|
46
|
+
type: 'deep_nesting',
|
|
47
|
+
severity: 'high',
|
|
48
|
+
detail: `检测到${maxNestingDepth}层嵌套循环`,
|
|
49
|
+
line: loopPositions.find(p => p.depth === maxNestingDepth)?.line
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 检测循环内的重复计算
|
|
54
|
+
for (let i = 0; i < lines.length; i++) {
|
|
55
|
+
const line = lines[i];
|
|
56
|
+
|
|
57
|
+
// 在for循环条件中检测属性访问或方法调用
|
|
58
|
+
const forConditionMatch = line.match(/for\s*\([^;]*;\s*([^;]+)\s*(?:<|>|<=|>=)[^;]*;/);
|
|
59
|
+
if (forConditionMatch) {
|
|
60
|
+
const condition = forConditionMatch[1];
|
|
61
|
+
// 如果是 .length 或 .size 以外的调用/属性访问
|
|
62
|
+
if (/\.\w+\(?\)?/.test(condition) && !/\.(length|size)$/.test(condition)) {
|
|
63
|
+
issues.push({
|
|
64
|
+
type: 'repeated_computation',
|
|
65
|
+
severity: 'medium',
|
|
66
|
+
detail: `循环条件中有可能的重复计算: ${condition.trim()}`,
|
|
67
|
+
line: i + 1
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 检测循环内创建新对象/数组
|
|
74
|
+
for (let i = 0; i < lines.length; i++) {
|
|
75
|
+
const line = lines[i];
|
|
76
|
+
const indent = line.match(/^(\s*)/)?.[1]?.length || 0;
|
|
77
|
+
|
|
78
|
+
// 判断是否在循环内部(基于缩进)
|
|
79
|
+
if (indent >= 4 && (
|
|
80
|
+
/new\s+(Array|Object|Map|Set|Date)\b/.test(line) ||
|
|
81
|
+
/\[\]/.test(line) && /=\s*\[/.test(line) ||
|
|
82
|
+
/\{\}/.test(line) && /=\s*\{/.test(line)
|
|
83
|
+
)) {
|
|
84
|
+
issues.push({
|
|
85
|
+
type: 'allocation_in_loop',
|
|
86
|
+
severity: 'low',
|
|
87
|
+
detail: '循环内可能有不必要的对象分配',
|
|
88
|
+
line: i + 1
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
passed: issues.filter(i => i.severity === 'high').length === 0,
|
|
95
|
+
deduction: Math.min(
|
|
96
|
+
issues.filter(i => i.severity === 'high').length * 10 +
|
|
97
|
+
issues.filter(i => i.severity === 'medium').length * 5 +
|
|
98
|
+
issues.filter(i => i.severity === 'low').length * 2,
|
|
99
|
+
25
|
|
100
|
+
),
|
|
101
|
+
message: issues.length > 0
|
|
102
|
+
? `发现${issues.length}个性能问题: ${issues.map(i => i.detail).join('; ')}`
|
|
103
|
+
: '循环结构效率良好',
|
|
104
|
+
suggestion: '将循环不变量提取到循环外;使用Map/Set替代数组的O(n)查找;考虑用高阶函数替代嵌套循环',
|
|
105
|
+
line: issues.length > 0 ? issues[0].line : null
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
{
|
|
111
|
+
name: 'memoryAllocation',
|
|
112
|
+
description: '内存分配: 避免不必要的对象创建和大数组操作',
|
|
113
|
+
maxScore: 20,
|
|
114
|
+
check(code, ext) {
|
|
115
|
+
const lines = code.split('\n');
|
|
116
|
+
const issues = [];
|
|
117
|
+
|
|
118
|
+
let largeArrayCreations = 0;
|
|
119
|
+
let objectCreationsInHotPath = 0;
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < lines.length; i++) {
|
|
122
|
+
const line = lines[i].trim();
|
|
123
|
+
|
|
124
|
+
// 大数组字面量(超过10个元素)
|
|
125
|
+
const arrayLiteral = line.match(/\[(?:[^,\]]+,){10,}[^]\]]*\]/);
|
|
126
|
+
if (arrayLiteral) {
|
|
127
|
+
largeArrayCreations++;
|
|
128
|
+
if (largeArrayCreations <= 3) {
|
|
129
|
+
issues.push({
|
|
130
|
+
type: 'large_array_literal',
|
|
131
|
+
detail: '大数组字面量',
|
|
132
|
+
line: i + 1
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 频繁的临时对象创建模式
|
|
138
|
+
if (/new\s+Object\(|new\s+Array\(/.test(line)) {
|
|
139
|
+
objectCreationsInHotPath++;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 不必要的展开运算符复制大数组
|
|
143
|
+
if (/\[\.\.\.(?!.*(?:filter|map|slice))/.test(line)) {
|
|
144
|
+
issues.push({
|
|
145
|
+
type: 'unnecessary_spread',
|
|
146
|
+
detail: '不必要的数组展开拷贝',
|
|
147
|
+
line: i + 1
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 字符串拼接转数组再join的模式(低效)
|
|
152
|
+
if ((line.includes('.split(') && lines.slice(i, i + 4).join('\n').includes('.join('))) ||
|
|
153
|
+
(line.includes('.push(') && lines.slice(i, i + 5).join('\n').includes('"))) {
|
|
154
|
+
// 可能存在低效的字符串构建模式
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 检测可能的内存泄漏模式
|
|
159
|
+
const hasEventListenersWithoutCleanup = /addEventListener/.test(code) && !/removeEventListener/.test(code);
|
|
160
|
+
if (hasEventListenersWithoutCleanup) {
|
|
161
|
+
issues.push({
|
|
162
|
+
type: 'potential_memory_leak',
|
|
163
|
+
detail: '事件监听器可能未清理',
|
|
164
|
+
severity: 'high'
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 检测闭包中引用大量数据
|
|
169
|
+
const closureWithLargeData = /function\s*\([^)]*\)\s*\{[\s\S]{500,}return/.test(code);
|
|
170
|
+
if (closureWithLargeData) {
|
|
171
|
+
issues.push({
|
|
172
|
+
type: 'heavy_closure',
|
|
173
|
+
detail: '闭包可能捕获过多变量'
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
passed: issues.filter(i => i.severity === 'high').length === 0,
|
|
179
|
+
deduction: Math.min(issues.length * 3, 20),
|
|
180
|
+
message: issues.length > 0
|
|
181
|
+
? `发现${issues.length}个内存相关的问题`
|
|
182
|
+
: '内存使用模式良好',
|
|
183
|
+
suggestion: '复用对象而非频繁创建;使用对象池管理短生命周期对象;及时解除大对象的引用',
|
|
184
|
+
line: issues.length > 0 ? issues[0].line : null
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
{
|
|
190
|
+
name: 'ioOperation',
|
|
191
|
+
description: 'I/O操作: 批量I/O、异步I/O、避免同步阻塞',
|
|
192
|
+
maxScore: 20,
|
|
193
|
+
check(code, ext) {
|
|
194
|
+
const lines = code.split('\n');
|
|
195
|
+
const issues = [];
|
|
196
|
+
|
|
197
|
+
// 同步I/O操作(在Node.js中应尽量避免)
|
|
198
|
+
const syncPatterns = [
|
|
199
|
+
{ pattern: /readFileSync/, name: '同步文件读取', severity: 'high' },
|
|
200
|
+
{ pattern: /writeFileSync/, name: '同步文件写入', severity: 'high' },
|
|
201
|
+
{ pattern: /existsSync/, name: '同步文件检查', severity: 'medium' },
|
|
202
|
+
{ pattern: /\.execSync\(/, name: '同步子进程', severity: 'high' },
|
|
203
|
+
{ pattern: /require\s*\(['"]fs['"]\)/, name: 'fs模块引入', severity: 'info' }
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
for (let i = 0; i < lines.length; i++) {
|
|
207
|
+
const line = lines[i];
|
|
208
|
+
|
|
209
|
+
for (const { pattern, name, severity } of syncPatterns) {
|
|
210
|
+
if (pattern.test(line)) {
|
|
211
|
+
// 排除在注释中的引用
|
|
212
|
+
if (!line.trim().startsWith('//') && !line.trim().startsWith('*')) {
|
|
213
|
+
issues.push({
|
|
214
|
+
type: 'sync_io',
|
|
215
|
+
operation: name,
|
|
216
|
+
severity,
|
|
217
|
+
line: i + 1
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// N+1查询模式检测(简化版:循环内有数据库/I/O调用)
|
|
225
|
+
let loopDepth = 0;
|
|
226
|
+
for (let i = 0; i < lines.length; i++) {
|
|
227
|
+
const line = lines[i];
|
|
228
|
+
|
|
229
|
+
if (/\b(for|while|forEach|map|filter)\b/.test(line)) {
|
|
230
|
+
loopDepth++;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (loopDepth > 0 && /(\.query|\.execute|\.find|fetch\(|axios|request|readFile|writeFile)\s*\(/.test(line)) {
|
|
234
|
+
issues.push({
|
|
235
|
+
type: 'n_plus_one',
|
|
236
|
+
detail: '循环内可能有I/O操作(N+1问题)',
|
|
237
|
+
severity: 'high',
|
|
238
|
+
line: i + 1
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const closeBraces = (line.match(/\}/g) || []).length;
|
|
243
|
+
loopDepth = Math.max(0, loopDepth - closeBraces);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// 检查是否有批量处理替代方案
|
|
247
|
+
const hasBatchOperation = /Promise\.all|batch|bulk|multi/.test(code);
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
passed: issues.filter(i => i.severity === 'high').length === 0,
|
|
251
|
+
deduction: Math.min(
|
|
252
|
+
issues.filter(i => i.severity === 'high').length * 8 +
|
|
253
|
+
issues.filter(i => i.severity === 'medium').length * 3,
|
|
254
|
+
20
|
|
255
|
+
),
|
|
256
|
+
message: issues.length > 0
|
|
257
|
+
? `发现${issues.length}个I/O相关问题`
|
|
258
|
+
: 'I/O操作模式良好',
|
|
259
|
+
suggestion: '使用异步API替代同步API;将循环内的I/O操作提取为批量操作;使用Promise.all并行化独立请求',
|
|
260
|
+
line: issues.length > 0 ? issues[0].line : null
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
|
|
265
|
+
{
|
|
266
|
+
name: 'stringConcatenation',
|
|
267
|
+
description: '字符串拼接: 使用模板字面量或数组join替代多次+拼接',
|
|
268
|
+
maxScore: 10,
|
|
269
|
+
check(code, ext) {
|
|
270
|
+
const lines = code.split('\n');
|
|
271
|
+
const inefficientPatterns = [];
|
|
272
|
+
|
|
273
|
+
// 检测循环内的字符串+=操作
|
|
274
|
+
let inLoop = false;
|
|
275
|
+
let loopIndent = 0;
|
|
276
|
+
|
|
277
|
+
for (let i = 0; i < lines.length; i++) {
|
|
278
|
+
const line = lines[i];
|
|
279
|
+
const trimmed = line.trim();
|
|
280
|
+
const currentIndent = line.match(/^(\s*)/)?.[1]?.length || 0;
|
|
281
|
+
|
|
282
|
+
// 检测循环开始
|
|
283
|
+
if (/\b(for|while|do)\s*\(/.test(trimmed) || /\.forEach|\.map|\.filter|\.reduce/.test(trimmed)) {
|
|
284
|
+
inLoop = true;
|
|
285
|
+
loopIndent = currentIndent;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 检测字符串拼接
|
|
289
|
+
if (inLoop && currentIndent > loopIndent) {
|
|
290
|
+
const concatMatch = trimmed.match(/\w+\s*\+=\s*["']/);
|
|
291
|
+
if (concatMatch) {
|
|
292
|
+
inefficientPatterns.push({
|
|
293
|
+
type: 'concat_in_loop',
|
|
294
|
+
line: i + 1,
|
|
295
|
+
context: trimmed.substring(0, 50)
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 检测多级链式+
|
|
301
|
+
const chainedPlus = trimmed.match(/\+\s*["'][^"]*["']\s*\+\s*["']/);
|
|
302
|
+
if (chainedPlus && !trimmed.startsWith('//')) {
|
|
303
|
+
inefficientPatterns.push({
|
|
304
|
+
type: 'chained_concat',
|
|
305
|
+
line: i + 1,
|
|
306
|
+
context: trimmed.substring(0, 60)
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// 循环结束
|
|
311
|
+
if (inLoop && currentIndent <= loopIndent && trimmed === '}') {
|
|
312
|
+
inLoop = false;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return {
|
|
317
|
+
passed: inefficientPatterns.length <= 1,
|
|
318
|
+
deduction: Math.min(inefficientPatterns.length * 3, 10),
|
|
319
|
+
message: inefficientPatterns.length > 0
|
|
320
|
+
? `发现${inefficientPatterns.length}处低效字符串拼接`
|
|
321
|
+
: '字符串操作方式高效',
|
|
322
|
+
suggestion: '使用模板字符串(``)替代+拼接;循环中使用数组收集元素后join',
|
|
323
|
+
line: inefficientPatterns.length > 0 ? inefficientPatterns[0].line : null
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
|
|
328
|
+
{
|
|
329
|
+
name: 'asyncPattern',
|
|
330
|
+
description: '异步模式: 正确使用async/await和Promise',
|
|
331
|
+
maxScore: 10,
|
|
332
|
+
check(code, ext) {
|
|
333
|
+
const issues = [];
|
|
334
|
+
|
|
335
|
+
// 检测不必要的await(非Promise值上的await)
|
|
336
|
+
const unnecessaryAwaits = [];
|
|
337
|
+
const lines = code.split('\n');
|
|
338
|
+
|
|
339
|
+
for (let i = 0; i < lines.length; i++) {
|
|
340
|
+
const line = lines[i];
|
|
341
|
+
|
|
342
|
+
// await常量或简单值
|
|
343
|
+
if (/await\s+\d+|await\s+"|await\s+'|await\s+(true|false|null|undefined)/.test(line)) {
|
|
344
|
+
unnecessaryAwaits.push({ line: i + 1, context: line.trim().substring(0, 50) });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// async函数中没有await
|
|
348
|
+
if (/async\s+function|=\s*async/.test(line)) {
|
|
349
|
+
const funcBody = this._extractUntilClose(lines, i);
|
|
350
|
+
if (funcBody && !/\bawait\b/.test(funcBody)) {
|
|
351
|
+
issues.push({
|
|
352
|
+
type: 'async_without_await',
|
|
353
|
+
detail: 'async函数中没有await调用',
|
|
354
|
+
line: i + 1
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 检测顺序await可并行化的场景
|
|
361
|
+
const sequentialAwaits = this._findSequentialAwaits(lines);
|
|
362
|
+
if (sequentialAwaits.length > 0) {
|
|
363
|
+
issues.push({
|
|
364
|
+
type: 'sequential_awaits',
|
|
365
|
+
detail: `${sequentialAwaits.length}处顺序await可能可以并行化`,
|
|
366
|
+
suggestions: sequentialAwaits
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// 检测未处理的Promise rejection
|
|
371
|
+
if (/new Promise\(/.test(code) && !/reject\(/.test(code)) {
|
|
372
|
+
issues.push({
|
|
373
|
+
type: 'unhandled_rejection',
|
|
374
|
+
detail: 'Promise构造缺少reject处理'
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
passed: issues.length === 0,
|
|
380
|
+
deduction: Math.min(issues.length * 3, 10),
|
|
381
|
+
message: issues.length > 0
|
|
382
|
+
? `发现${issues.length}个异步模式问题`
|
|
383
|
+
: '异步模式使用正确',
|
|
384
|
+
suggestion: '移除不必要的await;使用Promise.all并行独立操作;确保所有Promise路径都有错误处理',
|
|
385
|
+
line: issues.length > 0 ? issues[0].line : null
|
|
386
|
+
};
|
|
387
|
+
},
|
|
388
|
+
|
|
389
|
+
_extractUntilClose(lines, startIndex) {
|
|
390
|
+
let braces = 0;
|
|
391
|
+
let started = false;
|
|
392
|
+
const body = [];
|
|
393
|
+
|
|
394
|
+
for (let i = startIndex; i < lines.length && body.length < 100; i++) {
|
|
395
|
+
for (const ch of lines[i]) {
|
|
396
|
+
if (ch === '{') { braces++; started = true; }
|
|
397
|
+
if (ch === '}') braces--;
|
|
398
|
+
}
|
|
399
|
+
if (started) body.push(lines[i]);
|
|
400
|
+
if (started && braces === 0) break;
|
|
401
|
+
}
|
|
402
|
+
return body.join('\n');
|
|
403
|
+
},
|
|
404
|
+
|
|
405
|
+
_findSequentialAwaits(lines) {
|
|
406
|
+
const sequential = [];
|
|
407
|
+
for (let i = 0; i < lines.length - 2; i++) {
|
|
408
|
+
const line1 = lines[i].trim();
|
|
409
|
+
const line2 = lines[i + 1].trim();
|
|
410
|
+
|
|
411
|
+
if (/^await\s+/.test(line1) && /^await\s+/.test(line2)) {
|
|
412
|
+
// 排除后者依赖前者结果的场景
|
|
413
|
+
if (!this._isDependent(lines[i], lines[i + 1])) {
|
|
414
|
+
sequential.push({
|
|
415
|
+
line1: i + 1,
|
|
416
|
+
line2: i + 2
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return sequential;
|
|
422
|
+
},
|
|
423
|
+
|
|
424
|
+
_isDependent(line1, line2) {
|
|
425
|
+
// 简单判断:第二行是否使用了第一行的赋值结果
|
|
426
|
+
const assignMatch = line1.match(/(?:const|let|var)\s+(\w+)\s*=/);
|
|
427
|
+
if (assignMatch) {
|
|
428
|
+
return line2.includes(assignMatch[1]);
|
|
429
|
+
}
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
|
|
434
|
+
{
|
|
435
|
+
name: 'dataStructure',
|
|
436
|
+
description: '数据结构选择: 根据操作类型选择合适的数据结构',
|
|
437
|
+
maxScore: 15,
|
|
438
|
+
check(code, ext) {
|
|
439
|
+
const issues = [];
|
|
440
|
+
const lines = code.split('\n');
|
|
441
|
+
|
|
442
|
+
// 数组includes/find/indexOf用于大数据集
|
|
443
|
+
for (let i = 0; i < lines.length; i++) {
|
|
444
|
+
const line = lines[i];
|
|
445
|
+
|
|
446
|
+
// 在循环中使用数组的includes/find/indexOf
|
|
447
|
+
if (/\b(for|while|forEach)\b/.test(lines[Math.max(0, i - 1)]?.trim() || '')) {
|
|
448
|
+
if (/\.(includes|indexOf|find|findIndex)\s*\(/.test(line)) {
|
|
449
|
+
const arrVar = line.match(/(\w+)\.(includes|indexOf|find)/)?.[1];
|
|
450
|
+
if (arrVar) {
|
|
451
|
+
issues.push({
|
|
452
|
+
type: 'array_lookup_in_loop',
|
|
453
|
+
detail: `循环中使用数组.${line.match(/\.\w+/)?.[0]}查找,建议改用Set`,
|
|
454
|
+
line: i + 1,
|
|
455
|
+
variable: arrVar
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// 对象属性枚举用于频繁查找
|
|
463
|
+
const objectKeysUsage = (code.match(/Object\.keys\([^)]+\)\.find/g) || []).length;
|
|
464
|
+
if (objectKeysUsage > 2) {
|
|
465
|
+
issues.push({
|
|
466
|
+
type: 'object_keys_frequent',
|
|
467
|
+
detail: `频繁使用Object.keys().find() (${objectKeysUsage}次),考虑使用Map`,
|
|
468
|
+
count: objectKeysUsage
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// 数组shift/unshift操作(O(n)复杂度)
|
|
473
|
+
const shiftOps = (code.match(/\.\b(shift|unshift)\s*\(/g) || []).length;
|
|
474
|
+
if (shiftOps > 2) {
|
|
475
|
+
issues.push({
|
|
476
|
+
type: 'expensive_array_ops',
|
|
477
|
+
detail: `多处使用shift/unshift操作(${shiftOps}次),时间复杂度O(n)`
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return {
|
|
482
|
+
passed: issues.filter(i => i.type === 'array_lookup_in_loop').length === 0,
|
|
483
|
+
deduction: Math.min(
|
|
484
|
+
issues.filter(i => i.type === 'array_lookup_in_loop').length * 5 +
|
|
485
|
+
issues.filter(i => i.type !== 'array_lookup_in_loop').length * 2,
|
|
486
|
+
15
|
|
487
|
+
),
|
|
488
|
+
message: issues.length > 0
|
|
489
|
+
? `发现${issues.length}个数据结构选择问题`
|
|
490
|
+
: '数据结构使用合理',
|
|
491
|
+
suggestion: '频繁查找场景使用Set/Map(O(1))替代数组(O(n));队列操作考虑LinkedList;键值对优先使用Map',
|
|
492
|
+
line: issues.length > 0 ? issues[0].line : null
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
];
|
|
497
|
+
|
|
498
|
+
export default rules;
|