<commit_before>/// <reference path='../node_modules/typescript/bin/typescript.d.ts' />
import base = require('./base');
import ts = require('typescript');
import ts2dart = require('./main');

type FacadeHandler = (c: ts.CallExpression, context: ts.Expression) => void;

const FACADE_DEBUG = false;

export class FacadeConverter extends base.TranspilerBase {
  private tc: ts.TypeChecker;
  private candidateMethods: {[fileName: string]: boolean};

  constructor(transpiler: ts2dart.Transpiler) {
    super(transpiler);
    this.candidateMethods = {};
    for (var fileName in this.subs) {
      Object.keys(this.subs[fileName])
          .filter((k) => this.subs[fileName].hasOwnProperty(k))
          .map((fnName) => fnName.substring(fnName.lastIndexOf('.') + 1))
          .forEach((fnName) => this.candidateMethods[fnName] = true);
    }
  }

  setTypeChecker(tc: ts.TypeChecker) { this.tc = tc; }

  maybeHandleCall(c: ts.CallExpression): boolean {
    if (!this.tc) return false;

    var symbol: ts.Symbol;
    var context: ts.Expression;
    var ident: string;

    if (c.expression.kind === ts.SyntaxKind.Identifier) {
      // Function call.
      ident = base.ident(c.expression);
      if (!this.candidateMethods.hasOwnProperty(ident)) return false;
      symbol = this.tc.getSymbolAtLocation(c.expression);
      if (FACADE_DEBUG) console.log('s:', symbol);

      if (!symbol) {
        this.reportMissingType(c, ident);
        return false;
      }

      context = null;
    } else if (c.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
      // Method call.
      var pa = <ts.PropertyAccessExpression>c.expression;
      ident = base.ident(pa.name);
      if (!this.candidateMethods.hasOwnProperty(ident)) return false;

      symbol = this.tc.getSymbolAtLocation(pa);
      if (FACADE_DEBUG) console.log('s:', symbol);

      // Error will be reported by PropertyAccess handling below.
      if (!symbol) return false;

      context = pa.expression;
    } else {
      // Not a call we recognize.
      return false;
    }

    if (symbol.flags & ts.SymbolFlags.Alias) symbol = this.tc.getAliasedSymbol(symbol);
    if (!symbol.valueDeclaration) return false;

    var fileName = symbol.valueDeclaration.getSourceFile().fileName;
    fileName = this.getRelativeFileName(fileName);
    fileName = fileName.replace(/(\.d)?\.ts$/, '');

    if (FACADE_DEBUG) console.log('fn:', fileName);
    var fileSubs = this.subs[fileName];
    if (!fileSubs) return false;
    var qn = this.tc.getFullyQualifiedName(symbol);
    // Function Qualified Names include their file name. Might be a bug in TypeScript, for the
    // time being just special case.
    if (symbol.flags & ts.SymbolFlags.Function) qn = symbol.getName();

    if (FACADE_DEBUG) console.log('qn', qn);
    var qnSub = fileSubs[qn];
    if (!qnSub) return false;

    if (qnSub(c, context)) return false; // true ==> not handled.
    return true;
  }

  private stdlibSubs: ts.Map<FacadeHandler> = {
    'Array.push': (c: ts.CallExpression, context: ts.Expression) => {<commit_msg>fix: clang-format.<commit_after>/// <reference path='../node_modules/typescript/bin/typescript.d.ts' />
import base = require('./base');
import ts = require('typescript');
import ts2dart = require('./main');

type FacadeHandler = (c: ts.CallExpression, context: ts.Expression) => void;

const FACADE_DEBUG = false;

export class FacadeConverter extends base.TranspilerBase {
  private tc: ts.TypeChecker;
  private candidateMethods: {[fileName: string]: boolean};

  constructor(transpiler: ts2dart.Transpiler) {
    super(transpiler);
    this.candidateMethods = {};
    for (var fileName in this.subs) {
      Object.keys(this.subs[fileName])
          .filter((k) => this.subs[fileName].hasOwnProperty(k))
          .map((fnName) => fnName.substring(fnName.lastIndexOf('.') + 1))
          .forEach((fnName) => this.candidateMethods[fnName] = true);
    }
  }

  setTypeChecker(tc: ts.TypeChecker) { this.tc = tc; }

  maybeHandleCall(c: ts.CallExpression): boolean {
    if (!this.tc) return false;

    var symbol: ts.Symbol;
    var context: ts.Expression;
    var ident: string;

    if (c.expression.kind === ts.SyntaxKind.Identifier) {
      // Function call.
      ident = base.ident(c.expression);
      if (!this.candidateMethods.hasOwnProperty(ident)) return false;
      symbol = this.tc.getSymbolAtLocation(c.expression);
      if (FACADE_DEBUG) console.log('s:', symbol);

      if (!symbol) {
        this.reportMissingType(c, ident);
        return false;
      }

      context = null;
    } else if (c.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
      // Method call.
      var pa = <ts.PropertyAccessExpression>c.expression;
      ident = base.ident(pa.name);
      if (!this.candidateMethods.hasOwnProperty(ident)) return false;

      symbol = this.tc.getSymbolAtLocation(pa);
      if (FACADE_DEBUG) console.log('s:', symbol);

      // Error will be reported by PropertyAccess handling below.
      if (!symbol) return false;

      context = pa.expression;
    } else {
      // Not a call we recognize.
      return false;
    }

    if (symbol.flags & ts.SymbolFlags.Alias) symbol = this.tc.getAliasedSymbol(symbol);
    if (!symbol.valueDeclaration) return false;

    var fileName = symbol.valueDeclaration.getSourceFile().fileName;
    fileName = this.getRelativeFileName(fileName);
    fileName = fileName.replace(/(\.d)?\.ts$/, '');

    if (FACADE_DEBUG) console.log('fn:', fileName);
    var fileSubs = this.subs[fileName];
    if (!fileSubs) return false;
    var qn = this.tc.getFullyQualifiedName(symbol);
    // Function Qualified Names include their file name. Might be a bug in TypeScript, for the
    // time being just special case.
    if (symbol.flags & ts.SymbolFlags.Function) qn = symbol.getName();

    if (FACADE_DEBUG) console.log('qn', qn);
    var qnSub = fileSubs[qn];
    if (!qnSub) return false;

    if (qnSub(c, context)) return false;  // true ==> not handled.
    return true;
  }

  private stdlibSubs: ts.Map<FacadeHandler> = {
    'Array.push': (c: ts.CallExpression, context: ts.Expression) => {
{'fraction_non_alphanumeric': 0.13967479674796748, 'fraction_numerical': 0.000975609756097561, 'mean_word_length': 3.2159013022618232, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '320304', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1918, 'n_words': 451}