MCPcopy Create free account
hub / github.com/angular/angular / ingestIfBlock

Function ingestIfBlock

packages/compiler/src/template/pipeline/src/ingest.ts:506–555  ·  view source on GitHub ↗

* Ingest an `@if` block into the given `ViewCompilation`.

(unit: ViewCompilationUnit, ifBlock: t.IfBlock)

Source from the content-addressed store, hash-verified

504 * Ingest an `@if` block into the given `ViewCompilation`.
505 */
506function ingestIfBlock(unit: ViewCompilationUnit, ifBlock: t.IfBlock): void {
507 let firstXref: ir.XrefId | null = null;
508 let conditions: Array<ir.ConditionalCaseExpr> = [];
509 for (let i = 0; i < ifBlock.branches.length; i++) {
510 const ifCase = ifBlock.branches[i];
511 const cView = unit.job.allocateView(unit.xref);
512 const tagName = ingestControlFlowInsertionPoint(unit, cView.xref, ifCase);
513
514 if (ifCase.expressionAlias !== null) {
515 cView.contextVariables.set(ifCase.expressionAlias.name, ir.CTX_REF);
516 }
517
518 let ifCaseI18nMeta: i18n.BlockPlaceholder | undefined = undefined;
519 if (ifCase.i18n !== undefined) {
520 if (!(ifCase.i18n instanceof i18n.BlockPlaceholder)) {
521 throw Error(`Unhandled i18n metadata type for if block: ${ifCase.i18n?.constructor.name}`);
522 }
523 ifCaseI18nMeta = ifCase.i18n;
524 }
525
526 const createOp = i === 0 ? ir.createConditionalCreateOp : ir.createConditionalBranchCreateOp;
527
528 const conditionalCreateOp = createOp(
529 cView.xref,
530 ir.TemplateKind.Block,
531 tagName,
532 'Conditional',
533 ir.Namespace.HTML,
534 ifCaseI18nMeta,
535 ifCase.startSourceSpan,
536 ifCase.sourceSpan,
537 );
538 unit.create.push(conditionalCreateOp);
539
540 if (firstXref === null) {
541 firstXref = cView.xref;
542 }
543
544 const caseExpr = ifCase.expression ? convertAst(ifCase.expression, unit.job, null) : null;
545 const conditionalCaseExpr = new ir.ConditionalCaseExpr(
546 caseExpr,
547 conditionalCreateOp.xref,
548 conditionalCreateOp.handle,
549 ifCase.expressionAlias,
550 );
551 conditions.push(conditionalCaseExpr);
552 ingestNodes(cView, ifCase.children);
553 }
554 unit.update.push(ir.createConditionalOp(firstXref!, null, conditions, ifBlock.sourceSpan));
555}
556
557/**
558 * Ingest an `@switch` block into the given `ViewCompilation`.

Callers 1

ingestNodesFunction · 0.85

Calls 7

ErrorInterface · 0.85
convertAstFunction · 0.85
ingestNodesFunction · 0.85
allocateViewMethod · 0.80
setMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected