(node?: Protocol | null, parentPath?: string, level = 0)
| 256 | }; |
| 257 | |
| 258 | const traverse = (node?: Protocol | null, parentPath?: string, level = 0): void => { |
| 259 | if (!node || !node.data) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | // 1. Normalize componentName (from top-level to data field) |
| 264 | const extendedNode = node as ExtendedFrameStructNode; |
| 265 | const topLevelComponentName = extendedNode.componentName; |
| 266 | if (topLevelComponentName && !node.data.componentName) { |
| 267 | node.data.componentName = topLevelComponentName; |
| 268 | delete extendedNode.componentName; |
| 269 | } |
| 270 | |
| 271 | // 2. Populate elements data from elementIds |
| 272 | if (frames) { |
| 273 | const nodeData = node.data as FrameData & { elementIds?: string[] }; |
| 274 | const elementIds = nodeData.elementIds; |
| 275 | if (elementIds && Array.isArray(elementIds)) { |
| 276 | if (elementIds.length > 0) { |
| 277 | node.data.elements = extractHierarchicalNodesByIds(frames, elementIds, { includeSubtree: true }); |
| 278 | } else { |
| 279 | node.data.elements = []; |
| 280 | } |
| 281 | delete nodeData.elementIds; |
| 282 | } else { |
| 283 | node.data.elements = node.data.elements || []; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // 3. Annotate with file system paths |
| 288 | const segment = toKebabName(node); |
| 289 | let currentPath: string; |
| 290 | |
| 291 | if (level === 0) { |
| 292 | // Root node always uses base path |
| 293 | currentPath = rootPath; |
| 294 | rootPath = currentPath; |
| 295 | } else { |
| 296 | const ancestorPath = parentPath || rootPath; |
| 297 | currentPath = joinSegments(ancestorPath, segment); |
| 298 | } |
| 299 | |
| 300 | // For reusable components, generate flat componentPath (non-hierarchical) |
| 301 | if (node.data.componentName) { |
| 302 | const componentKebabName = toKebabCase(node.data.componentName); |
| 303 | node.data.componentPath = joinSegments(rootPath, componentKebabName); |
| 304 | node.data.path = node.data.componentPath; |
| 305 | } |
| 306 | |
| 307 | node.data.path = currentPath; |
| 308 | |
| 309 | // Recursively process children |
| 310 | if (Array.isArray(node.children) && node.children.length > 0) { |
| 311 | node.children.forEach(child => traverse(child, node.data.path, level + 1)); |
| 312 | } |
| 313 | }; |
| 314 | |
| 315 | nodes.forEach(node => { |
no test coverage detected