| 259 | // each import of a name the module reads and no longer declares, as |
| 260 | // TypeScript's transform drops an import only a definition arktype's wrote read |
| 261 | const missingImportsOf = ( |
| 262 | unwritten: Program, |
| 263 | reached: Program, |
| 264 | source: string |
| 265 | ): string => { |
| 266 | const unresolved = new Set( |
| 267 | new ModuleScopes(unwritten).root.unresolved.flatMap(({ id, binding }) => |
| 268 | binding ? [] : id.name |
| 269 | ) |
| 270 | ) |
| 271 | let imports = "" |
| 272 | for (const statement of reached.body) { |
| 273 | if (statement.type !== "ImportDeclaration") continue |
| 274 | if (statement.importKind === "type") continue |
| 275 | const from = ` from ${source.slice(statement.source.start, statement.source.end)}` |
| 276 | for (const specifier of statement.specifiers) { |
| 277 | if (!unresolved.has(specifier.local.name)) continue |
| 278 | if (specifier.type === "ImportDefaultSpecifier") |
| 279 | imports += `import ${specifier.local.name}${from}\n` |
| 280 | else if (specifier.type === "ImportNamespaceSpecifier") |
| 281 | imports += `import * as ${specifier.local.name}${from}\n` |
| 282 | else if (specifier.importKind !== "type") |
| 283 | imports += `import { ${source.slice(specifier.imported.start, specifier.imported.end)} as ${specifier.local.name} }${from}\n` |
| 284 | } |
| 285 | } |
| 286 | return imports |
| 287 | } |
| 288 | |
| 289 | // A module's tokens, each a string, regular expression, template part or JSX |
| 290 | // text its tree names, or a word, number or other character of the text |