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

Function namedFunctionOf

ark/aot/graph.ts:7358–7393  ·  view source on GitHub ↗
(node: Node, module: SourceModule)

Source from the content-addressed store, hash-verified

7356
7357// esbuild's keepNames: `__name(fn, "name")` returns `fn`
7358const namedFunctionOf = (node: Node, module: SourceModule): Node => {
7359 if (
7360 node.type !== "CallExpression" ||
7361 node.arguments.length !== 2 ||
7362 node.arguments[1].type !== "Literal" ||
7363 typeof node.arguments[1].value !== "string"
7364 )
7365 return node
7366 const namer = initOf(node.callee, module)
7367 if (
7368 namer?.type !== "ArrowFunctionExpression" ||
7369 namer.body.type !== "CallExpression" ||
7370 namer.body.callee.type !== "Identifier"
7371 )
7372 return node
7373 const define = namer.body.callee
7374 const [target, value] = namer.params.map(param =>
7375 param.type === "Identifier" ? param.name : ""
7376 )
7377 // a minifier renames the parameters and writes `true` as `!0`
7378 const call = compactTextOf(namer.body, module)
7379 .replace("{value,", "{value:value,")
7380 .replace("!0", "true")
7381 const defineProperty = initOf(define, module)
7382 return (
7383 namer.params.length === 2 &&
7384 target &&
7385 call ===
7386 `${define.name}(${target},"name",{value:${value},configurable:true})` &&
7387 defineProperty?.type === "MemberExpression" &&
7388 compactTextOf(defineProperty, module) === "Object.defineProperty" &&
7389 !module.scopes.byId.get(defineProperty.object)?.binding
7390 ) ?
7391 unwrapChain(node.arguments[0])
7392 : node
7393}
7394
7395const isNamed = (node: Node, module: SourceModule): boolean =>
7396 node.parent?.parent?.type === "ExpressionStatement" &&

Callers 8

definitionOfMethod · 0.85
methodOfMethod · 0.85
boundFunctionOfMethod · 0.85
definesArkMethod · 0.85
functionOfBindingFunction · 0.85
isNamedFunction · 0.85
bindingOfFunctionFunction · 0.85

Calls 6

unwrapChainFunction · 0.90
compactTextOfFunction · 0.85
replaceMethod · 0.80
initOfFunction · 0.70
mapMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected