MCPcopy Create free account
hub / github.com/arktypeio/arktype / TraversalCompiler

Class TraversalCompiler

ark/schema/units.ts:623–713  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

621})
622
623export class TraversalCompiler extends NodeCompiler {
624 /** how the units it writes reference one another */
625 readonly link: UnitLinkage
626
627 constructor(kind: TraversalKind, link: UnitLinkage, needsCtx: boolean) {
628 super({
629 kind,
630 needsCtx,
631 refs: link.refs
632 })
633 this.link = link
634 }
635
636 // declares a node's traversals some other way, returning their names
637 declareTraversals?(node: BaseNode): string[] | undefined
638
639 // a one-expression node is written inline
640 override inv(node: BaseNode, opts?: InvokeOptions): string {
641 this.reach(node)
642 const expression =
643 (
644 (opts?.kind ?? this.trav) !== this.trav ||
645 (opts?.arg ?? this.data) !== this.data
646 ) ?
647 undefined
648 : node.is("structure") ?
649 // a structure sharing transformed keys applies them in a call of its own
650 this.trav === "Apply" && node.sharesTfs ?
651 undefined
652 : node.jsConjunction(this)
653 : node.jsExpression?.(this)
654 return expression === undefined ? super.inv(node, opts) : `(${expression})`
655 }
656
657 override traversal(node: BaseNode, kind: TraversalKind): string {
658 this.reach(node)
659 return super.traversal(node, kind)
660 }
661
662 // a node passed as a value has its traversals read from it, as the node a
663 // morph pipes to does. One a root's unit would leave uncompiled since
664 // nothing invokes it is compiled with the unit, or each read walks its nodes
665 override ref(value: object | symbol): string {
666 if (isNode(value) && !value.unitFn && this.link.unreached.delete(value))
667 this.link.reached.push(value)
668 return super.ref(value)
669 }
670
671 reach(node: BaseNode): void {
672 if (!this.link.byId[node.id]) {
673 this.link.byId[node.id] = node
674 this.link.reached.push(node)
675 }
676 }
677
678 // a node can be named without being invoked, e.g. an Allows passed
679 override refToId(id: NodeId, opts?: ReferenceOptions): string {
680 const reference = this.link.byId[id]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected