defParts

tcmts.core.ir.defParts · #6xp7zzhr · term · runs anywhere

A definition's IR (as stored, or parsed) in its parts: its tag, salt and JSX runtime, and its syntax, wherever each is (see DefParts). Something that isn't a definition's IR (a file's) has no tag and no syntax.

import type { IR } from "#codebase/tcmts/core/hash";
import type { DefParts } from "#codebase/tcmts/core/ir";

export function defParts(ir: IR[] | string): DefParts {
  const nodes = typeof ir === "string" ? (JSON.parse(ir) as IR) : ir;
  if (!Array.isArray(nodes) || nodes[0] !== "Def" || typeof nodes[1] !== "string")
    return { tag: "", head: [], body: [] };
  const parts: DefParts = { tag: nodes[1], head: nodes.slice(0, 2), body: [] };
  let at = 2;
  const marked = (key: string) => {
    const n = nodes[at];
    return Array.isArray(n) && n.length === 2 && n[0] === key && typeof n[1] === "string"
      ? n[1]
      : undefined;
  };
  const salt = marked("Salt");
  if (salt !== undefined) {
    parts.salt = salt;
    at++;
  }
  const jsx = marked("Jsx");
  if (jsx !== undefined) {
    parts.jsx = jsx;
    at++;
  }
  parts.head = nodes.slice(0, at);
  parts.body = nodes.slice(at);
  return parts;
}

uses

used by