All files / packages/fds-uif/core/src extract.ts

100% Statements 74/74
92.45% Branches 49/53
100% Functions 31/31
100% Lines 66/66

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284                                                                            111x             2x               110x   110x 12x     110x   110x             1x               217x   217x 23x     217x   217x             1x             110x             118x             1x             112x             3x             2x       24x             4x               4x 4x             2x             2x             2x             2x             1x     1x 1x       1x 1x       1x 1x       1x 1x       1x 1x     1x               189x 13x   189x 48x 79x           374x 6x   374x 95x 157x           206x   206x 55x     206x 54x 88x       206x       192x 192x   192x 15x 32x                 192x 49x 80x       192x    
/**
 * @fds-uif/core - Extraction Utilities
 *
 * Utilities for extracting information from resolved UIFs.
 * These are generic utilities useful for tooling, documentation, IDE support, etc.
 */
 
import type {
  ResolvedUIF,
  ResolvedStructure,
  ResolvedState,
  ResolvedModifier,
  ResolvedVariant,
  ResolvedSlot,
  ResolvedStateClass,
} from '@fds-uif/schema';
 
/**
 * Bound attribute with context
 */
export interface ExtractedBoundAttribute {
  /** HTML attribute name */
  attribute: string;
 
  /** Prop name to bind */
  prop: string;
 
  /** Whether the prop is required */
  required: boolean;
 
  /** Path to the structure node (for context) */
  path: string;
}
 
/**
 * Get all states from a resolved UIF
 */
export function getStates(uif: ResolvedUIF): ResolvedState[] {
  return uif.states ?? [];
}
 
/**
 * Get a specific state by name
 */
export function getState(uif: ResolvedUIF, name: string): ResolvedState | undefined {
  return uif.states?.find((s) => s.name === name);
}
 
/**
 * Get all modifiers from a resolved UIF
 * Extracts modifiers from both root level and structure tree
 */
export function getModifiers(uif: ResolvedUIF): ResolvedModifier[] {
  const modifiers: ResolvedModifier[] = [];
 
  if (uif.modifiers) {
    modifiers.push(...uif.modifiers);
  }
 
  collectStructureModifiers(uif.structure, modifiers);
 
  return modifiers;
}
 
/**
 * Get a specific modifier by name
 */
export function getModifier(uif: ResolvedUIF, name: string): ResolvedModifier | undefined {
  return uif.modifiers?.find((m) => m.name === name);
}
 
/**
 * Get all variants from a resolved UIF
 * Extracts variants from both root level and structure tree
 */
export function getVariants(uif: ResolvedUIF): ResolvedVariant[] {
  const variants: ResolvedVariant[] = [];
 
  if (uif.variants) {
    variants.push(...uif.variants);
  }
 
  collectStructureVariants(uif.structure, variants);
 
  return variants;
}
 
/**
 * Get a specific variant by name
 */
export function getVariant(uif: ResolvedUIF, name: string): ResolvedVariant | undefined {
  return uif.variants?.find((v) => v.name === name);
}
 
/**
 * Get all state class mappings from a resolved UIF
 */
export function getStateClasses(uif: ResolvedUIF): ResolvedStateClass[] {
  return uif.stateClasses ?? [];
}
 
/**
 * Get all slots from a resolved UIF (traverses structure)
 */
export function getSlots(uif: ResolvedUIF): ResolvedSlot[] {
  return collectSlots(uif.structure);
}
 
/**
 * Get a specific slot by name
 */
export function getSlot(uif: ResolvedUIF, name: string): ResolvedSlot | undefined {
  return getSlots(uif).find((s) => s.name === name);
}
 
/**
 * Get all bound attributes from a resolved UIF (traverses structure)
 */
export function getBoundAttributes(uif: ResolvedUIF): ExtractedBoundAttribute[] {
  return collectBoundAttributes(uif.structure, '');
}
 
/**
 * Get the default element for a UIF
 */
export function getDefaultElement(uif: ResolvedUIF, fallback = 'div'): string {
  return uif.structure.restrict?.[0] ?? fallback;
}
 
/**
 * Get all allowed elements for a UIF
 */
export function getAllowedElements(uif: ResolvedUIF): string[] {
  return uif.structure.restrict ?? [];
}
 
/** Salesforce UX Foundations namespace key */
const SF_UX_NAMESPACE = 'com.salesforce-ux';
 
/**
 * Check if a UIF is ready for generation.
 * Reads from extensions["com.salesforce-ux"].genReady.
 */
export function isGenReady(uif: ResolvedUIF): boolean {
  return uif.extensions?.[SF_UX_NAMESPACE]?.genReady === true;
}
 
/**
 * Get the date a UIF component was added.
 * Reads from extensions["com.salesforce-ux"].dateAdded.
 */
export function getDateAdded(uif: ResolvedUIF): string | undefined {
  const value = uif.extensions?.[SF_UX_NAMESPACE]?.dateAdded;
  return typeof value === 'string' ? value : undefined;
}
 
/**
 * Check if a UIF has a specific state
 */
export function hasState(uif: ResolvedUIF, name: string): boolean {
  return uif.states?.some((s) => s.name === name) ?? false;
}
 
/**
 * Check if a UIF has a specific modifier
 */
export function hasModifier(uif: ResolvedUIF, name: string): boolean {
  return uif.modifiers?.some((m) => m.name === name) ?? false;
}
 
/**
 * Check if a UIF has a specific variant
 */
export function hasVariant(uif: ResolvedUIF, name: string): boolean {
  return uif.variants?.some((v) => v.name === name) ?? false;
}
 
/**
 * Check if a UIF has a specific slot
 */
export function hasSlot(uif: ResolvedUIF, name: string): boolean {
  return getSlots(uif).some((s) => s.name === name);
}
 
/**
 * Get all prop names that would be needed for this UIF
 */
export function getPropNames(uif: ResolvedUIF): string[] {
  const props = new Set<string>();
 
  // States
  for (const state of uif.states ?? []) {
    props.add(state.name);
  }
 
  // Modifiers
  for (const modifier of uif.modifiers ?? []) {
    props.add(modifier.name);
  }
 
  // Variants
  for (const variant of uif.variants ?? []) {
    props.add(variant.name);
  }
 
  // Slots (default slot becomes 'children')
  for (const slot of getSlots(uif)) {
    props.add(slot.name === 'default' ? 'children' : slot.name);
  }
 
  // Bound attributes
  for (const attr of getBoundAttributes(uif)) {
    props.add(attr.prop);
  }
 
  return Array.from(props);
}
 
// ============================================================================
// Internal Helpers
// ============================================================================
 
function collectStructureModifiers(structure: ResolvedStructure, out: ResolvedModifier[]): void {
  if (structure.modifiers) {
    out.push(...structure.modifiers);
  }
  if (structure.children) {
    for (const child of structure.children) {
      collectStructureModifiers(child, out);
    }
  }
}
 
function collectStructureVariants(structure: ResolvedStructure, out: ResolvedVariant[]): void {
  if (structure.variants) {
    out.push(...structure.variants);
  }
  if (structure.children) {
    for (const child of structure.children) {
      collectStructureVariants(child, out);
    }
  }
}
 
function collectSlots(structure: ResolvedStructure): ResolvedSlot[] {
  const slots: ResolvedSlot[] = [];
 
  if (structure.slot) {
    slots.push(structure.slot);
  }
 
  if (structure.children) {
    for (const child of structure.children) {
      slots.push(...collectSlots(child));
    }
  }
 
  return slots;
}
 
function collectBoundAttributes(structure: ResolvedStructure, path: string): ExtractedBoundAttribute[] {
  const attrs: ExtractedBoundAttribute[] = [];
  const currentPath = path ? `${path}/${structure.name ?? 'root'}` : (structure.name ?? 'root');
 
  if (structure.attributes?.bound) {
    for (const [attr, config] of Object.entries(structure.attributes.bound)) {
      attrs.push({
        attribute: attr,
        prop: config.prop,
        required: config.required ?? false,
        path: currentPath,
      });
    }
  }
 
  if (structure.children) {
    for (const child of structure.children) {
      attrs.push(...collectBoundAttributes(child, currentPath));
    }
  }
 
  return attrs;
}