reviewItems

tcmts.share.reviewItems · #2to6dl4f · term · runs on the server

uses tcmts.share.definitionLines, which uses tcmts.cli.files.fileContent, which uses Buffer

The changed definitions a namespace diff holds, each with its lines, coloured (#ky3fk2il): what's added or removed, whole; what's edited or reworded, as a line diff; what's renamed or conflicted, by name. oldNames and newNames name what the code refers to, before and after.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { keyPath } from "#codebase/tcmts/cli/diff";
import type { NamespaceDiff } from "#codebase/tcmts/cli/diff";
import { isTestPath } from "#codebase/tcmts/cli/testing";
import { definitionLines, diffWithHtml, highlightedLines } from "#codebase/tcmts/share";
import type { ReviewItem } from "#codebase/tcmts/share";

export function reviewItems(
  cb: Codebase,
  d: NamespaceDiff,
  oldNames: (h: string) => string | undefined,
  newNames: (h: string) => string | undefined,
): ReviewItem[] {
  const test = (p: string) => isTestPath(p);
  // a definition's lines (a text version of it, if asked), and each as HTML
  // (the first thirty read exactly: in a merge of hundreds, the rest by the scanner, so the page comes in seconds)
  let exact = 30;
  const side = (
    h: string,
    names: (h: string) => string | undefined,
    text?: { version: string | undefined },
  ) => {
    const lines = definitionLines(cb, h, names, text);
    return { lines, html: highlightedLines(cb, h, lines, exact-- > 0) };
  };
  const whole = (op: "+" | "-", x: { lines: string[]; html: string[] }) => ({
    lines: x.lines.map((l) => [op, l] as [" " | "+" | "-", string]),
    html: x.html,
  });
  const diff = diffWithHtml;
  return [
    ...d.added.map((a): ReviewItem => ({
      change: "added",
      path: keyPath(a.path),
      hash: a.hash,
      ...whole("+", side(a.hash, newNames)),
      test: test(keyPath(a.path)),
    })),
    ...d.edited.map((e): ReviewItem => ({
      change: "edited",
      path: keyPath(e.path),
      hash: e.new,
      old: e.old,
      ...diff(side(e.old, oldNames), side(e.new, newNames)),
      test: test(keyPath(e.path)),
    })),
    ...d.reworded.map((r): ReviewItem => ({
      change: "reworded",
      path: keyPath(r.path),
      hash: r.hash,
      ...diff(
        side(r.hash, oldNames, { version: r.old }),
        side(r.hash, newNames, { version: r.new }),
      ),
      test: test(keyPath(r.path)),
    })),
    ...d.renamed.map((r): ReviewItem => ({
      change: "renamed",
      path: keyPath(r.to),
      from: keyPath(r.from),
      hash: r.hash,
      lines: [],
      test: test(keyPath(r.to)),
    })),
    ...d.removed.map((r): ReviewItem => ({
      change: "removed",
      path: keyPath(r.path),
      old: r.hash,
      ...whole("-", side(r.hash, oldNames)),
      test: test(keyPath(r.path)),
    })),
    ...d.conflicted.map((x): ReviewItem => ({
      change: "conflicted",
      path: keyPath(x.path),
      lines: [],
      test: false,
    })),
  ];
}

uses

used by