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

Function declarationOf

ark/aot/flat.ts:2320–2378  ·  view source on GitHub ↗
(
	statement: Node,
	scopes: ModuleScopes,
	templates: Map<string, number>,
	created: ReadonlySet<string>
)

Source from the content-addressed store, hash-verified

2318const unitName = /\b\w+?(?:R\d+|Allows|Apply|Transform)\b/g
2319
2320const declarationOf = (
2321 statement: Node,
2322 scopes: ModuleScopes,
2323 templates: Map<string, number>,
2324 created: ReadonlySet<string>
2325): Declaration => {
2326 const { id, init } = (
2327 statement as Extract<Node, { type: "VariableDeclaration" }>
2328 ).declarations[0]
2329 const name = (id as { name: string }).name
2330 const node = statement
2331 switch (init!.type) {
2332 case "ArrayExpression":
2333 if (
2334 init!.elements.length &&
2335 init!.elements.every(
2336 e =>
2337 e?.type === "ArrowFunctionExpression" ||
2338 e?.type === "FunctionExpression"
2339 )
2340 )
2341 return { kind: "array", name, node, array: init! }
2342 break
2343 case "ArrowFunctionExpression":
2344 case "FunctionExpression": {
2345 const length = returnedLengthOf(init!)
2346 if (length) templates.set(name, length)
2347 break
2348 }
2349 case "CallExpression": {
2350 const callee = init!.callee
2351 if (callee.type === "Identifier") {
2352 const length = templates.get(callee.name)
2353 if (length) return { kind: "templateCall", name, node, id, length }
2354 } else if (
2355 callee.type === "ArrowFunctionExpression" &&
2356 isFactory(init!, callee, scopes, created.has(name))
2357 ) {
2358 // e.g. a template called once, inlined as its call's function
2359 const names =
2360 id.type === "ArrayPattern" ?
2361 id.elements.map(e => (e?.type === "Identifier" ? e.name : ""))
2362 : undefined
2363 if (!names || namesReturnedLocals(names, callee, scopes)) {
2364 return {
2365 kind: "factory",
2366 name: names ? names.join(", ") : name,
2367 node,
2368 call: init!,
2369 fn: callee,
2370 returnsValue: created.has(name),
2371 names
2372 }
2373 }
2374 }
2375 }
2376 }
2377 return { kind: "value", name, node }

Callers 1

writeFlatFunction · 0.85

Calls 7

returnedLengthOfFunction · 0.85
namesReturnedLocalsFunction · 0.85
isFactoryFunction · 0.70
getMethod · 0.65
mapMethod · 0.65
setMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected