|
| 1 | +import type { BoundType } from "../bindings/index.js"; |
| 2 | +import { Scope } from "./scope.js"; |
| 3 | +import { structKey, unionKey } from "./type-keys.js"; |
| 4 | + |
| 5 | +/** A named compound type (struct or union) discovered during type collection. */ |
| 6 | +export interface NamedType { |
| 7 | + name: string; |
| 8 | + type: BoundType; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Recursively collect all struct/union types and assign unique names. |
| 13 | + * |
| 14 | + * Walks the BoundType tree depth-first, using structural keys (`structKey`, |
| 15 | + * `unionKey`) to deduplicate types that appear multiple times in the tree. |
| 16 | + * Each unique struct/union gets a name generated by applying `nameTransform` |
| 17 | + * to a hint string (derived from the root name or field/variant names). |
| 18 | + * |
| 19 | + * @param rootType - The root BoundType to walk. |
| 20 | + * @param rootName - The hint for the root type's name. |
| 21 | + * @param scope - Symbol scope for collision avoidance. |
| 22 | + * @param nameTransform - Converts a hint string to a type name (e.g. pascalCase, snake_case). |
| 23 | + * @returns `namedTypes` maps structural keys to assigned names, `typeDecls` lists declarations in order. |
| 24 | + */ |
| 25 | +export function collectNamedTypes( |
| 26 | + rootType: BoundType, |
| 27 | + rootName: string, |
| 28 | + scope: Scope, |
| 29 | + nameTransform: (hint: string) => string, |
| 30 | +): { namedTypes: Map<string, string>; typeDecls: NamedType[] } { |
| 31 | + const namedTypes = new Map<string, string>(); |
| 32 | + const typeDecls: NamedType[] = []; |
| 33 | + |
| 34 | + function visit(type: BoundType, hint: string): void { |
| 35 | + switch (type.kind) { |
| 36 | + case "struct": { |
| 37 | + const key = structKey(type); |
| 38 | + if (!namedTypes.has(key)) { |
| 39 | + const name = scope.add(nameTransform(hint)); |
| 40 | + namedTypes.set(key, name); |
| 41 | + typeDecls.push({ name, type }); |
| 42 | + for (const [fieldName, fieldType] of Object.entries(type.fields)) { |
| 43 | + visit(fieldType, fieldName); |
| 44 | + } |
| 45 | + } |
| 46 | + break; |
| 47 | + } |
| 48 | + case "union": { |
| 49 | + const key = unionKey(type); |
| 50 | + if (!namedTypes.has(key)) { |
| 51 | + const name = scope.add(nameTransform(hint)); |
| 52 | + namedTypes.set(key, name); |
| 53 | + typeDecls.push({ name, type }); |
| 54 | + for (const v of type.variants) { |
| 55 | + visit(v.type, v.name ?? hint); |
| 56 | + } |
| 57 | + } |
| 58 | + break; |
| 59 | + } |
| 60 | + case "optional": |
| 61 | + visit(type.inner, hint); |
| 62 | + break; |
| 63 | + case "list": |
| 64 | + visit(type.item, hint); |
| 65 | + break; |
| 66 | + default: |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + visit(rootType, rootName); |
| 72 | + return { namedTypes, typeDecls }; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Create a type name resolver from a namedTypes map. |
| 77 | + * Returns a function that maps a BoundType to its assigned name (if any). |
| 78 | + */ |
| 79 | +export function resolveTypeName( |
| 80 | + namedTypes: Map<string, string>, |
| 81 | +): (type: BoundType) => string | undefined { |
| 82 | + return (type) => { |
| 83 | + if (type.kind === "struct") return namedTypes.get(structKey(type)); |
| 84 | + if (type.kind === "union") return namedTypes.get(unionKey(type)); |
| 85 | + return undefined; |
| 86 | + }; |
| 87 | +} |
0 commit comments