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

Function writePrecompiled

ark/docs/components/playground/precompile.ts:265–382  ·  view source on GitHub ↗
(
	source: string,
	js: string,
	optimize: Optimization
)

Source from the content-addressed store, hash-verified

263 * bundler scanned, refusing those it would write from what the program reads
264 */
265export const writePrecompiled = async (
266 source: string,
267 js: string,
268 optimize: Optimization
269): Promise<Precompiled> => {
270 const isolated = isolate(js)
271 const program = await parse(isolated)
272 const sourceProgram = await parse(source)
273 const emitted = functionsOf(program)
274 const written = functionsOf(sourceProgram)
275 // the emit strips only types, so keeps every function in order, unless
276 // e.g. an enum adds its own
277 const isMapped = emitted.length === written.length
278 const initsByName = declarationsOf(program)
279 const sourceInitsByName = declarationsOf(sourceProgram)
280 const values = evaluate(
281 nameFunctions(isolated, program, emitted),
282 Object.keys(initsByName),
283 { $arkHole: holeNamerOf(isMapped ? written : emitted) }
284 )
285 const holesByName = new Map(
286 (isMapped ? written : []).map(site => [holeNameAt(site), site])
287 )
288 const refusals: Record<string, string> = {}
289 const exportsByName: Record<string, StandaloneExport> = {}
290 let tables = 0
291 for (const [name, value] of Object.entries(values)) {
292 const init = initsByName[name]
293 const sourceInit = sourceInitsByName[name]
294 // a value read off a site, as `types.user` is, isn't one
295 if (
296 !exportedKinds.has(arkKindOf(value)) ||
297 init?.type !== "CallExpression" ||
298 !sourceInit
299 )
300 continue
301 const refusal = refusalOf(init, values)
302 if (refusal) {
303 refusals[name] = refusal
304 continue
305 }
306 // received in the order the definition reads them
307 const received: [start: number, param: FactoryParam][] = []
308 for (const hole of keyOf(value as Foldable)[1]) {
309 const site =
310 typeof hole === "function" ? holesByName.get(hole.name) : undefined
311 const declared =
312 site && declaredNameOf(site.node as never, site.parent as never)
313 const read =
314 !site ? undefined
315 : contains(sourceInit, site.node) ?
316 isPassedIn(sourceInit, site.node) ? site.node
317 : undefined
318 : declared && readsOf(sourceInit).find(read => read.name === declared)
319 if (!read) {
320 refusals[name] =
321 `${name} holds ${printable(hole)}, which the playground can't trace to where ${name} reads it`
322 break

Callers 2

PrecompiledResultFunction · 0.90
shownValidatorsOfFunction · 0.90

Calls 15

isolateFunction · 0.90
evaluateFunction · 0.90
arkKindOfFunction · 0.90
keyOfFunction · 0.90
declaredNameOfFunction · 0.90
printableFunction · 0.90
combinesChecksFunction · 0.90
functionsOfFunction · 0.85
nameFunctionsFunction · 0.85
holeNamerOfFunction · 0.85
holeNameAtFunction · 0.85
isPassedInFunction · 0.85

Tested by 1

shownValidatorsOfFunction · 0.72