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

Class TcbForOfOp

packages/compiler/src/typecheck/ops/for_block.ts:28–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26 * Executing this operation returns nothing.
27 */
28export class TcbForOfOp extends TcbOp {
29 constructor(
30 private tcb: Context,
31 private scope: Scope,
32 private block: ForLoopBlock,
33 ) {
34 super();
35 }
36
37 override get optional() {
38 return false;
39 }
40
41 override execute(): null {
42 const loopScope = this.scope.createChildScope(
43 this.scope,
44 this.block,
45 this.tcb.env.config.checkControlFlowBodies ? this.block.children : [],
46 null,
47 );
48 const initializerId = loopScope.resolve(this.block.item);
49 const initializer = new TcbExpr(`const ${initializerId.print()}`);
50 initializer.addParseSpanInfo(this.block.item.keySpan);
51
52 // It's common to have a for loop over a nullable value (e.g. produced by the `async` pipe).
53 // Add a non-null expression to allow such values to be assigned.
54 const expr = tcbExpression(this.block.expression, this.tcb, this.scope).wrapForTypeChecker();
55 const expression = new TcbExpr(`${expr.print()}!`).addParseSpanInfo(
56 this.block.expression.sourceSpan,
57 );
58
59 let statements: TcbExpr[];
60
61 if (this.block.trackBy === null) {
62 statements = loopScope.render();
63 } else {
64 const trackTranslator = new TcbForLoopTrackTranslator(this.tcb, loopScope, this.block);
65 const trackExpression = trackTranslator.translate(this.block.trackBy);
66 statements = [...loopScope.render(), trackExpression];
67 }
68
69 this.scope.addStatement(
70 new TcbExpr(
71 `for (${initializer.print()} of ${expression.print()}) {\n${getStatementsBlock(statements)} }`,
72 ),
73 );
74 return null;
75 }
76}
77
78export class TcbForLoopTrackTranslator extends TcbExpressionTranslator {
79 private allowedVariables: Set<Variable>;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected