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 | 8x 9x 9x 8x 8x 2x 9x 65x 65x 12x 65x 13x 5x 26x 1x 7x 7x 7x 7x 7x 7x 7x 7x 60x | /**
* Structure Analysis
*
* Analyzes UIF structure trees and extracts rendering metadata.
*/
import type { ResolvedStructure } from '@fds-uif/schema';
import type {
StructureMetadata,
SlotMetadata,
ComposedComponentMetadata,
CompositionPropsMetadata,
} from '../types/index.js';
const DEFAULT_ELEMENT = 'div';
/**
* Get nested slot names from structure metadata.
* Note: Consider moving to @fds-uif/core if needed by other packages.
*/
function structureToSlotNames(metadatas: (StructureMetadata | SlotMetadata | ComposedComponentMetadata)[]): string[] {
let slotNames: string[] = [];
metadatas.forEach((metadata) => {
if (metadata.hasOwnProperty('type') && (metadata as SlotMetadata).type === 'slot') {
slotNames.push((metadata as SlotMetadata).name);
return;
}
slotNames = [...slotNames, ...structureToSlotNames((metadata as StructureMetadata).children)];
});
return slotNames;
}
/**
* Analyze a resolved structure and extract rendering metadata
*/
export function analyzeStructure(
structure: ResolvedStructure,
defaultElement: string = DEFAULT_ELEMENT,
): StructureMetadata {
// If this is a component reference, handle specially
const element =
structure.component ?? structure.restrict?.[0] ?? (structure.slot ? undefined : defaultElement);
// Build bound attributes with both attribute name and prop name
const boundAttributes: Array<{ attribute: string; prop: string }> = [];
if (structure.attributes?.bound) {
for (const [attr, config] of Object.entries(structure.attributes.bound)) {
boundAttributes.push({
attribute: attr,
prop: config.prop,
});
}
}
const metadata: StructureMetadata = {
element,
attributes: structure.attributes?.static ?? {},
boundAttributes,
children: [],
};
// Add slot if present
if (structure.slot) {
// for element parents append child
if (metadata.element) {
if (metadata.children) {
metadata.children.push({
type: 'slot',
name: structure.slot.name,
required: structure.slot.required ?? false,
multiple: false,
});
}
} else {
return {
type: 'slot',
name: structure.slot.name,
required: structure.slot.required ?? false,
multiple: false,
} as any as StructureMetadata;
}
}
// Add structural children
if (structure.children) {
for (const child of structure.children) {
metadata.children.push(analyzeStructure(child, defaultElement));
}
}
// Detect variant-based conditional rendering
if (structure.variants && structure.variants.length > 0) {
metadata.conditionals = [];
}
// For slotFilled overwrite parent node chain
if (structure.renderWhen === 'slotFilled' && metadata.children.length) {
const {
element: parentElement,
attributes: parentAttributes,
boundAttributes: parentBoundAttributes,
conditionalAttributes: parentConditionalAttributes,
} = metadata;
metadata.element = undefined;
metadata.attributes = {};
metadata.boundAttributes = [];
metadata.conditionals = metadata.conditionals ?? [];
const slotNames = structureToSlotNames(metadata.children);
if (slotNames.length === 0) {
throw new Error('slotFilled must have atleast one child slot');
}
metadata.conditionals?.push({
condition: slotNames.join(' || '),
trueBranch: {
element: parentElement,
attributes: parentAttributes,
boundAttributes: parentBoundAttributes,
children: [...metadata.children],
conditionalAttributes: parentConditionalAttributes,
},
});
metadata.children = [];
}
return metadata;
}
/**
* Extract component props configuration from ResolvedComponentProps
*/
export function extractComponentPropsMetadata(
componentProps: ResolvedStructure['componentProps'],
): CompositionPropsMetadata {
if (!componentProps) {
return {};
}
return {
static: componentProps.static,
bound: componentProps.bound,
forwarded: componentProps.forwarded,
byVariant: componentProps.byVariant,
restrict: componentProps.restrict,
};
}
|