replacedUses

tcmts.cli.main.replacedUses · #xffor2wr · term · runs anywhere

What definitions use that nothing names now (a todo item's, made against the versions before a change it couldn't follow), as a scratch file writes it: the version that replaced it, by its name (that's what it's to be fixed against, and the reason it's in todo), or, where nothing names that, the version used, by its hash (byHash: its module must be in the cache for the file to check). notes says which, a line each.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { shortHash } from "#codebase/tcmts/core/hash";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function replacedUses(
  cb: Codebase,
  recs: DefRecord[],
): { notes: string[]; byHash: string[] } {
  const own = new Set(recs.map((r) => r.hash));
  const seen = new Set<string>();
  const notes: string[] = [];
  const byHash: string[] = [];
  for (const r of recs)
    for (const d of r.deps) {
      const h = d.ref.startsWith("#") ? d.ref.slice(1) : undefined;
      if (!h || own.has(h) || seen.has(h)) continue;
      seen.add(h);
      if (cb.store.pathsOf(h).length) continue;
      const now = cb.store.patched(h);
      const name = cb.store.pathsOf(now)[0];
      if (name)
        notes.push(
          `${name.slice(name.lastIndexOf(".") + 1)}: imported as ${name} (the version it used, ${shortHash(h)}, is replaced)`,
        );
      else {
        byHash.push(now);
        notes.push(
          `${cb.store.getDef(now)?.nameHint ?? shortHash(now)}: imported by its hash, ${shortHash(now)}, as nothing names it or a version that replaced it`,
        );
      }
    }
  return { notes, byHash };
}

uses

used by