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

Function writeComponent

ark/schema/units.ts:835–890  ·  view source on GitHub ↗
(
	component: readonly BaseNode[],
	s: ContentNamingState,
	opts: UnitOptions
)

Source from the content-addressed store, hash-verified

833}
834
835const writeComponent = (
836 component: readonly BaseNode[],
837 s: ContentNamingState,
838 opts: UnitOptions
839): WrittenUnit | undefined => {
840 const namesRead = new Map<NodeId, string | undefined>()
841 const readName = (id: NodeId) => {
842 const name = s.names[id]
843 namesRead.set(id, name)
844 return name
845 }
846 const reachedRead = new Map<NodeId, BaseNode | undefined>()
847 const reachedAt = (id: NodeId) => {
848 const node = s.byId[id]?.node
849 reachedRead.set(id, node)
850 return node
851 }
852 const roots: BaseNode[] = component.filter(node => node.isRoot())
853 roots.sort((l, r) => (readName(l.id) < readName(r.id) ? -1 : 1))
854 if (!roots.length) return
855 const declaredByName: Record<string, BaseNode> = {}
856 const reached: BaseNode[] = [...roots]
857 for (const node of reached) {
858 if (declaredByName[readName(node.id)]) continue
859 declaredByName[readName(node.id)] = node
860 for (const callee of s.byId[node.id].callees)
861 if (!callee.isRoot() || roots.includes(callee)) reached.push(callee)
862 }
863 const name = readName(roots[0].id)
864 const link = createLinkage(readName, opts, name)
865 // reads through to what the pass reached
866 link.byId = Object.create(
867 new Proxy({}, { get: (_, id) => reachedAt(id as NodeId) })
868 )
869 // a reached root another component declares
870 link.reused = {
871 has: node =>
872 reachedAt(node.id) === node &&
873 node.isRoot() &&
874 !declaredByName[readName(node.id)]
875 }
876 const declared = Object.values(declaredByName)
877 const declaredTraversals = declared.map(node => declareTraversals(link, node))
878 return {
879 component,
880 namesRead,
881 reachedRead,
882 name,
883 roots: roots as BaseRoot[],
884 declared,
885 declaredTraversals,
886 members: link.members,
887 refs: [...link.refs.keys()],
888 dependencies: link.dependencies
889 }
890}
891
892interface ContentNamingState {

Callers 1

unitsOfFunction · 0.85

Calls 8

readNameFunction · 0.85
createLinkageFunction · 0.85
reachedAtFunction · 0.85
declareTraversalsFunction · 0.85
filterMethod · 0.80
sortMethod · 0.80
mapMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected