* Deterministic in-process aggregation. * NOTE: Map-based inputs are not JSON-friendly for future agent calls; we can add a JSON form later.
(
positions: Record<string, ElementAbsolutePosition>,
elementToComponentMap: Map<string, { id: string; name: string; path: string }>,
positionThreshold: number
)
| 60 | * NOTE: Map-based inputs are not JSON-friendly for future agent calls; we can add a JSON form later. |
| 61 | */ |
| 62 | aggregateElements( |
| 63 | positions: Record<string, ElementAbsolutePosition>, |
| 64 | elementToComponentMap: Map<string, { id: string; name: string; path: string }>, |
| 65 | positionThreshold: number |
| 66 | ): AggregateElementsResult { |
| 67 | const componentMap = new Map<string, ComponentData>(); |
| 68 | const skippedElements: SkippedElement[] = []; |
| 69 | |
| 70 | for (const [elementId, position] of Object.entries(positions)) { |
| 71 | if (!position.figmaPosition) { |
| 72 | skippedElements.push({ |
| 73 | elementId, |
| 74 | reason: 'no_figma_position', |
| 75 | details: 'Element exists in render but has no corresponding Figma position data', |
| 76 | }); |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | const component = elementToComponentMap.get(elementId); |
| 81 | if (!component) { |
| 82 | skippedElements.push({ |
| 83 | elementId, |
| 84 | reason: 'missing_component_mapping', |
| 85 | details: 'Element has no component mapping in structure tree', |
| 86 | }); |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | if (!componentMap.has(component.id)) { |
| 91 | componentMap.set(component.id, { |
| 92 | componentId: component.id, |
| 93 | componentName: component.name, |
| 94 | componentPath: component.path, |
| 95 | elementIds: [], |
| 96 | positions: [], |
| 97 | targets: [], |
| 98 | errors: [], |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | const compData = componentMap.get(component.id)!; |
| 103 | compData.elementIds.push(elementId); |
| 104 | compData.positions.push(toRect(position.boundingBox)); |
| 105 | compData.targets.push(toRect(position.figmaPosition)); |
| 106 | compData.errors.push({ |
| 107 | x: Math.abs(position.metrics?.xDelta ?? 0), |
| 108 | y: Math.abs(position.metrics?.yDelta ?? 0), |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | this.logSkippedElementsSummary(skippedElements); |
| 113 | |
| 114 | const misalignedComponents: ComponentMisalignment[] = []; |
| 115 | |
| 116 | for (const compData of componentMap.values()) { |
| 117 | if (compData.positions.length === 0 || compData.targets.length === 0) { |
| 118 | for (const elemId of compData.elementIds) { |
| 119 | skippedElements.push({ |
no test coverage detected