(result: TreeValidationResult, type: "error" | "warning" = "error")
| 7 | * @param type 提示类型:"error" | "warning" |
| 8 | */ |
| 9 | export function showTreeValidationErrors(result: TreeValidationResult, type: "error" | "warning" = "error"): void { |
| 10 | if (result.issues.length === 0) return; |
| 11 | |
| 12 | // 按类型分组问题 |
| 13 | const issuesByType = new Map<string, typeof result.issues>(); |
| 14 | for (const issue of result.issues) { |
| 15 | if (!issuesByType.has(issue.type)) { |
| 16 | issuesByType.set(issue.type, []); |
| 17 | } |
| 18 | issuesByType.get(issue.type)!.push(issue); |
| 19 | } |
| 20 | |
| 21 | // 构建错误消息 |
| 22 | const messages: string[] = []; |
| 23 | messages.push("当前结构不符合树形结构:"); |
| 24 | |
| 25 | // 1. 自环问题 |
| 26 | const selfLoopIssues = issuesByType.get("selfLoop"); |
| 27 | if (selfLoopIssues && selfLoopIssues.length > 0) { |
| 28 | messages.push(""); |
| 29 | messages.push("• 存在自环:"); |
| 30 | for (const issue of selfLoopIssues) { |
| 31 | messages.push(` - ${issue.message}`); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // 2. 边重叠问题 |
| 36 | const overlappingIssues = issuesByType.get("overlappingEdges"); |
| 37 | if (overlappingIssues && overlappingIssues.length > 0) { |
| 38 | messages.push(""); |
| 39 | messages.push("• 存在重叠的边:"); |
| 40 | for (const issue of overlappingIssues) { |
| 41 | messages.push(` - ${issue.message}`); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // 3. 环路问题 |
| 46 | const cycleIssues = issuesByType.get("cycle"); |
| 47 | if (cycleIssues && cycleIssues.length > 0) { |
| 48 | messages.push(""); |
| 49 | messages.push("• 存在环路:"); |
| 50 | for (const issue of cycleIssues) { |
| 51 | messages.push(` - ${issue.message}`); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // 4. 菱形结构问题 |
| 56 | const diamondIssues = issuesByType.get("diamond"); |
| 57 | if (diamondIssues && diamondIssues.length > 0) { |
| 58 | messages.push(""); |
| 59 | messages.push("• 存在菱形结构(一个节点有多个父节点):"); |
| 60 | for (const issue of diamondIssues) { |
| 61 | messages.push(` - ${issue.message}`); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | messages.push(""); |
| 66 | messages.push("请修复上述问题后再进行树形格式化。"); |
no test coverage detected