| 60 | * Undefined where the module reads none. |
| 61 | */ |
| 62 | export const writeIssueFeaturesInto = ( |
| 63 | id: string, |
| 64 | code: string, |
| 65 | issues: IssueFeatures |
| 66 | ): string | undefined => { |
| 67 | if (!namesIssueFeature(code)) return |
| 68 | const module = new SourceModule(id, code, id) |
| 69 | if (module.parseErrors.length) return |
| 70 | const { scopes } = module |
| 71 | const edits: Edit[] = [] |
| 72 | const valueByReference = new Map<Node, string>() |
| 73 | // each import's specifiers that are written in, by its declaration |
| 74 | const writtenByImport = new Map<Node, Set<Node>>() |
| 75 | for (const binding of Object.values(scopes.root.bindings)) { |
| 76 | const specifier = binding.declaration |
| 77 | if (binding.kind !== "import" || specifier.type !== "ImportSpecifier") |
| 78 | continue |
| 79 | const declaration = specifier.parent as Node | undefined |
| 80 | if (declaration?.type !== "ImportDeclaration") continue |
| 81 | const imported = |
| 82 | specifier.imported.type === "Identifier" ? |
| 83 | specifier.imported.name |
| 84 | : specifier.imported.value |
| 85 | if ( |
| 86 | !hasOwn(issues, imported) || |
| 87 | !exportsFeatures(declaration.source.value) || |
| 88 | binding.writes.length |
| 89 | ) |
| 90 | continue |
| 91 | // in parentheses, which esbuild reads as meant: it warns that `&&` after |
| 92 | // a bare `false` always returns it, where the module isn't a dependency's |
| 93 | const value = `(${issues[imported as keyof IssueFeatures]})` |
| 94 | let isExported = false |
| 95 | for (const { id } of binding.references) { |
| 96 | // `export { appliesValues }`, as arktype's runtime exports |
| 97 | // @ark/schema's: still the binding |
| 98 | if ((id as Node).parent?.type === "ExportSpecifier") isExported = true |
| 99 | else valueByReference.set(id, value) |
| 100 | } |
| 101 | if (isExported) continue |
| 102 | let written = writtenByImport.get(declaration) |
| 103 | if (!written) writtenByImport.set(declaration, (written = new Set())) |
| 104 | written.add(specifier) |
| 105 | } |
| 106 | if (!valueByReference.size && !writtenByImport.size) return |
| 107 | // `{ writesText }` holds the key too |
| 108 | const shorthand = new Set<Node>() |
| 109 | walk(module.program, node => { |
| 110 | if ( |
| 111 | node.type === "Property" && |
| 112 | node.shorthand && |
| 113 | valueByReference.has(node.value) |
| 114 | ) { |
| 115 | shorthand.add(node.value) |
| 116 | edits.push([ |
| 117 | node.start, |
| 118 | node.end, |
| 119 | `${code.slice(node.value.start, node.value.end)}: ${valueByReference.get(node.value)}` |