<commit_before>import {isBlank, isPresent, StringWrapper} from 'angular2/src/facade/lang';
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {EventHandlerVars, ViewProperties} from './constants';

import * as o from '../output/output_ast';
import {CompileElement} from './compile_element';
import {CompileMethod} from './compile_method';

import {BoundEventAst, DirectiveAst} from '../template_ast';
import {CompileDirectiveMetadata} from '../compile_metadata';

import {convertCdStatementToIr} from './expression_converter';
import {CompileBinding} from './compile_binding';

export class CompileEventListener {
  private _method: CompileMethod;
  private _hasComponentHostListener: boolean = false;
  private _methodName: string;
  private _eventParam: o.FnParam;
  private _actionResultExprs: o.Expression[] = [];

  static getOrCreate(compileElement: CompileElement, eventTarget: string, eventName: string,
                     targetEventListeners: CompileEventListener[]): CompileEventListener {
    var listener = targetEventListeners.find(listener => listener.eventTarget == eventTarget &&
                                                         listener.eventName == eventName);
    if (isBlank(listener)) {
      listener = new CompileEventListener(compileElement, eventTarget, eventName,
                                          targetEventListeners.length);
      targetEventListeners.push(listener);
    }
    return listener;
  }

  constructor(public compileElement: CompileElement, public eventTarget: string,
              public eventName: string, listenerIndex: number) {
    this._method = new CompileMethod(compileElement.view);
    this._methodName =
        `_handle_${santitizeEventName(eventName)}_${compileElement.nodeIndex}_${listenerIndex}`;
    this._eventParam =
        new o.FnParam(EventHandlerVars.event.name,
                      o.importType(this.compileElement.view.genConfig.renderTypes.renderEvent));
  }

  addAction(hostEvent: BoundEventAst, directive: CompileDirectiveMetadata,
            directiveInstance: o.Expression) {
    if (isPresent(directive) && directive.isComponent) {
      this._hasComponentHostListener = true;
    }
    this._method.resetDebugInfo(this.compileElement.nodeIndex, hostEvent);
    var context = isPresent(directiveInstance) ? directiveInstance : o.THIS_EXPR.prop('context');
    var actionStmts = convertCdStatementToIr(this.compileElement.view, context, hostEvent.handler);
    var lastIndex = actionStmts.length - 1;
    if (lastIndex >= 0) {
      var lastStatement = actionStmts[lastIndex];
      var returnExpr = convertStmtIntoExpression(lastStatement);
      var preventDefaultVar = o.variable(`pd_${this._actionResultExprs.length}`);
      this._actionResultExprs.push(preventDefaultVar);
      if (isPresent(returnExpr)) {
        // Note: We need to cast the result of the method call to dynamic,
        // as it might be a void method!
        actionStmts[lastIndex] =
            preventDefaultVar.set(returnExpr.cast(o.DYNAMIC_TYPE).notIdentical(o.literal(false)))
                .toDeclStmt(null, [o.StmtModifier.Final]);
      }
    }
    this._method.addStmts(actionStmts);
  }

  finishMethod() {
    var markPathToRootStart = this._hasComponentHostListener ?
                                  this.compileElement.appElement.prop('componentView') :
                                  o.THIS_EXPR;
    var resultExpr: o.Expression = o.literal(true);
    this._actionResultExprs.forEach((expr) => { resultExpr = resultExpr.and(expr); });
    var stmts =
        (<o.Statement[]>[markPathToRootStart.callMethod('markPathToRootAsCheckOnce', []).toStmt()])
            .concat(this._method.finish())
            .concat([new o.ReturnStatement(resultExpr)]);
    this.compileElement.view.eventHandlerMethods.push(new o.ClassMethod(
        this._methodName, [this._eventParam], stmts, o.BOOL_TYPE, [o.StmtModifier.Private]));
  }

  listenToRenderer() {
    var listenExpr;
    var eventListener = o.THIS_EXPR.callMethod('eventHandler', [
      o.fn([this._eventParam],
           [
             new o.ReturnStatement(
                 o.THIS_EXPR.callMethod(this._methodName, [EventHandlerVars.event]))
           ])
    ]);
    if (isPresent(this.eventTarget)) {
      listenExpr = ViewProperties.renderer.callMethod(
          'listenGlobal', [o.literal(this.eventTarget), o.literal(this.eventName), eventListener]);
    } else {
      listenExpr = ViewProperties.renderer.callMethod(
          'listen', [this.compileElement.renderNode, o.literal(this.eventName), eventListener]);
    }
    var disposable = o.variable(`disposable_${this.compileElement.view.disposables.length}`);
    this.compileElement.view.disposables.push(disposable);
    this.compileElement.view.createMethod.addStmt(
        disposable.set(listenExpr).toDeclStmt(o.FUNCTION_TYPE, [o.StmtModifier.Private]));<commit_msg>fix(codegen): event handler has boolean return type<commit_after>import {isBlank, isPresent, StringWrapper} from 'angular2/src/facade/lang';
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {EventHandlerVars, ViewProperties} from './constants';

import * as o from '../output/output_ast';
import {CompileElement} from './compile_element';
import {CompileMethod} from './compile_method';

import {BoundEventAst, DirectiveAst} from '../template_ast';
import {CompileDirectiveMetadata} from '../compile_metadata';

import {convertCdStatementToIr} from './expression_converter';
import {CompileBinding} from './compile_binding';

export class CompileEventListener {
  private _method: CompileMethod;
  private _hasComponentHostListener: boolean = false;
  private _methodName: string;
  private _eventParam: o.FnParam;
  private _actionResultExprs: o.Expression[] = [];

  static getOrCreate(compileElement: CompileElement, eventTarget: string, eventName: string,
                     targetEventListeners: CompileEventListener[]): CompileEventListener {
    var listener = targetEventListeners.find(listener => listener.eventTarget == eventTarget &&
                                                         listener.eventName == eventName);
    if (isBlank(listener)) {
      listener = new CompileEventListener(compileElement, eventTarget, eventName,
                                          targetEventListeners.length);
      targetEventListeners.push(listener);
    }
    return listener;
  }

  constructor(public compileElement: CompileElement, public eventTarget: string,
              public eventName: string, listenerIndex: number) {
    this._method = new CompileMethod(compileElement.view);
    this._methodName =
        `_handle_${santitizeEventName(eventName)}_${compileElement.nodeIndex}_${listenerIndex}`;
    this._eventParam =
        new o.FnParam(EventHandlerVars.event.name,
                      o.importType(this.compileElement.view.genConfig.renderTypes.renderEvent));
  }

  addAction(hostEvent: BoundEventAst, directive: CompileDirectiveMetadata,
            directiveInstance: o.Expression) {
    if (isPresent(directive) && directive.isComponent) {
      this._hasComponentHostListener = true;
    }
    this._method.resetDebugInfo(this.compileElement.nodeIndex, hostEvent);
    var context = isPresent(directiveInstance) ? directiveInstance : o.THIS_EXPR.prop('context');
    var actionStmts = convertCdStatementToIr(this.compileElement.view, context, hostEvent.handler);
    var lastIndex = actionStmts.length - 1;
    if (lastIndex >= 0) {
      var lastStatement = actionStmts[lastIndex];
      var returnExpr = convertStmtIntoExpression(lastStatement);
      var preventDefaultVar = o.variable(`pd_${this._actionResultExprs.length}`);
      this._actionResultExprs.push(preventDefaultVar);
      if (isPresent(returnExpr)) {
        // Note: We need to cast the result of the method call to dynamic,
        // as it might be a void method!
        actionStmts[lastIndex] =
            preventDefaultVar.set(returnExpr.cast(o.DYNAMIC_TYPE).notIdentical(o.literal(false)))
                .toDeclStmt(null, [o.StmtModifier.Final]);
      }
    }
    this._method.addStmts(actionStmts);
  }

  finishMethod() {
    var markPathToRootStart = this._hasComponentHostListener ?
                                  this.compileElement.appElement.prop('componentView') :
                                  o.THIS_EXPR;
    var resultExpr: o.Expression = o.literal(true);
    this._actionResultExprs.forEach((expr) => { resultExpr = resultExpr.and(expr); });
    var stmts =
        (<o.Statement[]>[markPathToRootStart.callMethod('markPathToRootAsCheckOnce', []).toStmt()])
            .concat(this._method.finish())
            .concat([new o.ReturnStatement(resultExpr)]);
    this.compileElement.view.eventHandlerMethods.push(new o.ClassMethod(
        this._methodName, [this._eventParam], stmts, o.BOOL_TYPE, [o.StmtModifier.Private]));
  }

  listenToRenderer() {
    var listenExpr;
    var eventListener = o.THIS_EXPR.callMethod('eventHandler', [
      o.fn([this._eventParam],
           [
             new o.ReturnStatement(
                 o.THIS_EXPR.callMethod(this._methodName, [EventHandlerVars.event]))
           ], o.BOOL_TYPE)
    ]);
    if (isPresent(this.eventTarget)) {
      listenExpr = ViewProperties.renderer.callMethod(
          'listenGlobal', [o.literal(this.eventTarget), o.literal(this.eventName), eventListener]);
    } else {
      listenExpr = ViewProperties.renderer.callMethod(
          'listen', [this.compileElement.renderNode, o.literal(this.eventName), eventListener]);
    }
    var disposable = o.variable(`disposable_${this.compileElement.view.disposables.length}`);
    this.compileElement.view.disposables.push(disposable);
    this.compileElement.view.createMethod.addStmt(
        disposable.set(listenExpr).toDeclStmt(o.FUNCTION_TYPE, [o.StmtModifier.Private]));
{'fraction_non_alphanumeric': 0.11272874330199172, 'fraction_numerical': 0.0008088160954402993, 'mean_word_length': 3.449842555105713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5105289', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2726, 'n_words': 486}