namesChanged

tcmts.cli.changes.namesChanged · #ndzkpcgn · term · runs anywhere

What changed from one state of the names to another, name by name.

import type { NameChange } from "#codebase/tcmts/cli/changes";
import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { diffNamespaces, keyPath } from "#codebase/tcmts/cli/diff";
import type { NameState } from "#codebase/tcmts/cli/diff";
import { shortHash } from "#codebase/tcmts/core/hash";

export function namesChanged(
  cb: Codebase,
  before: NameState,
  after: NameState,
  viewUri: (hash: string, side: "before" | "after") => string,
): NameChange[] {
  const d = diffNamespaces(before, after, (h) => cb.store.getDef(h)!.ir);
  // each side's document reads with that side's names
  const uri = (h: string, side: "before" | "after" = "after") => {
    const rec = cb.store.getDef(h);
    return rec && rec.kind !== "file" ? { uri: viewUri(h, side) } : {};
  };
  const was = (h: string) => {
    const u = uri(h, "before");
    return u.uri ? { oldUri: u.uri } : {};
  };
  const nameOf = (h: string) => cb.store.pathsOf(h)[0] ?? shortHash(h);
  return [
    ...d.edited.map((x): NameChange => ({
      change: "edited",
      path: keyPath(x.path),
      old: x.old,
      hash: x.new,
      ...was(x.old),
      ...uri(x.new),
    })),
    ...d.propagated.map((x): NameChange => ({
      change: "propagated",
      path: keyPath(x.path),
      old: x.old,
      hash: x.new,
      because: x.because.map(nameOf),
      ...was(x.old),
      ...uri(x.new),
    })),
    ...d.added.map((x): NameChange => ({
      change: "added",
      path: keyPath(x.path),
      hash: x.hash,
      ...uri(x.hash),
    })),
    ...d.removed.map((x): NameChange => ({
      change: "removed",
      path: keyPath(x.path),
      hash: x.hash,
      ...uri(x.hash, "before"),
    })),
    ...d.renamed.map((x): NameChange => ({
      change: "renamed",
      path: keyPath(x.to),
      from: keyPath(x.from),
      hash: x.hash,
      ...uri(x.hash),
    })),
    ...d.aliased.map((x): NameChange => ({
      change: "aliased",
      path: keyPath(x.path),
      hash: x.hash,
      existing: x.existing.map(keyPath),
      ...uri(x.hash),
    })),
    ...d.conflicted.map((x): NameChange => ({
      change: "conflicted",
      path: keyPath(x.path),
      hashes: x.hashes,
    })),
  ];
}

uses

used by