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

Function isFactory

ark/aot/flat.ts:2455–2494  ·  view source on GitHub ↗
(
	call: Call,
	fn: Arrow,
	scopes: ModuleScopes,
	returnsValue: boolean
)

Source from the content-addressed store, hash-verified

2453}
2454
2455const isFactory = (
2456 call: Call,
2457 fn: Arrow,
2458 scopes: ModuleScopes,
2459 returnsValue: boolean
2460): boolean => {
2461 if (fn.async || (!returnsValue && !returnedLengthOf(fn))) return false
2462 let params = fn.params as Node[]
2463 const rest = params[params.length - 1] as Node | undefined
2464 if (rest?.type === "RestElement") {
2465 if (scopes.bindingsById.get(rest.argument)?.references.length) return false
2466 params = params.slice(0, -1)
2467 }
2468 if (call.arguments.length !== params.length) return false
2469 const isFlat = params.every((param, i) => {
2470 const arg = call.arguments[i]
2471 if (arg.type === "SpreadElement") return false
2472 if (param.type === "Identifier") return true
2473 return (
2474 param.type === "ObjectPattern" &&
2475 arg.type === "Identifier" &&
2476 param.properties.every(
2477 p =>
2478 p.type === "Property" &&
2479 !p.computed &&
2480 p.key.type === "Literal" &&
2481 typeof p.key.value === "number" &&
2482 p.value.type === "Identifier"
2483 )
2484 )
2485 })
2486 if (!isFlat) return false
2487 // its body runs as the function's, so it can return only at its end
2488 let returns = 0
2489 walk(fn.body, node => {
2490 if (node.type === "ReturnStatement") returns++
2491 return node === fn.body || !isFunction(node)
2492 })
2493 return returns === 1
2494}
2495
2496const isFunction = (node: Node) =>
2497 node.type === "ArrowFunctionExpression" ||

Callers 1

declarationOfFunction · 0.70

Calls 4

walkFunction · 0.90
returnedLengthOfFunction · 0.85
isFunctionFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected