( module: SourceModule, flattened: ReadonlySet<string>, exported: ReadonlySet<string>, specifier: string, read: Set<string>, isRead: (binding: Binding) => boolean, // whether it drops an import of the precompiler's that nothing reads, as // a rewritten site's are read only where the function creates in its place drops = false )
| 646 | } |
| 647 | |
| 648 | const repointedIn = ( |
| 649 | module: SourceModule, |
| 650 | flattened: ReadonlySet<string>, |
| 651 | exported: ReadonlySet<string>, |
| 652 | specifier: string, |
| 653 | read: Set<string>, |
| 654 | isRead: (binding: Binding) => boolean, |
| 655 | // whether it drops an import of the precompiler's that nothing reads, as |
| 656 | // a rewritten site's are read only where the function creates in its place |
| 657 | drops = false |
| 658 | ): Repointed[] | undefined => { |
| 659 | const result: Repointed[] = [] |
| 660 | for (const statement of module.program.body) { |
| 661 | if (statement.type !== "ImportDeclaration") continue |
| 662 | const source = statement.source.value |
| 663 | const flat: string[] = [] |
| 664 | const kept: string[] = [] |
| 665 | let dropped = false |
| 666 | for (const imported of statement.specifiers) { |
| 667 | if (imported.type !== "ImportSpecifier") { |
| 668 | // a unit read whole would be a copy of the function's |
| 669 | if (flattened.has(source)) return |
| 670 | continue |
| 671 | } |
| 672 | const name = exportNameOf(imported.imported) |
| 673 | const local = imported.local.name |
| 674 | const named = name === local ? name : `${name} as ${local}` |
| 675 | const isReadHere = isRead(module.scopes.root.bindings[local]) |
| 676 | // an import nothing kept reads links to the unit's own module |
| 677 | if (isReadHere && exported.has(`${source}\0${name}`)) { |
| 678 | read.add(name) |
| 679 | flat.push(named) |
| 680 | } else if (drops && !isReadHere && source.startsWith("~arktype/")) |
| 681 | dropped = true |
| 682 | else kept.push(named) |
| 683 | } |
| 684 | if (!flat.length && !dropped) continue |
| 685 | result.push({ |
| 686 | start: statement.start, |
| 687 | end: statement.end, |
| 688 | imports: [ |
| 689 | ...(flat.length ? [[specifier, flat] as [string, string[]]] : []), |
| 690 | ...(kept.length ? [[source, kept] as [string, string[]]] : []) |
| 691 | ] |
| 692 | }) |
| 693 | } |
| 694 | return result |
| 695 | } |
| 696 | |
| 697 | const writeImports = ( |
| 698 | imports: Repointed["imports"], |
no test coverage detected