All files / src/mcp-runtime compose-source.ts

100% Statements 3/3
100% Branches 2/2
100% Functions 2/2
100% Lines 3/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                                                3x     4x     3x    
/**
 * Source composition for GenUX cross-component rendering (#1270).
 *
 * Pure string builder (no React/side-effects) so it's unit-testable in isolation
 * from the runtime entry (`index.tsx`, which mounts on import).
 */
 
/** A resolved registry dependency: another component this render composes from. */
export interface RenderDependency {
  /** Name referenced as a JSX tag in the body (a valid identifier). */
  name: string;
  /** The dependency's stored body (calls `render(<… props …>)`, like any component). */
  body: string;
}
 
/**
 * Compose the source react-live evaluates: prepend each resolved dependency as a
 * local component definition, then the root body. Each dependency is the Phase-1
 * `render(<…props…>)` form, so it's wrapped in a `(props) => element` function with
 * a *local* `render` capture (mirroring react-live `noInline`); the outer `render`
 * the root calls stays react-live's. Dependencies are emitted deepest-first so a
 * dependency can reference earlier ones; built-ins remain in the outer scope.
 */
export function composeSource(source: string, components: readonly RenderDependency[]): string {
  const defs = components
    .map(
      (d) =>
        `const ${d.name} = (props) => { let __el; const render = (el) => { __el = el; };\n${d.body}\n; return __el; };`,
    )
    .join("\n");
  return defs ? `${defs}\n${source}` : source;
}