* Ingest an `@switch` block into the given `ViewCompilation`.
(unit: ViewCompilationUnit, switchBlock: t.SwitchBlock)
| 674 | * Ingest an `@switch` block into the given `ViewCompilation`. |
| 675 | */ |
| 676 | function ingestSwitchBlock(unit: ViewCompilationUnit, switchBlock: t.SwitchBlock): void { |
| 677 | // Don't ingest empty switches since they won't render anything. |
| 678 | if (switchBlock.groups.length === 0) { |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | let firstXref: ir.XrefId | null = null; |
| 683 | let conditions: Array<ir.ConditionalCaseExpr> = []; |
| 684 | for (let i = 0; i < switchBlock.groups.length; i++) { |
| 685 | const switchCaseGroup = switchBlock.groups[i]; |
| 686 | const cView = unit.job.allocateView(unit.xref); |
| 687 | const tagName = ingestControlFlowInsertionPoint(unit, cView.xref, switchCaseGroup); |
| 688 | let switchCaseI18nMeta: i18n.BlockPlaceholder | undefined = undefined; |
| 689 | if (switchCaseGroup.i18n !== undefined) { |
| 690 | if (!(switchCaseGroup.i18n instanceof i18n.BlockPlaceholder)) { |
| 691 | throw Error( |
| 692 | `Unhandled i18n metadata type for switch block: ${switchCaseGroup.i18n?.constructor.name}`, |
| 693 | ); |
| 694 | } |
| 695 | switchCaseI18nMeta = switchCaseGroup.i18n; |
| 696 | } |
| 697 | |
| 698 | const createOp = i === 0 ? ir.createConditionalCreateOp : ir.createConditionalBranchCreateOp; |
| 699 | |
| 700 | const conditionalCreateOp = createOp( |
| 701 | cView.xref, |
| 702 | ir.TemplateKind.Block, |
| 703 | tagName, |
| 704 | 'Case', |
| 705 | ir.Namespace.HTML, |
| 706 | switchCaseI18nMeta, |
| 707 | switchCaseGroup.startSourceSpan, |
| 708 | switchCaseGroup.sourceSpan, |
| 709 | ); |
| 710 | unit.create.push(conditionalCreateOp); |
| 711 | |
| 712 | if (firstXref === null) { |
| 713 | firstXref = cView.xref; |
| 714 | } |
| 715 | |
| 716 | for (const switchCase of switchCaseGroup.cases) { |
| 717 | const caseExpr = switchCase.expression |
| 718 | ? convertAst(switchCase.expression, unit.job, switchBlock.startSourceSpan) |
| 719 | : null; |
| 720 | const conditionalCaseExpr = new ir.ConditionalCaseExpr( |
| 721 | caseExpr, |
| 722 | conditionalCreateOp.xref, |
| 723 | conditionalCreateOp.handle, |
| 724 | ); |
| 725 | conditions.push(conditionalCaseExpr); |
| 726 | } |
| 727 | ingestNodes(cView, switchCaseGroup.children); |
| 728 | } |
| 729 | unit.update.push( |
| 730 | ir.createConditionalOp( |
| 731 | firstXref!, |
| 732 | convertAst(switchBlock.expression, unit.job, null), |
| 733 | conditions, |
no test coverage detected