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

Function isDeadTopLevelFunction

ark/aot/usage.ts:2050–2112  ·  view source on GitHub ↗
(
	fn: Node,
	module: SourceModule,
	ctx: ThisReadsContext
)

Source from the content-addressed store, hash-verified

2048}
2049
2050const isDeadTopLevelFunction = (
2051 fn: Node,
2052 module: SourceModule,
2053 ctx: ThisReadsContext
2054): boolean => {
2055 if (
2056 ctx.hasDynamicImport ||
2057 ctx.entries.has(module.id) ||
2058 ctx.exposed.has(module.id) ||
2059 module.scopes.isCommonJs
2060 )
2061 return false
2062 const [op, parent] = operatorOf(fn)
2063 if (parent?.type === "ExportDefaultDeclaration" && parent.declaration === op)
2064 return !isExportUsed(module, "default", ctx)
2065 const binding =
2066 (
2067 fn.type === "FunctionDeclaration" &&
2068 fn.id &&
2069 (parent?.type === "Program" ||
2070 ((parent?.type === "ExportNamedDeclaration" ||
2071 parent?.type === "ExportDefaultDeclaration") &&
2072 parent.parent?.type === "Program"))
2073 ) ?
2074 module.scopes.bindingsById.get(fn.id)
2075 : (
2076 parent?.type === "VariableDeclarator" &&
2077 parent.init === op &&
2078 parent.id.type === "Identifier" &&
2079 parent.parent?.type === "VariableDeclaration" &&
2080 (parent.parent.parent?.type === "Program" ||
2081 (parent.parent.parent?.type === "ExportNamedDeclaration" &&
2082 parent.parent.parent.parent?.type === "Program"))
2083 ) ?
2084 module.scopes.bindingsById.get(parent.id)
2085 : undefined
2086 if (!binding || binding.writes.length > 0) return false
2087 for (const ref of binding.references) {
2088 const [refOp, refUse] = operatorOf(ref.id)
2089 if (
2090 refUse?.type === "ExportSpecifier" ||
2091 (refUse?.type === "ExportDefaultDeclaration" &&
2092 refUse.declaration === refOp)
2093 )
2094 continue
2095 // prototype definitions on an otherwise unreferenced constructor don't make it live
2096 if (
2097 refUse?.type === "MemberExpression" &&
2098 refUse.object === refOp &&
2099 memberNameOf(refUse) === "prototype"
2100 ) {
2101 const [protoOp, protoUse] = operatorOf(refUse)
2102 if (
2103 protoUse?.type === "MemberExpression" &&
2104 protoUse.object === protoOp &&
2105 isDiscardedProtoMethodWrite(protoUse)
2106 )
2107 continue

Callers 1

computeSkippedFunctionFunction · 0.85

Calls 6

operatorOfFunction · 0.90
memberNameOfFunction · 0.90
isExportUsedFunction · 0.85
getMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected