keptValue

tcmts.cli.kept.keptValue · #4xfjag2a · term · runs on the server

uses tcmts.cli.kept.keptKey, which uses process

A constant's kept value (DefSource.kept): null if it isn't kept, undefined if that isn't known yet. Its initializer's place is found again in the text shown now, when a rename since moved it (a constant's name isn't in its hash, so tcm mv changes its text: const indexOpened = … to const hubOpened = …).

import type { CodebaseCore } from "#codebase/tcmts/cli/codebase";
import { computedInitializer, keptKey } from "#codebase/tcmts/cli/kept";
import type { DefRecord } from "#codebase/tcmts/core/types";
import type { Kept } from "#codebase/tcmts/loader";

export function keptValue(cb: CodebaseCore, rec: DefRecord | undefined): Kept | null | undefined {
  // (most definitions aren't constants: they're seen not to be at once)
  if (
    !rec ||
    rec.kind !== "term" ||
    rec.componentSize !== 1 ||
    rec.initKind !== "strict" ||
    !rec.sourceText.startsWith("const ")
  )
    return null;
  const got = cb.store.getMetadata(rec.hash, keptKey());
  const k = got === undefined ? undefined : (JSON.parse(got) as Kept | null);
  if (!k || /^const [\w$]+(\s*:[^=]*)?\s*=\s*$/.test(rec.sourceText.slice(0, k.start))) return k;
  const at = computedInitializer(rec);
  return at ? { ...k, ...at } : null;
}

uses

used by