importedFrom

tcmts.cli.main.importedFrom · #q5wokjyn · term · runs anywhere

Where the codebase's own code imports name from outside it (a package's or Node's export), most often: the import (import { spawnSync } from "node:child_process", or import fs from "node:fs" for a default export the code calls fs, or import { Node as ts_Node } from "typescript/ast" for an export the code calls ts_Node), or undefined if nothing does.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { specifierOf } from "#codebase/tcmts/cli/mirror";

export function importedFrom(cb: Codebase, name: string): string | undefined {
  const counts = new Map<string, number>();
  for (const h of new Set(cb.store.names().map((n) => n.hash))) {
    const rec = cb.store.getDef(h);
    for (const s of rec?.spans ?? []) {
      if (!s.ext) continue;
      const spec = JSON.stringify(specifierOf(s.ext));
      const written = rec!.sourceText.slice(s.start, s.end);
      const line =
        s.ext.export === name
          ? `import { ${name} } from ${spec};`
          : written !== name
            ? undefined
            : s.ext.export === "default"
              ? `import ${name} from ${spec};`
              : s.ext.export === "*"
                ? `import * as ${name} from ${spec};`
                : `import { ${s.ext.export} as ${name} } from ${spec};`;
      if (line) counts.set(line, (counts.get(line) ?? 0) + 1);
    }
  }
  return [...counts].sort((a, b) => b[1] - a[1])[0]?.[0];
}

uses

used by