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

Function tcbCreateEventHandler

packages/compiler/src/typecheck/ops/events.ts:305–347  ·  view source on GitHub ↗

* Creates an arrow function to be used as handler function for event bindings. The handler * function has a single parameter `$event` and the bound event's handler `AST` represented as a * TypeScript expression as its body. * * When `eventType` is set to `Infer`, the `$event` parameter will not

(
  event: BoundEvent,
  tcb: Context,
  scope: Scope,
  eventType: EventParamType | string,
  assertionExpression?: TcbExpr,
)

Source from the content-addressed store, hash-verified

303 * bindings. Alternatively, an explicit type can be passed for the `$event` parameter.
304 */
305function tcbCreateEventHandler(
306 event: BoundEvent,
307 tcb: Context,
308 scope: Scope,
309 eventType: EventParamType | string,
310 assertionExpression?: TcbExpr,
311): TcbExpr {
312 const handler = tcbEventHandlerExpression(event.handler, tcb, scope);
313 const statements: TcbExpr[] = [];
314
315 if (assertionExpression !== undefined) {
316 statements.push(assertionExpression);
317 }
318
319 statements.push(handler);
320
321 let eventParamType: string | undefined;
322 if (eventType === EventParamType.Infer) {
323 eventParamType = undefined;
324 } else if (eventType === EventParamType.Any) {
325 eventParamType = 'any';
326 } else {
327 eventParamType = eventType;
328 }
329
330 // Obtain all guards that have been applied to the scope and its parents, as they have to be
331 // repeated within the handler function for their narrowing to be in effect within the handler.
332 const guards = scope.guards();
333 let body = `{\n${getStatementsBlock(statements)} }`;
334
335 if (guards !== null) {
336 // Wrap the body in an `if` statement containing all guards that have to be applied.
337 body = `{ if (${guards.print()}) ${body} }`;
338 }
339
340 const eventParam = new TcbExpr(
341 `${EVENT_PARAMETER}${eventParamType === undefined ? '' : ': ' + eventParamType}`,
342 );
343 eventParam.addExpressionIdentifier(ExpressionIdentifier.EVENT_PARAMETER);
344
345 // Return an arrow function instead of a function expression to preserve the `this` context.
346 return new TcbExpr(`(${eventParam.print()}): any => ${body}`);
347}

Callers 2

executeMethod · 0.85
executeMethod · 0.85

Calls 6

printMethod · 0.95
getStatementsBlockFunction · 0.90
guardsMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected