( sites: Record<string, string>, parsed: Map<string, SourceModule>, units: UnitModule[], kept: KeptCode )
| 404 | } |
| 405 | |
| 406 | const creationsIn = ( |
| 407 | sites: Record<string, string>, |
| 408 | parsed: Map<string, SourceModule>, |
| 409 | units: UnitModule[], |
| 410 | kept: KeptCode |
| 411 | ): Map<string, Creation[]> => { |
| 412 | const result = new Map<string, Creation[]>() |
| 413 | const declaredIn = new Map(units.map(unit => [unit.specifier, unit])) |
| 414 | const originOf = ( |
| 415 | binding: Binding | undefined |
| 416 | ): [specifier: string, name: string] | undefined => { |
| 417 | if (binding?.kind !== "import") return |
| 418 | const specifier = binding.declaration |
| 419 | if ( |
| 420 | specifier.type !== "ImportSpecifier" || |
| 421 | specifier.parent?.type !== "ImportDeclaration" |
| 422 | ) |
| 423 | return |
| 424 | const source = specifier.parent.source.value |
| 425 | const target = parsed.get(source) |
| 426 | const entry = target?.scopes.exportsByName[exportNameOf(specifier.imported)] |
| 427 | if (entry?.kind !== "binding") return |
| 428 | return entry.binding.kind === "import" ? |
| 429 | originOf(entry.binding) |
| 430 | : [source, entry.binding.name] |
| 431 | } |
| 432 | const callsOf = new Map<string, number>() |
| 433 | for (const module of parsed.values()) { |
| 434 | for (const binding of Object.values(module.scopes.root.bindings)) { |
| 435 | const origin = originOf(binding) |
| 436 | if (!origin) continue |
| 437 | const key = origin.join("\0") |
| 438 | const calls = binding.references.filter( |
| 439 | reference => |
| 440 | reference.id.parent?.type !== "ExportSpecifier" && |
| 441 | kept.statements.has(statementOf(reference.id)) |
| 442 | ).length |
| 443 | callsOf.set(key, (callsOf.get(key) ?? 0) + calls) |
| 444 | } |
| 445 | } |
| 446 | const names = new Set<string>() |
| 447 | for (const id in sites) { |
| 448 | const module = parsed.get(id)! |
| 449 | const creations: Creation[] = [] |
| 450 | for (const { statement, declaration, call } of keptCallsIn(module, kept)) { |
| 451 | if ( |
| 452 | call.callee.type !== "Identifier" || |
| 453 | !call.arguments.length || |
| 454 | !call.arguments.every(arg => arg.type === "Identifier") |
| 455 | ) |
| 456 | continue |
| 457 | // a creation the precompiler marks pure runs the same anywhere |
| 458 | const annotation = pureAnnotationOf(module.source, call) |
| 459 | if (!annotation) continue |
| 460 | const bindingOf = (id: Node) => |
| 461 | module.scopes.root.bindings[(id as { name: string }).name] |
| 462 | const creator = originOf(bindingOf(call.callee)) |
| 463 | const args = call.arguments.map(arg => originOf(bindingOf(arg))) |
no test coverage detected