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