cacheRefOf

tcmts.core.shape.cacheRefOf · #c6h7rfer · term · runs anywhere

The hash of a codebase type a symbol denotes, when it's declared in a generated cache module.

import { hashFromExportId } from "#codebase/tcmts/core/hash";
import type { TypeChecker } from "#codebase/tcmts/core/ts";
import { SymbolFlags as ts_SymbolFlags } from "typescript/sync";
import type { Symbol as ts_Symbol } from "typescript/sync";

export function cacheRefOf(checker: TypeChecker): (sym: ts_Symbol) => string | undefined {
  const exports = new Map<string, Map<number, string>>();
  return (sym) => {
    const file = sym.declarations?.[0]?.path;
    if (!file || !/[\\/]\.tcm[\\/]cache[\\/][a-z2-7]{52}\.tsx?$/.test(file)) return undefined;
    let byLocal = exports.get(file);
    if (!byLocal) {
      byLocal = new Map();
      const mod = checker.getSymbolOfSourceFile(file);
      for (const e of mod ? checker.getExportsOfModule(mod) : []) {
        const hash = hashFromExportId(e.name);
        const local = e.flags & ts_SymbolFlags.Alias ? checker.getAliasedSymbol(e) : e;
        if (hash) byLocal.set(local.id, hash);
      }
      exports.set(file, byLocal);
    }
    return byLocal.get(sym.id);
  };
}

uses

used by