isDeferred

tcmts.core.canonicalize.isDeferred · #u45nka2u · term · runs anywhere

A reference is deferred when it is only evaluated later (inside a function body or instance field initializer). A function called where it's written ((() => { … })()) runs now: what it uses isn't deferred.

import { invokedWhereWritten } from "#codebase/tcmts/core/canonicalize";
import { getModifiers, isFunctionLike } from "#codebase/tcmts/core/ts";
import { isPropertyDeclaration as ts_isPropertyDeclaration, SyntaxKind as ts_SyntaxKind } from "typescript/ast";
import type { Node as ts_Node } from "typescript/ast";

export function isDeferred(id: ts_Node, roots: readonly ts_Node[]): boolean {
  let n: ts_Node = id;
  while (!roots.includes(n) && n.parent) {
    const p: ts_Node = n.parent;
    if (isFunctionLike(p) && (p as { name?: ts_Node }).name !== n && !invokedWhereWritten(p))
      return true;
    if (ts_isPropertyDeclaration(p) && p.initializer === n) {
      const isStatic = (getModifiers(p) ?? []).some((m) => m.kind === ts_SyntaxKind.StaticKeyword);
      if (!isStatic) return true;
    }
    n = p;
  }
  return false;
}

uses

used by