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

Function dropUnread

ark/aot/flat.ts:2219–2258  ·  view source on GitHub ↗
({ code, program, scopes }, s, body)

Source from the content-addressed store, hash-verified

2217
2218// a bundler drops only a module's unread declarations, not a function's
2219const dropUnread: UnitsPass = ({ code, program, scopes }, s, body) => {
2220 const dropped: Node[] = []
2221 // dropped once nothing reads them
2222 const droppable: Binding[] = []
2223 for (const statement of program.body as Node[]) {
2224 if (statement.type !== "VariableDeclaration") continue
2225 const { declarations } = statement
2226 const unread = declarations.map(declarator => {
2227 const binding = scopes.bindingsById.get(declarator.id)
2228 if (
2229 !binding ||
2230 (declarator.init && !isPureExpression(declarator.init, code))
2231 )
2232 return false
2233 if (binding.references.length) {
2234 droppable.push(binding)
2235 return false
2236 }
2237 // a name declared again is bound by its next declaration
2238 if (binding.writes.length) droppable.push(binding)
2239 dropped.push(declarator)
2240 return true
2241 })
2242 if (unread.every(Boolean))
2243 s.remove(statement.start, statementEndIn(body, statement))
2244 else if (unread.some(Boolean)) {
2245 const kept = declarations.filter((_, i) => !unread[i])
2246 s.overwrite(
2247 statement.start,
2248 statementEndIn(body, statement),
2249 `${statement.kind} ${kept.map(d => code.slice(d.start, d.end)).join(", ")}`
2250 )
2251 }
2252 }
2253 // settled unless a declaration was read only by those dropped
2254 const isDropped = spansContaining(dropped)
2255 return !droppable.some(binding =>
2256 binding.references.every(({ id }) => isDropped(id))
2257 )
2258}
2259
2260const pureAnnotation = /^\s*[#@]__PURE__\s*$/
2261

Callers

nothing calls this directly

Calls 8

isPureExpressionFunction · 0.85
statementEndInFunction · 0.85
spansContainingFunction · 0.85
removeMethod · 0.80
filterMethod · 0.80
mapMethod · 0.65
getMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected