({ domain, className, types, commands, events, vendorModules = [], specVersion })
| 999 | } |
| 1000 | |
| 1001 | function generateDomainFile({ domain, className, types, commands, events, vendorModules = [], specVersion }) { |
| 1002 | const parts = [LICENSE_HEADER, '', GENERATED_NOTE] |
| 1003 | |
| 1004 | parts.push(`// Built from the WebDriver BiDi CDDL spec (v${specVersion}).`) |
| 1005 | parts.push(`// Source: https://github.com/w3c/webref/tree/main/ed/cddl`) |
| 1006 | parts.push('') |
| 1007 | |
| 1008 | const hasImplementation = className != null && (commands.length > 0 || events.length > 0) |
| 1009 | |
| 1010 | // Vendor types follow the spec's, and are tagged with the vendor class that owns their enum constants. |
| 1011 | const vendorTypeOwner = new Map(vendorModules.flatMap((m) => Object.keys(m.types).map((name) => [name, m]))) |
| 1012 | const allTypes = Object.assign({}, types, ...vendorModules.map((m) => m.types)) |
| 1013 | const typeEntries = Object.entries(allTypes) |
| 1014 | |
| 1015 | const crossDomainImports = computeCrossDomainImports(allTypes, domain) |
| 1016 | if (crossDomainImports.length > 0) { |
| 1017 | for (const line of crossDomainImports) parts.push(line) |
| 1018 | parts.push('') |
| 1019 | } |
| 1020 | |
| 1021 | if (typeEntries.length > 0) { |
| 1022 | parts.push(`import { defineRecord, defineAlias } from '../serialization/record.js'`) |
| 1023 | parts.push(`import { defineEnum } from '../serialization/enum.js'`) |
| 1024 | parts.push(`import { defineUnion } from '../serialization/union.js'`) |
| 1025 | parts.push('') |
| 1026 | } |
| 1027 | |
| 1028 | if (hasImplementation) { |
| 1029 | // Domain.connect(driver) reaches the one BiDi connection for `driver` |
| 1030 | // through the real internal accessor (never the deprecated driver.getBidi()). |
| 1031 | parts.push(`import { Domain, event, type EventDescriptor, DOMAIN_TOKEN } from '../domain.js'`) |
| 1032 | parts.push('') |
| 1033 | } |
| 1034 | |
| 1035 | // tsName -> {runtimeName, kind, fields?}, so a command method in this same file |
| 1036 | // can validate its own params/result directly (rather than only reaching |
| 1037 | // nested/cross-domain refs indirectly through the shared registry) and document |
| 1038 | // each params field in its JSDoc from the same field list, not a second copy. |
| 1039 | const runtimeByTsName = new Map() |
| 1040 | // Enum constants owned by this domain, exposed as a discoverable static property |
| 1041 | // on the domain class (or a top-level export when there's no class to attach to) — |
| 1042 | // e.g. Network.SameSite = { STRICT: 'strict', ... }, mirroring how Ruby's generator |
| 1043 | // exposes Network::SAME_SITE as a real, inspectable constant rather than a bare string. |
| 1044 | const enumConstants = [] |
| 1045 | |
| 1046 | if (typeEntries.length > 0) { |
| 1047 | parts.push(`// --- Types ---`) |
| 1048 | parts.push('') |
| 1049 | for (const [name, node] of typeEntries) { |
| 1050 | const declared = generateTypeDeclaration(name, node) |
| 1051 | const tsName = normalizeDottedName(name) |
| 1052 | if (declared.runtimeName) { |
| 1053 | runtimeByTsName.set(tsName, { |
| 1054 | runtimeName: declared.runtimeName, |
| 1055 | kind: declared.kind, |
| 1056 | fields: node.kind === 'record' ? node.fields : undefined, |
| 1057 | }) |
| 1058 | } else if (declared.kind === 'alias') { |
no test coverage detected