* The vendor variants of one spec domain, one per namespace (`moz` → `MozWebExtension`), each a * subclass of the shared domain class, so a non-matching browser never sees a vendor field or * command. Mirrors Ruby's generator (bidi_generate.rb#vendor_modules_for): * * - a command whose params ty
(schema, domain, parentClassName, specCommands)
| 659 | * types: Object<string, object>, commands: Array<object>}>} |
| 660 | */ |
| 661 | function buildVendorModules(schema, domain, parentClassName, specCommands) { |
| 662 | const vendorTypes = Object.assign({}, ...Object.values(schema.vendor ?? {}).map((s) => s.types ?? {})) |
| 663 | |
| 664 | return Object.entries(schema.vendor ?? {}).flatMap(([namespace, section]) => { |
| 665 | const stray = (section.events ?? []).find((e) => e.domain === domain) |
| 666 | if (stray) { |
| 667 | throw new Error(`vendor event ${stray.method} in spec domain ${domain} is unsupported`) |
| 668 | } |
| 669 | |
| 670 | const types = {} |
| 671 | const commands = [] |
| 672 | |
| 673 | for (const [typeName, entry] of Object.entries(section.extends ?? {})) { |
| 674 | const cmd = schema.commands.find((c) => c.params?.ref === typeName) |
| 675 | if (!cmd || cmd.domain !== domain) continue |
| 676 | const base = schema.types[typeName] |
| 677 | const taken = new Set(base.fields.map((f) => f.name)) |
| 678 | const vendorFields = entry.fields.map((f) => ({ ...f, name: vendorFieldName(f.name, namespace, taken) })) |
| 679 | const vendorTypeName = vendorExtendedTypeName(namespace, typeName) |
| 680 | types[vendorTypeName] = { ...base, fields: [...base.fields, ...vendorFields] } |
| 681 | commands.push({ ...schemaToCommand(cmd), paramsTypeName: normalizeDottedName(vendorTypeName) }) |
| 682 | } |
| 683 | |
| 684 | const specNames = new Set(specCommands.map((c) => c.methodName)) |
| 685 | for (const cmd of (section.commands ?? []).filter((c) => c.domain === domain)) { |
| 686 | const methodName = cmd.name.replace(`${namespace}:`, '') |
| 687 | if (specNames.has(methodName)) { |
| 688 | throw new Error(`vendor command ${cmd.method} would override spec command ${domain}.${methodName}`) |
| 689 | } |
| 690 | commands.push({ ...schemaToCommand(cmd), methodName, specUrl: null }) |
| 691 | for (const ref of [cmd.params?.ref, cmd.result?.ref].filter(Boolean)) { |
| 692 | collectVendorTypes(ref, vendorTypes, types) |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (commands.length === 0) return [] |
| 697 | return [{ namespace, className: `${pascalCase(namespace)}${parentClassName}`, parentClassName, types, commands }] |
| 698 | }) |
| 699 | } |
| 700 | |
| 701 | /** Adds `name` and every vendor type it transitively references to `into`; spec types are left to their own domain. */ |
| 702 | function collectVendorTypes(name, vendorTypes, into) { |
no test coverage detected