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

100% Statements 34/34
63.15% Branches 24/38
100% Functions 10/10
100% Lines 30/30

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                                            95x     95x 24x       95x 39x       95x 7x       95x 44x       95x 173x 18x                   95x         24x     24x 18x 6x 4x   5x     24x                     39x   39x 22x 8x                   31x                   7x 32x 7x   32x                 44x                
/**
 * Props Building
 *
 * Transforms UIF states, modifiers, variants, slots, and bound attributes into PropMetadata.
 */
 
import type { ResolvedState, ResolvedModifier, ResolvedVariant, ResolvedSlot } from '@fds-uif/schema';
 
import type { PropMetadata } from '../types/index.js';
import { sanitizeIdentifier } from '../types/index.js';
 
/**
 * Build props from extracted UIF data
 */
export function buildProps(
  states: ResolvedState[],
  modifiers: ResolvedModifier[],
  variants: ResolvedVariant[],
  slots: ResolvedSlot[],
  boundAttrs: Array<{ attribute: string; prop: string; required: boolean }>,
  inferDescriptions: boolean,
): PropMetadata[] {
  const props: PropMetadata[] = [];
 
  // States → props
  for (const state of states) {
    props.push(stateToProps(state, inferDescriptions));
  }
 
  // Modifiers → props
  for (const modifier of modifiers) {
    props.push(modifierToProps(modifier, inferDescriptions));
  }
 
  // Variants → props
  for (const variant of variants) {
    props.push(variantToProps(variant, inferDescriptions));
  }
 
  // Slots → props
  for (const slot of slots) {
    props.push(slotToProps(slot, inferDescriptions));
  }
 
  // Bound attributes → props (avoid duplicates)
  for (const attr of boundAttrs) {
    if (!props.some((p) => p.name === attr.prop)) {
      props.push({
        name: attr.prop,
        type: 'string',
        required: attr.required,
        description: inferDescriptions ? `Bound attribute: ${attr.attribute}` : undefined,
        source: 'attribute',
      });
    }
  }
 
  return props;
}
 
function stateToProps(state: ResolvedState, inferDescriptions: boolean): PropMetadata {
  // Sanitize state name to be a valid JS identifier
  const safeName = sanitizeIdentifier(state.name);
 
  let type: string;
  if (state.type === 'boolean') {
    type = 'boolean';
  } else if (state.type === 'enum' && state.options) {
    type = state.options.map((v) => `'${v}'`).join(' | ');
  } else {
    type = 'string';
  }
 
  return {
    name: safeName,
    type,
    required: false,
    default: state.default,
    description: inferDescriptions ? (state.description ?? `State: ${state.name}`) : undefined,
    source: 'state',
  };
}
 
function modifierToProps(modifier: ResolvedModifier, inferDescriptions: boolean): PropMetadata {
  const safeName = sanitizeIdentifier(modifier.name);
 
  if (modifier.options && modifier.options.length > 0) {
    const typeValues = modifier.options.map((o) => `'${o.propValue}'`).join(' | ');
    return {
      name: safeName,
      type: typeValues,
      required: false,
      default: modifier.default,
      description: inferDescriptions ? (modifier.description ?? `Modifier: ${modifier.name}`) : undefined,
      source: 'variant',
    };
  }
 
  return {
    name: safeName,
    type: 'boolean',
    required: false,
    description: inferDescriptions ? (modifier.description ?? `Modifier: ${modifier.name}`) : undefined,
    source: 'modifier',
  };
}
 
function variantToProps(variant: ResolvedVariant, inferDescriptions: boolean): PropMetadata {
  const safeName = sanitizeIdentifier(variant.name);
  const values = variant.options.map((o) => o.value);
  return {
    name: safeName,
    type: values.map((v) => `'${v}'`).join(' | '),
    required: false,
    default: variant.default ?? values[0],
    description: inferDescriptions ? (variant.description ?? `Variant: ${variant.name}`) : undefined,
    source: 'variant',
  };
}
 
function slotToProps(slot: ResolvedSlot, inferDescriptions: boolean): PropMetadata {
  return {
    name: slot.name === 'default' ? 'children' : slot.name,
    type: 'ReactNode', // Library-specific generators may override
    required: slot.required ?? false,
    description: inferDescriptions ? `Slot: ${slot.name}` : undefined,
    source: 'slot',
  };
}