holdsState

tcmts.cli.kept.holdsState · #yvro7bfr · term · runs on the server

uses tcmts.cli.kept.computedInitializer, which uses tcmts.core.ts.parse, which uses tcmts.core.ts.compiler, which uses process

What a definition holds, as module state goes: nothing (a function, a type); a value nothing can change (a constant written as a number, a string, true…); maybe one (a computed constant: if its kept value is one of those); or state something could change (an object, an array, a top-level let, a class's statics).

import { computedInitializer } from "#codebase/tcmts/cli/kept";
import type { IR } from "#codebase/tcmts/core/hash";
import { defParts, initializerOf } from "#codebase/tcmts/core/ir";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function holdsState(rec: DefRecord): "none" | "fixed" | "maybe" | "state" {
  if (rec.kind === "type") return "none";
  if (rec.kind !== "term") return "state";
  const ir = JSON.parse(rec.ir) as IR[];
  const { tag: def } = defParts(ir);
  if (def === "Function") return "none";
  if (def !== "Const") return "state";
  const init = initializerOf(ir);
  const tag = init?.[0];
  if (tag === "ArrowFunction" || tag === "FunctionExpression") return "none";
  if (["Num", "Str", "BigInt", "TrueKeyword", "FalseKeyword", "NullKeyword"].includes(String(tag)))
    return "fixed";
  if (tag === "PrefixUnaryExpression" && JSON.stringify(init).includes('["Num"')) return "fixed";
  return computedInitializer(rec) ? "maybe" : "state";
}

uses

used by