({ domain, className, types, commands, events, vendorModules = [], specVersion })
| 940 | } |
| 941 | |
| 942 | function generateDomainFile({ domain, className, types, commands, events, vendorModules = [], specVersion }) { |
| 943 | const parts = [LICENSE_HEADER, '', GENERATED_NOTE] |
| 944 | |
| 945 | parts.push(`// Built from the WebDriver BiDi CDDL spec (v${specVersion}).`) |
| 946 | parts.push(`// Source: https://github.com/w3c/webref/tree/main/ed/cddl`) |
| 947 | parts.push('') |
| 948 | |
| 949 | const hasImplementation = className != null && (commands.length > 0 || events.length > 0) |
| 950 | |
| 951 | // Vendor types follow the spec's, and are tagged with the vendor class that owns their enum constants. |
| 952 | const vendorTypeOwner = new Map(vendorModules.flatMap((m) => Object.keys(m.types).map((name) => [name, m]))) |
| 953 | const allTypes = Object.assign({}, types, ...vendorModules.map((m) => m.types)) |
| 954 | const typeEntries = Object.entries(allTypes) |
| 955 | |
| 956 | const crossDomainImports = computeCrossDomainImports(allTypes, domain) |
| 957 | if (crossDomainImports.length > 0) { |
| 958 | for (const line of crossDomainImports) parts.push(line) |
| 959 | parts.push('') |
| 960 | } |
| 961 | |
| 962 | if (typeEntries.length > 0) { |
| 963 | parts.push(`import { defineRecord, defineAlias } from '../serialization/record.js'`) |
| 964 | parts.push(`import { defineEnum } from '../serialization/enum.js'`) |
| 965 | parts.push(`import { defineUnion } from '../serialization/union.js'`) |
| 966 | parts.push('') |
| 967 | } |
| 968 | |
| 969 | if (hasImplementation) { |
| 970 | // Domain.connect(driver) reaches the one BiDi connection for `driver` |
| 971 | // through the real internal accessor (never the deprecated driver.getBidi()). |
| 972 | parts.push(`import { Domain, event, type EventDescriptor, DOMAIN_TOKEN } from '../domain.js'`) |
| 973 | parts.push('') |
| 974 | } |
| 975 | |
| 976 | // tsName -> {runtimeName, kind, fields?}, so a command method in this same file |
| 977 | // can validate its own params/result directly (rather than only reaching |
| 978 | // nested/cross-domain refs indirectly through the shared registry) and document |
| 979 | // each params field in its JSDoc from the same field list, not a second copy. |
| 980 | const runtimeByTsName = new Map() |
| 981 | // Enum constants owned by this domain, exposed as a discoverable static property |
| 982 | // on the domain class (or a top-level export when there's no class to attach to) — |
| 983 | // e.g. Network.SameSite = { STRICT: 'strict', ... }, mirroring how Ruby's generator |
| 984 | // exposes Network::SAME_SITE as a real, inspectable constant rather than a bare string. |
| 985 | const enumConstants = [] |
| 986 | |
| 987 | if (typeEntries.length > 0) { |
| 988 | parts.push(`// --- Types ---`) |
| 989 | parts.push('') |
| 990 | for (const [name, node] of typeEntries) { |
| 991 | const declared = generateTypeDeclaration(name, node) |
| 992 | const tsName = normalizeDottedName(name) |
| 993 | if (declared.runtimeName) { |
| 994 | runtimeByTsName.set(tsName, { |
| 995 | runtimeName: declared.runtimeName, |
| 996 | kind: declared.kind, |
| 997 | fields: node.kind === 'record' ? node.fields : undefined, |
| 998 | }) |
| 999 | } else if (declared.kind === 'alias') { |
no test coverage detected