* Walk the `CommandData` or `EventData` union type hierarchy and collect all * leaf definition names (the actual command/event group names). * * The CDDL AST represents union groups with Properties that can be: * - An array of choice objects (each with a Type.Value pointing to the next level)
(rootName, defMap, visited = new Set())
| 392 | * A leaf is a definition that itself has a `method` property (string literal). |
| 393 | */ |
| 394 | function collectUnionMembers(rootName, defMap, visited = new Set()) { |
| 395 | if (visited.has(rootName)) return new Set() |
| 396 | visited.add(rootName) |
| 397 | |
| 398 | const def = defMap.get(rootName) |
| 399 | if (!def) return new Set() |
| 400 | |
| 401 | const members = new Set() |
| 402 | |
| 403 | // Flatten Properties — each element is either a choice-array or a property object. A one-member |
| 404 | // aggregate (`MozCommand = webExtension.MozListExtensions`) parses as a variable holding a PropertyType. |
| 405 | const rawProps = def.Properties ?? (def.Type === 'variable' ? [{ Type: def.PropertyType }] : []) |
| 406 | const allChoices = [] |
| 407 | for (const prop of rawProps) { |
| 408 | if (Array.isArray(prop)) { |
| 409 | allChoices.push(...prop) |
| 410 | } else { |
| 411 | allChoices.push(prop) |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | for (const choice of allChoices) { |
| 416 | // choice.Type can be a single object or an array of type alternatives. |
| 417 | const typeEntries = Array.isArray(choice.Type) ? choice.Type : [choice.Type] |
| 418 | |
| 419 | for (const entry of typeEntries) { |
| 420 | if (entry?.Type !== 'group' || !entry.Value) continue |
| 421 | const childName = entry.Value |
| 422 | const childDef = defMap.get(childName) |
| 423 | if (!childDef) continue |
| 424 | |
| 425 | // A leaf has a `method` property — it is the actual command or event definition. |
| 426 | const childProps = childDef.Properties ?? [] |
| 427 | const flat = childProps.flatMap((p) => (Array.isArray(p) ? p : [p])) |
| 428 | if (flat.some((p) => p.Name === 'method')) { |
| 429 | members.add(childName) |
| 430 | } else { |
| 431 | // Intermediate union — recurse. |
| 432 | for (const m of collectUnionMembers(childName, defMap, visited)) { |
| 433 | members.add(m) |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | return members |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Build a name → definition map from the AST (deduplicated — first wins). |
no test coverage detected