* Constructs a `Scope` given either a `Template` or a list of `Node`s. * * @param tcb the overall context of TCB generation. * @param parentScope the `Scope` of the parent template (if any) or `null` if this is the root * `Scope`. * @param scopedNode Node that provides the scope aroun
(
tcb: Context,
parentScope: Scope | null,
scopedNode: Template | IfBlockBranch | ForLoopBlock | HostElement | BoundaryErrorBlock | null,
children: Node[] | null,
guard: TcbExpr | null,
)
| 179 | * @param guard an expression that is applied to this scope for type narrowing purposes. |
| 180 | */ |
| 181 | static forNodes( |
| 182 | tcb: Context, |
| 183 | parentScope: Scope | null, |
| 184 | scopedNode: Template | IfBlockBranch | ForLoopBlock | HostElement | BoundaryErrorBlock | null, |
| 185 | children: Node[] | null, |
| 186 | guard: TcbExpr | null, |
| 187 | ): Scope { |
| 188 | const scope = new Scope(tcb, parentScope, guard); |
| 189 | |
| 190 | if (parentScope === null && tcb.env.config.enableTemplateTypeChecker) { |
| 191 | // Add an autocompletion point for the component context. |
| 192 | scope.opQueue.push(new TcbComponentContextCompletionOp(scope)); |
| 193 | } |
| 194 | |
| 195 | // If given an actual `Template` instance, then process any additional information it |
| 196 | // has. |
| 197 | if (scopedNode instanceof Template) { |
| 198 | // The template's variable declarations need to be added as `TcbVariableOp`s. |
| 199 | const varMap = new Map<string, Variable>(); |
| 200 | |
| 201 | for (const v of scopedNode.variables) { |
| 202 | // Validate that variables on the `Template` are only declared once. |
| 203 | if (!varMap.has(v.name)) { |
| 204 | varMap.set(v.name, v); |
| 205 | } else { |
| 206 | const firstDecl = varMap.get(v.name)!; |
| 207 | tcb.oobRecorder.duplicateTemplateVar(tcb.id, v, firstDecl); |
| 208 | } |
| 209 | Scope.registerVariable(scope, v, new TcbTemplateVariableOp(tcb, scope, scopedNode, v)); |
| 210 | } |
| 211 | } else if (scopedNode instanceof IfBlockBranch) { |
| 212 | const {expression, expressionAlias} = scopedNode; |
| 213 | if (expression !== null && expressionAlias !== null) { |
| 214 | Scope.registerVariable( |
| 215 | scope, |
| 216 | expressionAlias, |
| 217 | new TcbBlockVariableOp( |
| 218 | tcb, |
| 219 | scope, |
| 220 | tcbExpression(expression, tcb, scope), |
| 221 | expressionAlias, |
| 222 | ), |
| 223 | ); |
| 224 | } |
| 225 | } else if (scopedNode instanceof BoundaryErrorBlock) { |
| 226 | for (const variable of scopedNode.contextVariables) { |
| 227 | let typeExpr: TcbExpr; |
| 228 | if (variable.value === '$error') { |
| 229 | typeExpr = new TcbExpr(`(err as Error)`); |
| 230 | } else if (variable.value === '$reset') { |
| 231 | typeExpr = new TcbExpr(`(() => {})`); |
| 232 | } else { |
| 233 | throw new Error(`Unrecognized context variable ${variable.value}`); |
| 234 | } |
| 235 | Scope.registerVariable( |
| 236 | scope, |
| 237 | variable, |
| 238 | new TcbBlockVariableOp(tcb, scope, typeExpr, variable), |
no test coverage detected