patchLocations

tcmts.cli.main.patchLocations · #2x6tuujf · term · runs on the server

uses tcmts.cli.mirror.declarations, which uses tcmts.core.ts.parse, which uses tcmts.core.ts.compiler, which uses process

Where a place in a patch's file (patch-123.ts:51:44, as the checker and linter say) is in the definitions patched: tcmts.lib.f:12:44, the line as tcm view numbers it. A rewriter for what's printed while the file is checked and saved (the file is gone afterwards, and its lines aren't the view's): see locationRewriter. Where each declaration starts in the view is found from the file before it was patched (an edit can change its first line).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { locationRewriter } from "#codebase/tcmts/cli/main";
import { declarations } from "#codebase/tcmts/cli/mirror";
import { render } from "#codebase/tcmts/loader";

export function patchLocations(
  cb: Codebase,
  file: string,
  text: string,
  names: string[],
  before: string = text,
): (s: string) => string {
  const nameOf = (leaf: string) => names.find((n) => n === leaf || n.endsWith(`.${leaf}`)) ?? leaf;
  // the view's line of each declaration's first line
  const lines = before.split("\n");
  const viewAt = new Map<string, number>();
  for (const [leaf, d] of declarations(file, before)) {
    const rec = cb
      .lookup(nameOf(leaf))
      .map((h) => cb.store.getDef(h))
      .find(Boolean);
    if (!rec) continue;
    const view = render([rec], cb.source(), cb.lay, {
      style: "scratch",
      exportDeclarations: true,
    }).text.split("\n");
    const first = lines[before.slice(0, d.start).split("\n").length - 1]!.trim();
    viewAt.set(
      leaf,
      view.findIndex((l) => l.trim() === first),
    );
  }
  const starts = [...declarations(file, text)].map(([leaf, d]) => ({
    name: nameOf(leaf),
    line: text.slice(0, d.start).split("\n").length - 1,
    end: text.slice(0, d.end).split("\n").length - 1,
    viewAt: viewAt.get(leaf) ?? -1,
  }));
  return locationRewriter(file, starts, names);
}

uses

used by