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 | 42x 2x 41x 5x 2x 26x 41x 41x 1x 80x 5x 2x 52x 80x 80x 1x 41x 49x 1x 43x 3x 2x 3x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 84x 29x 35x 84x 70x 70x 15x 27x 70x | /**
* @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[] = [];
// Get root-level modifiers
if (uif.modifiers) {
modifiers.push(...uif.modifiers);
}
// Get structure-level modifiers (recursively traverse structure tree)
function collectStructureModifiers(structure: ResolvedStructure) {
if (structure.modifiers) {
modifiers.push(...structure.modifiers);
}
if (structure.children) {
for (const child of structure.children) {
collectStructureModifiers(child);
}
}
}
collectStructureModifiers(uif.structure);
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[] = [];
// Get root-level variants
if (uif.variants) {
variants.push(...uif.variants);
}
// Get structure-level variants (recursively traverse structure tree)
function collectStructureVariants(structure: ResolvedStructure) {
if (structure.variants) {
variants.push(...structure.variants);
}
if (structure.children) {
for (const child of structure.children) {
collectStructureVariants(child);
}
}
}
collectStructureVariants(uif.structure);
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 ?? [];
}
/**
* Check if a UIF is ready for generation
*/
export function isGenReady(uif: ResolvedUIF): boolean {
return uif.genReady === true;
}
/**
* 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 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;
}
|