isLiteralValue

tcmts.cli.suites.isLiteralValue · #nsz26ikd · term · runs anywhere

A constant whose value is written out (a string, a template, an object or array of such, functions in it included), not computed: loading it calls nothing, so it does nothing but be there. What a run did depends on it only through the code that ran reading it.

import { evaluatedOnLoad } from "#codebase/tcmts/cli/suites";
import { initializerOf } from "#codebase/tcmts/core/ir";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function isLiteralValue(rec: DefRecord): boolean {
  if (rec.kind !== "term" || !evaluatedOnLoad(rec)) return false;
  const computes = (n: unknown): boolean =>
    Array.isArray(n) &&
    (/^(CallExpression|NewExpression|TaggedTemplateExpression|AwaitExpression|YieldExpression|ClassExpression|DeleteExpression)$/.test(
      String(n[0]),
    ) ||
      // (a function written in it isn't called by loading it)
      (!/^(ArrowFunction|FunctionExpression|MethodDeclaration)$/.test(String(n[0])) &&
        n.some(computes)));
  const init = initializerOf(rec.ir);
  return !!init && !computes(init);
}

uses

used by