codeFor

tcmts.cli.mirror.codeFor · #blgruzvw · term · runs on the server

uses tcmts.core.ts.parse, which uses tcmts.core.ts.compiler, which uses process

A definition's code as it reads in its mirror file.

import type { Spelling } from "#codebase/tcmts/cli/mirror";
import { parse } from "#codebase/tcmts/core/ts";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function codeFor(
  rec: DefRecord,
  leaf: string,
  spelling: Spelling,
  exported: boolean,
): string {
  let text = rec.sourceText;
  for (const s of [...rec.spans].sort((a, b) => b.start - a.start)) {
    if (s.literal) continue;
    const written = text.slice(s.start, s.end);
    let spelled = spelling.spell(s, written, rec.hash, leaf);
    if (s.shorthand && spelled !== s.shorthand) spelled = `${s.shorthand}: ${spelled}`;
    text = text.slice(0, s.start) + spelled + text.slice(s.end);
  }
  if (!exported) return text;
  // (JSX only parses as .tsx)
  const starts = parse(rec.jsx ? "code.tsx" : "code.ts", text).statements.map((st) =>
    st.getStart(),
  );
  for (const at of [...starts].reverse()) text = text.slice(0, at) + "export " + text.slice(at);
  return text;
}

uses

used by