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

Function functionsDeclared

ark/aot/flat.ts:1994–2018  ·  view source on GitHub ↗
(body: string)

Source from the content-addressed store, hash-verified

1992// V8 parses a function declared for a sixth less than a constant holding it, and
1993// a call reads it without checking it's been initialized
1994const functionsDeclared = (body: string): string => {
1995 const { program } = parseUnits(body)
1996 const s = new MagicString(body)
1997 for (const statement of program.body as Node[]) {
1998 if (statement.type !== "VariableDeclaration" || statement.kind !== "const")
1999 continue
2000 const { declarations } = statement
2001 if (!declarations.some(declaresFunction)) continue
2002 s.overwrite(
2003 statement.start,
2004 statement.end,
2005 declarations
2006 .map(declarator =>
2007 declaresFunction(declarator) ?
2008 `function ${(declarator.id as { name: string }).name}${body.slice(
2009 declarator.init!.start + "function".length,
2010 declarator.init!.end
2011 )}`
2012 : `const ${body.slice(declarator.start, declarator.end)}`
2013 )
2014 .join("\n")
2015 )
2016 }
2017 return s.toString()
2018}
2019
2020const declaresFunction = ({ id, init }: VariableDeclarator): boolean =>
2021 id.type === "Identifier" && !!init && isPlainFunction(init)

Callers 1

writtenUnitsOfFunction · 0.85

Calls 4

parseUnitsFunction · 0.85
declaresFunctionFunction · 0.85
mapMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected