All files / packages/fds-uif/generator-base/src/types index.ts

50% Statements 2/4
100% Branches 0/0
0% Functions 0/1
66.66% Lines 2/3

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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365                                55x                             55x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
/**
 * Core type definitions for UIF code generators
 *
 * These are generation-specific types that build on the base types from @fds-uif/schema.
 * The pipeline is: Resolved UIF → Analyze → Generate → Format
 */
 
// ============================================================================
// Utility Functions
// ============================================================================
 
/**
 * Convert kebab-case to camelCase for valid JS identifiers
 * e.g., 'outline-brand' → 'outlineBrand'
 */
export function toCamelCase(str: string): string {
  return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
}
 
/**
 * Check if a string is a valid JavaScript identifier
 */
export function isValidJsIdentifier(str: string): boolean {
  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(str);
}
 
/**
 * Sanitize a name to be a valid JS identifier
 * Converts kebab-case to camelCase
 */
export function sanitizeIdentifier(name: string): string {
  return toCamelCase(name);
}
 
// ============================================================================
// Re-exports
// ============================================================================
 
// Re-export resolved UIF types from schema for convenience
export type {
  ResolvedUIF,
  ResolvedStructure,
  ResolvedAttributes,
  ResolvedBoundAttribute,
  ResolvedConditionalAttribute,
  ResolvedSlot,
  ResolvedState,
  ResolvedModifier,
  ResolvedVariant,
  ResolvedVariantOption,
  ResolvedStateClass,
  ResolvedAccessibility,
} from '@fds-uif/schema';
 
// ============================================================================
// Output Types (for generators)
// ============================================================================
 
/**
 * Component metadata extracted by analyzer
 *
 * This is what generators consume to produce library-specific code.
 */
export interface ComponentMetadata {
  /** Component name */
  name: string;
 
  /** Component description */
  description: string;
 
  /** Inferred component props */
  props: PropMetadata[];
 
  /** Structure tree with rendering instructions */
  structure: StructureMetadata;
 
  /** CSS class composition rules */
  classNames: ClassNameRule[];
 
  /** Component dependencies (other components referenced) */
  dependencies: string[];
 
  /** Composed component metadata */
  compositions?: CompositionMetadata[];
 
  /** Interface declarations for composed components */
  componentRefs?: Record<string, ComponentRefMetadata>;
 
  /** Accessibility metadata */
  accessibility?: {
    role?: string;
    ariaProps?: string[];
    landmarks?: Record<string, string>;
  };
 
  /** Behavior module reference */
  behavior?: string;
 
  /** Path to component's CSS file */
  cssSource?: string;
}
 
/**
 * Prop metadata for type generation
 */
export interface PropMetadata {
  /** Prop name */
  name: string;
 
  /** TypeScript type */
  type: string;
 
  /** Whether prop is required */
  required: boolean;
 
  /** Default value */
  default?: unknown;
 
  /** Prop description */
  description?: string;
 
  /** Source (state, modifier, variant, slot, attribute) */
  source: 'state' | 'modifier' | 'variant' | 'slot' | 'attribute';
 
  /** Structure path where this prop is defined (for nested modifiers/variants) */
  path?: string;
}
 
/**
 * Composition metadata for embedded components
 */
export interface CompositionMetadata {
  /** Name/identifier of the structure node */
  nodeName: string;
 
  /** Referenced component name */
  component: string;
 
  /** Structure path to this composition */
  path: string;
 
  /** Component props configuration */
  props: CompositionPropsMetadata;
}
 
/**
 * Props configuration for composed components
 */
export interface CompositionPropsMetadata {
  /** Hardcoded prop values */
  static?: Record<string, unknown>;
 
  /** Props bound to parent props */
  bound?: Record<string, string>;
 
  /** Props passed through unchanged */
  forwarded?: string[];
 
  /** Props determined by variant values */
  byVariant?: Record<string, Record<string, Record<string, unknown>>>;
 
  /** Allowed props (narrowed interface) */
  restrict?: string[];
}
 
/**
 * Component reference interface metadata
 */
export interface ComponentRefMetadata {
  /** Description of the component reference */
  description?: string;
 
  /** Expected props interface */
  props: Record<string, ComponentRefPropMetadata>;
}
 
/**
 * Component reference prop metadata
 */
export interface ComponentRefPropMetadata {
  /** TypeScript type */
  type: string;
 
  /** Whether the prop is required */
  required?: boolean;
 
  /** Prop description */
  description?: string;
}
 
/**
 * Structure metadata with rendering instructions
 */
export interface StructureMetadata {
  /** Node name/identifier */
  name?: string | undefined;
 
  /** Element type to render */
  element: string | undefined;
 
  /** Static attributes */
  attributes: Record<string, string | boolean | number>;
 
  /** Dynamic attributes (bound to props) */
  boundAttributes: BoundAttributeMapping[];
 
  /** Conditional attributes */
  conditionalAttributes?: ConditionalAttributeMetadata[];
 
  /** Children (slots, structural nodes, or composed components) */
  children: (SlotMetadata | StructureMetadata | ComposedComponentMetadata)[];
 
  /** Conditional rendering rules */
  conditionals?: ConditionalRule[];
 
  /** renderWhen expression from UIF */
  renderWhen?: string;
}
 
/**
 * Bound attribute mapping (attribute name to prop name)
 */
export interface BoundAttributeMapping {
  /** HTML attribute name (e.g., 'aria-disabled', 'accesskey') */
  attribute: string;
 
  /** Component prop name (e.g., 'disabled', 'accessKey') */
  prop: string;
}
 
/**
 * Conditional attribute metadata
 */
export interface ConditionalAttributeMetadata {
  /** Attribute name */
  attribute: string;
 
  /** Value to set */
  value: string;
 
  /** Condition expression (prop name or expression) */
  when: string;
}
 
/**
 * Composed component in structure
 */
export interface ComposedComponentMetadata {
  type: 'component';
 
  /** Node name */
  name?: string;
 
  /** Component reference name */
  component: string;
 
  /** Props configuration */
  props: CompositionPropsMetadata;
}
 
/**
 * Slot metadata
 */
export interface SlotMetadata {
  type: 'slot';
  name: string;
  required: boolean;
  multiple: boolean;
}
 
/**
 * Conditional rendering rule
 */
export interface ConditionalRule {
  condition: string;
  trueBranch: StructureMetadata;
  falseBranch?: StructureMetadata;
}
 
/**
 * CSS class composition rule
 */
export interface ClassNameRule {
  /** Base classes (always applied) */
  base: string[];
 
  /** Conditional classes (prop-dependent) */
  conditional: Array<{
    classes: string[];
    condition: string;
  }>;
}
 
// ============================================================================
// Generated Output Types
// ============================================================================
 
/**
 * Generated code output
 */
export interface GeneratedCode {
  files: GeneratedFile[];
  metadata: ComponentMetadata;
}
 
/**
 * Generated file
 */
export interface GeneratedFile {
  /** File path relative to output directory */
  path: string;
 
  /** File contents */
  content: string;
 
  /** File type (for formatter selection) */
  type: 'typescript' | 'javascript' | 'html' | 'lwc' | 'css' | 'json';
}
 
// ============================================================================
// Options Types
// ============================================================================
 
/**
 * Analyzer options
 */
export interface AnalyzerOptions {
  /** Default element when restrict is empty */
  defaultElement?: string;
 
  /** Infer prop descriptions from UIF */
  inferDescriptions?: boolean;
 
  /** Validate UIF before analysis (default: true) */
  validate?: boolean;
}
 
/**
 * Formatter options
 */
export interface FormatterOptions {
  /** Custom Prettier config */
  prettier?: Record<string, unknown>;
 
  /** Skip formatting */
  skip?: boolean;
}
 
/**
 * Generator options
 */
export interface GeneratorOptions {
  /** Output directory */
  outputDir: string;
 
  /** Format generated code with Prettier */
  format?: boolean;
 
  /** TypeScript strict mode */
  strict?: boolean;
 
  /** Custom prettier config */
  prettierConfig?: Record<string, unknown>;
}