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

Function sourceOf

ark/aot/graph.ts:610–644  ·  view source on GitHub ↗
(
	binding: Binding
)

Source from the content-addressed store, hash-verified

608// the module an import or a require reads a binding from, and the name it
609// reads, "*" for the whole module
610const sourceOf = (
611 binding: Binding
612): [specifier: string, name: string] | undefined => {
613 const declaration = binding.declaration
614 switch (declaration.type) {
615 case "ImportSpecifier":
616 case "ImportDefaultSpecifier":
617 case "ImportNamespaceSpecifier":
618 return [
619 (declaration.parent as ImportDeclaration).source.value,
620 declaration.type === "ImportSpecifier" ?
621 exportNameOf(declaration.imported)
622 : declaration.type === "ImportDefaultSpecifier" ? "default"
623 : "*"
624 ]
625 case "VariableDeclarator": {
626 const init = declaration.init && unwrapExpression(declaration.init)
627 const loaded =
628 init?.type === "CallExpression" ? loadedSourceOf(init) : undefined
629 if (loaded?.type !== "Literal" || typeof loaded.value !== "string") return
630 if (declaration.id.type === "Identifier") return [loaded.value, "*"]
631 // e.g. `const { register } = require("registry")`
632 const property =
633 declaration.id.type === "ObjectPattern" ?
634 declaration.id.properties.find(
635 property =>
636 property.type === "Property" && property.value === binding.id
637 )
638 : undefined
639 const name =
640 property?.type === "Property" ? keyNameOf(property) : undefined
641 return typeof name === "string" ? [loaded.value, name] : undefined
642 }
643 }
644}
645
646// e.g. `exports` or `module.exports`
647const isExportsOf = (node: Node): boolean => {

Callers 1

definitionOfMethod · 0.70

Calls 5

exportNameOfFunction · 0.90
unwrapExpressionFunction · 0.90
loadedSourceOfFunction · 0.90
findMethod · 0.80
keyNameOfFunction · 0.70

Tested by

no test coverage detected