nameChanges

tcmts.cli.main.nameChanges · #twye2lwx · term · runs on the server

uses tcmts.cli.style.yellow, which uses node:util

What changed between two states, name by name.

import { keyPath } from "#codebase/tcmts/cli/diff";
import type { NameState } from "#codebase/tcmts/cli/diff";
import { green, red, yellow } from "#codebase/tcmts/cli/style";
import { shortHash } from "#codebase/tcmts/core/hash";

export function nameChanges(
  before: NameState,
  after: NameState,
  // the text versions each side's definitions show: the same code reading differently is a change too
  texts?: { before: ReadonlyMap<string, string>; after: ReadonlyMap<string, string> },
): string[] {
  const out: string[] = [];
  const list = (hs: Set<string> | undefined) => [...(hs ?? [])].map((h) => shortHash(h)).join(", ");
  for (const key of [...new Set([...before.keys(), ...after.keys()])].sort()) {
    const [b, a] = [before.get(key), after.get(key)];
    if (b && a && b.size === a.size && [...b].every((h) => a.has(h))) {
      if (texts && [...a].some((h) => texts.before.get(h) !== texts.after.get(h)))
        out.push(
          `    ${keyPath(key).padEnd(28)} ${yellow(`${list(a)} reads differently (comments, layout or doc)`)}`,
        );
      continue;
    }
    const change = !b
      ? green(`+ ${list(a)}`)
      : !a
        ? red(`- ${list(b)}`)
        : yellow(`${list(b)} → ${list(a)}`);
    out.push(`    ${keyPath(key).padEnd(28)} ${change}`);
  }
  return out;
}

uses

used by