MCPcopy Index your code
hub / github.com/angular/angular / TcbIfBlockOp

Class TcbIfBlockOp

packages/compiler/src/typecheck/ops/if_block.ts:21–137  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19 * Executing this operation returns nothing.
20 */
21export class TcbIfBlockOp extends TcbOp {
22 private expressionScopes = new Map<IfBlockBranch, Scope>();
23
24 constructor(
25 private tcb: Context,
26 private scope: Scope,
27 private block: IfBlock,
28 ) {
29 super();
30 }
31
32 override get optional() {
33 return false;
34 }
35
36 override execute(): null {
37 const root = this.generateBranch(0);
38 root && this.scope.addStatement(root);
39 return null;
40 }
41
42 private generateBranch(index: number): TcbExpr | undefined {
43 const branch = this.block.branches[index];
44
45 if (!branch) {
46 return undefined;
47 }
48
49 // If the expression is null, it means that it's an `else` statement.
50 if (branch.expression === null) {
51 const branchScope = this.getBranchScope(this.scope, branch, index);
52 return new TcbExpr(`{\n${getStatementsBlock(branchScope.render())}}`);
53 }
54
55 // We process the expression first in the parent scope, but create a scope around the block
56 // that the body will inherit from. We do this, because we need to declare a separate variable
57 // for the case where the expression has an alias _and_ because we need the processed
58 // expression when generating the guard for the body.
59 const outerScope = this.scope.createChildScope(this.scope, branch, [], null);
60 outerScope.render().forEach((stmt) => this.scope.addStatement(stmt));
61 this.expressionScopes.set(branch, outerScope);
62
63 let expression = tcbExpression(branch.expression, this.tcb, this.scope);
64 if (branch.expressionAlias !== null) {
65 expression = new TcbExpr(
66 `(${expression.print()}) && ${outerScope.resolve(branch.expressionAlias).print()}`,
67 );
68 }
69 const bodyScope = this.getBranchScope(outerScope, branch, index);
70 const ifStatement = `if (${expression.print()}) {\n${getStatementsBlock(bodyScope.render())}}`;
71 const elseBranch = this.generateBranch(index + 1);
72
73 return new TcbExpr(ifStatement + (elseBranch ? ' else ' + elseBranch.print() : ''));
74 }
75
76 private getBranchScope(parentScope: Scope, branch: IfBlockBranch, index: number): Scope {
77 const checkBody = this.tcb.env.config.checkControlFlowBodies;
78 return this.scope.createChildScope(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…