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

Function withTemplatesInlined

ark/aot/flat.ts:2381–2418  ·  view source on GitHub ↗
(
	code: string
)

Source from the content-addressed store, hash-verified

2379
2380// a template called once becomes its call's function, flattened as a factory
2381const withTemplatesInlined = (
2382 code: string
2383): { code: string; names: string[] } | undefined => {
2384 const { program, errors } = parseUnits(code)
2385 if (errors.length) return
2386 const scopes = new ModuleScopes(program)
2387 const s = new MagicString(code)
2388 const names: string[] = []
2389 for (const statement of program.body as Node[]) {
2390 if (
2391 statement.type !== "VariableDeclaration" ||
2392 statement.declarations.length !== 1
2393 )
2394 continue
2395 const { id, init: fn } = statement.declarations[0]
2396 if (fn?.type !== "ArrowFunctionExpression" || !returnedLengthOf(fn))
2397 continue
2398 const references = scopes.bindingsById.get(id)?.references ?? []
2399 const callee = references[0]?.id
2400 const call = callee?.parent
2401 const declarator = call?.parent
2402 if (
2403 references.length === 1 &&
2404 call?.type === "CallExpression" &&
2405 call.callee === callee &&
2406 declarator?.type === "VariableDeclarator" &&
2407 declarator.init === call &&
2408 (declarator.id.type === "Identifier" ||
2409 declarator.id.type === "ArrayPattern") &&
2410 statementOf(declarator).parent?.type === "Program"
2411 ) {
2412 s.overwrite(callee.start, callee.end, `(${code.slice(fn.start, fn.end)})`)
2413 s.remove(statement.start, statement.end)
2414 names.push((id as { name: string }).name)
2415 }
2416 }
2417 return { code: s.toString(), names }
2418}
2419
2420const namesReturnedLocals = (
2421 names: string[],

Callers 1

writeFlatFunction · 0.85

Calls 7

statementOfFunction · 0.90
parseUnitsFunction · 0.85
returnedLengthOfFunction · 0.85
removeMethod · 0.80
getMethod · 0.65
toStringMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected