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 | 16x 25x 25x 26x 22x 22x 4x 4x 25x 165x 25x 44x 44x 14x 44x 25x 44x 22x 21x 21x 21x 1x 20x 22x 22x 22x 22x 22x 22x 22x 174x 9x 165x 174x 174x 44x 44x 25x 146x 146x 146x 69x 146x 3x 146x 22x 145x 146x 146x 48x 79x 9x 9x 1x 9x 9x 1x 9x 9x 4x 5x | /**
* 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';
import { buildNodeClassNameRules } from './classNames.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 ('type' in metadata && (metadata as SlotMetadata).type === 'slot') {
slotNames.push((metadata as SlotMetadata).name);
return;
}
Iif ('type' in metadata && (metadata as ComposedComponentMetadata).type === 'component') {
return;
}
slotNames = [...slotNames, ...structureToSlotNames((metadata as StructureMetadata).children)];
});
return slotNames;
}
/** Extract bound attributes from a structure node into attribute/prop pairs. */
function buildBoundAttributes(structure: ResolvedStructure): Array<{ attribute: string; prop: string }> {
if (!structure.attributes?.bound) return [];
return Object.entries(structure.attributes.bound).map(([attr, config]) => ({
attribute: attr,
prop: config.prop,
}));
}
/** Build SlotMetadata from a structure node that declares a slot. */
function buildSlotMetadata(structure: ResolvedStructure): SlotMetadata {
const slotMeta: SlotMetadata = {
type: 'slot',
name: structure.slot!.name,
required: structure.slot!.required ?? false,
multiple: false,
};
if (structure.slot!.restrict) {
slotMeta.restrict = structure.slot!.restrict;
}
if (structure.description) {
slotMeta.description = structure.description;
}
return slotMeta;
}
/**
* Transform a `renderWhen: "slotFilled"` node into a conditional structure.
* The original element is moved into the conditional's trueBranch and the
* metadata becomes a wrapper with empty element/attributes.
*/
function applySlotFilledConditional(metadata: StructureMetadata): void {
if (metadata.children.length === 0) return;
const {
element: parentElement,
attributes: parentAttributes,
boundAttributes: parentBoundAttributes,
conditionalAttributes: parentConditionalAttributes,
} = metadata;
const slotNames = structureToSlotNames(metadata.children);
if (slotNames.length === 0) {
throw new Error('slotFilled must have atleast one child slot');
}
const conditionals = metadata.conditionals ?? [];
conditionals.push({
condition: slotNames.join(' || '),
trueBranch: {
element: parentElement,
attributes: parentAttributes,
boundAttributes: parentBoundAttributes,
children: [...metadata.children],
conditionalAttributes: parentConditionalAttributes,
},
});
metadata.element = undefined;
metadata.attributes = {};
metadata.boundAttributes = [];
metadata.renderWhen = undefined;
metadata.conditionals = conditionals;
metadata.children = [];
}
/**
* Analyze a resolved structure and extract rendering metadata.
*
* When a node declares `component` with a wrapper element (`restrict`), the
* wrapper becomes a StructureMetadata and the composed component becomes a
* ComposedComponentMetadata child inside it. When `component` is set without
* `restrict`, a bare ComposedComponentMetadata is returned.
*/
export function analyzeStructure(
structure: ResolvedStructure,
defaultElement: string = DEFAULT_ELEMENT,
): StructureMetadata | ComposedComponentMetadata {
if (structure.component && !structure.restrict?.length) {
return buildComposedComponentMetadata(structure);
}
const element = structure.restrict?.[0] ?? (structure.slot ? undefined : defaultElement);
const metadata: StructureMetadata = {
name: structure.name,
element,
attributes: structure.attributes?.static ?? {},
boundAttributes: buildBoundAttributes(structure),
renderWhen: structure.renderWhen,
children: [],
};
if (structure.slot) {
const slotMeta = buildSlotMetadata(structure);
if (!metadata.element) return slotMeta as any as StructureMetadata;
metadata.children.push(slotMeta);
}
populateChildren(metadata, structure, defaultElement);
const nodeClassNames = buildNodeClassNameRules(structure);
if (nodeClassNames.length > 0) {
metadata.classNames = nodeClassNames;
}
if (structure.variants?.length) {
metadata.conditionals = [];
}
if (structure.renderWhen === 'slotFilled') {
applySlotFilledConditional(metadata);
}
return metadata;
}
/** Add composed component and structural children to the metadata node. */
function populateChildren(
metadata: StructureMetadata,
structure: ResolvedStructure,
defaultElement: string,
): void {
Iif (structure.component && structure.restrict?.length) {
metadata.children.push(buildComposedComponentMetadata(structure));
}
if (structure.children) {
for (const child of structure.children) {
metadata.children.push(analyzeStructure(child, defaultElement));
}
}
}
/** Build a ComposedComponentMetadata from a structure node that has `component`. */
function buildComposedComponentMetadata(structure: ResolvedStructure): ComposedComponentMetadata {
const meta: ComposedComponentMetadata = {
type: 'component',
name: structure.name,
component: structure.component!,
props: extractComponentPropsMetadata(structure.componentProps),
};
if (structure.renderWhen) {
meta.renderWhen = structure.renderWhen;
}
const nodeClassNames = buildNodeClassNameRules(structure);
if (nodeClassNames.length > 0) {
meta.classNames = nodeClassNames;
}
return meta;
}
/** 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,
};
}
|