All files / packages/fds-uif/generator-base/src/analyzer dependencies.ts

100% Statements 8/8
66.66% Branches 2/3
100% Functions 0/0
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                8x                       39x     65x     2x             5x             26x         39x 39x    
/**
 * Dependencies Detection
 *
 * Detects component dependencies from UIF structure.
 */
 
import type { ResolvedStructure } from '@fds-uif/schema';
 
const DEFAULT_ELEMENT = 'div';
 
/**
 * Detect component dependencies from structure
 *
 * Walks the structure tree and identifies PascalCase elements
 * and slot restrictions as component references.
 */
export function detectDependencies(
  structure: ResolvedStructure,
  defaultElement: string = DEFAULT_ELEMENT,
): string[] {
  const deps = new Set<string>();
 
  function walk(node: ResolvedStructure) {
    const element = node.restrict?.[0] ?? defaultElement;
    // PascalCase elements are component references
    if (/^[A-Z]/.test(element)) {
      deps.add(element);
    }
 
    // Check slot restrictions
    if (node.slot?.restrict) {
      for (const allowed of node.slot.restrict) {
        if (/^[A-Z]/.test(allowed)) {
          deps.add(allowed);
        }
      }
    }
 
    if (node.children) {
      for (const child of node.children) {
        walk(child);
      }
    }
  }
 
  walk(structure);
  return Array.from(deps);
}