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

Function creationsIn

ark/aot/flat.ts:406–516  ·  view source on GitHub ↗
(
	sites: Record<string, string>,
	parsed: Map<string, SourceModule>,
	units: UnitModule[],
	kept: KeptCode
)

Source from the content-addressed store, hash-verified

404}
405
406const creationsIn = (
407 sites: Record<string, string>,
408 parsed: Map<string, SourceModule>,
409 units: UnitModule[],
410 kept: KeptCode
411): Map<string, Creation[]> => {
412 const result = new Map<string, Creation[]>()
413 const declaredIn = new Map(units.map(unit => [unit.specifier, unit]))
414 const originOf = (
415 binding: Binding | undefined
416 ): [specifier: string, name: string] | undefined => {
417 if (binding?.kind !== "import") return
418 const specifier = binding.declaration
419 if (
420 specifier.type !== "ImportSpecifier" ||
421 specifier.parent?.type !== "ImportDeclaration"
422 )
423 return
424 const source = specifier.parent.source.value
425 const target = parsed.get(source)
426 const entry = target?.scopes.exportsByName[exportNameOf(specifier.imported)]
427 if (entry?.kind !== "binding") return
428 return entry.binding.kind === "import" ?
429 originOf(entry.binding)
430 : [source, entry.binding.name]
431 }
432 const callsOf = new Map<string, number>()
433 for (const module of parsed.values()) {
434 for (const binding of Object.values(module.scopes.root.bindings)) {
435 const origin = originOf(binding)
436 if (!origin) continue
437 const key = origin.join("\0")
438 const calls = binding.references.filter(
439 reference =>
440 reference.id.parent?.type !== "ExportSpecifier" &&
441 kept.statements.has(statementOf(reference.id))
442 ).length
443 callsOf.set(key, (callsOf.get(key) ?? 0) + calls)
444 }
445 }
446 const names = new Set<string>()
447 for (const id in sites) {
448 const module = parsed.get(id)!
449 const creations: Creation[] = []
450 for (const { statement, declaration, call } of keptCallsIn(module, kept)) {
451 if (
452 call.callee.type !== "Identifier" ||
453 !call.arguments.length ||
454 !call.arguments.every(arg => arg.type === "Identifier")
455 )
456 continue
457 // a creation the precompiler marks pure runs the same anywhere
458 const annotation = pureAnnotationOf(module.source, call)
459 if (!annotation) continue
460 const bindingOf = (id: Node) =>
461 module.scopes.root.bindings[(id as { name: string }).name]
462 const creator = originOf(bindingOf(call.callee))
463 const args = call.arguments.map(arg => originOf(bindingOf(arg)))

Callers 1

flattenBundleFunction · 0.85

Calls 15

statementOfFunction · 0.90
pureAnnotationOfFunction · 0.90
unwrapExpressionFunction · 0.90
originOfFunction · 0.85
keptCallsInFunction · 0.85
bindingOfFunction · 0.85
creatorFnOfFunction · 0.85
memberReadsOfFunction · 0.85
filterMethod · 0.80
declaredNameOfFunction · 0.70
mapMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected