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

Function precompileInPlace

ark/schema/units.ts:172–269  ·  view source on GitHub ↗
(
	root: BaseRoot,
	unbound: UnboundTraversals | undefined
)

Source from the content-addressed store, hash-verified

170}
171
172export const precompileInPlace = (
173 root: BaseRoot,
174 unbound: UnboundTraversals | undefined
175): void => {
176 const declared: [node: BaseNode, kinds: TraversalKind[]][] = []
177 const kindsByNode = new Map<BaseNode, TraversalKind[]>()
178 // a node a morph pipes to, whose traversals pipe reads from it
179 const shapes = new Map<BaseNode, object>()
180 const include = (node: BaseNode): void => {
181 if (link.byId[node.id]) return
182 for (const reference of referencesThroughAliases([
183 node,
184 ...node.references
185 ])) {
186 if (link.byId[reference.id]) continue
187 link.byId[reference.id] = reference
188 const kinds: TraversalKind[] = []
189 const isCompiled = !!reference.unitFn || !!unbound?.get(reference)
190 if (!isCompiled) kinds.push("Allows")
191 if (reference.tfs ? !reference.c.applyInPlace : !isCompiled)
192 kinds.push("Apply")
193 if (
194 reference.tfs &&
195 includes(transformedKinds, reference.kind) &&
196 !reference.c.applyInPlace
197 )
198 kinds.push("Transform")
199 if (!kinds.length) continue
200 declared.push([reference, kinds])
201 kindsByNode.set(reference, kinds)
202 }
203 }
204 class InPlaceCompiler extends TraversalCompiler {
205 override readonly inPlace = true
206
207 override reach(node: BaseNode): void {
208 include(node)
209 }
210
211 override refToId(id: NodeId, opts?: ReferenceOptions): string {
212 const reference = this.link.byId[id]
213 const kind = opts?.kind ?? this.trav
214 if (reference && !kindsByNode.get(reference)?.includes(kind))
215 this.link.dependencies[`${this.nameOf(id)}${kind}`] = [reference, kind]
216
217 return super.refToId(id, opts)
218 }
219
220 override ref(value: object | symbol): string {
221 if (!isNode(value) || !value.isRoot() || !value.tfs)
222 return super.ref(value)
223 include(value)
224 let shape = shapes.get(value)
225 if (!shape) shapes.set(value, (shape = Object.create(value) as object))
226 return super.ref(shape)
227 }
228 }
229 const link = createLinkage(undefined, { compiler: InPlaceCompiler })

Callers 1

inPlace.tsFile · 0.90

Calls 8

createLinkageFunction · 0.85
declareTraversalFunction · 0.85
writeUnitFunction · 0.85
includeFunction · 0.70
mapMethod · 0.65
getMethod · 0.65
pushMethod · 0.45
compileMethod · 0.45

Tested by

no test coverage detected