( codes: Record<string, string>, siteTexts: string[] )
| 145 | // one module of what the sites read, each after what it reads; a unit must |
| 146 | // declare or import what it reads by its export name |
| 147 | const oneModuleOf = ( |
| 148 | codes: Record<string, string>, |
| 149 | siteTexts: string[] |
| 150 | ): OneModule | undefined => { |
| 151 | const declarations = declarationsOf(codes) |
| 152 | if (!declarations) return |
| 153 | const { declared, imported, statements } = declarations |
| 154 | const order: string[] = [] |
| 155 | const states = new Map<string, "visiting" | "done">() |
| 156 | // each declaration after what it reads eagerly |
| 157 | const visit = (name: string): "done" | "cycle" | undefined => { |
| 158 | const declaration = declared.get(name) |
| 159 | if (!declaration) return imported.has(name) ? "done" : undefined |
| 160 | const state = states.get(name) |
| 161 | if (state) return state === "done" ? "done" : "cycle" |
| 162 | states.set(name, "visiting") |
| 163 | for (const [read, isEager] of declaration.reads) { |
| 164 | const visited = visit(read) |
| 165 | if (!visited || (visited === "cycle" && isEager)) return |
| 166 | } |
| 167 | states.set(name, "done") |
| 168 | order.push(name) |
| 169 | return "done" |
| 170 | } |
| 171 | const renamed: Record<string, string> = {} |
| 172 | const exported: string[] = [] |
| 173 | for (const text of siteTexts) { |
| 174 | for (const match of text.matchAll(siteImport)) { |
| 175 | const [, names, specifier] = match |
| 176 | if (!(specifier in codes)) continue |
| 177 | renamed[specifier] = "" |
| 178 | for (const name of names.split(",")) { |
| 179 | const [read] = name.trim().split(/\s+as\s+/) |
| 180 | if (!declared.has(read) || visit(read) !== "done") return |
| 181 | if (!exported.includes(read)) exported.push(read) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | // a site reading a unit other than by a named import would read nothing |
| 186 | for (const text of siteTexts) { |
| 187 | for (const [, specifier] of text.matchAll(virtualSource)) |
| 188 | if (specifier in codes && !(specifier in renamed)) return |
| 189 | } |
| 190 | const code = [ |
| 191 | ...imported.values(), |
| 192 | ...statements, |
| 193 | ...order.map(name => declared.get(name)!.text), |
| 194 | `export { ${exported.join(", ")} }` |
| 195 | ].join("\n") |
| 196 | const specifier = `~arktype/${hashOf(code)}` |
| 197 | for (const imported in renamed) renamed[imported] = specifier |
| 198 | return { specifier, code, renamed } |
| 199 | } |
| 200 | |
| 201 | interface SharedModules { |
| 202 | // each module declaring units, by its specifier |
no test coverage detected