<commit_before>import estraverse from 'estraverse';
import MethodDefinition from './../syntax/method-definition.js';
import ClassDeclaration from './../syntax/class-declaration.js';
import Identifier from './../syntax/identifier.js';
import ThisExpression from './../syntax/this-expression.js';
import MemberExpression from './../syntax/member-expression.js';
import CallExpression from './../syntax/call-expression.js';
import ReturnStatement from './../syntax/return-statement.js';

export default
  function (ast) {
    estraverse.traverse(ast, {
      enter: functionDetector
    });

    estraverse.traverse(ast, {
      enter: classMaker
    });

    estraverse.replace(ast, {
      enter: classReplacement
    });
  }

var functions = [];

function createClass(_function) {
  if (typeof _function.class === 'undefined') {
    let createdClass = new ClassDeclaration();
    createdClass.name = _function.id.name;

    let constructor = new MethodDefinition();
    constructor.name = 'constructor';
    constructor.body = _function.node.body;

    _function.class = createdClass;
    _function.node._class = createdClass;

    createdClass.body.addMethod(constructor, true);
  }
}

function functionDetector(node, parent) {

  if (node.type === 'FunctionDeclaration') {
    let id = node.id;
    functions.push({
      id: id,
      parent: parent,
      node: node
    });
  } else if (node.type === 'VariableDeclarator' && node.init && node.init.type === 'FunctionExpression') {
    parent._replace = node.init;
    let id = node.id;
    functions.push({
      id: id,
      parent: parent,
      node: node.init
    });
  }

}

function classMaker(node, parent) {

  if (node.type === 'AssignmentExpression') {

    if (node.left.object && node.left.object.property && node.left.object.property.name === 'prototype') {

      let functionName = node.left.object.object.name;

      for (let _function of functions) {

        if (_function.id.name === functionName) {
          createClass(_function);

          let method = node.right;
          let createdMethod = new MethodDefinition();

          if (method.type === 'Identifier') {

            createdMethod.body =
              new ReturnStatement(
                new CallExpression(
                  new MemberExpression(
                    node.right,
                    new Identifier('apply')
                  ), [
                    new ThisExpression(),
                    new Identifier('arguments')
                  ]
                )<commit_msg>fixed: Keep comments for classes #19<commit_after>import estraverse from 'estraverse';
import MethodDefinition from './../syntax/method-definition.js';
import ClassDeclaration from './../syntax/class-declaration.js';
import Identifier from './../syntax/identifier.js';
import ThisExpression from './../syntax/this-expression.js';
import MemberExpression from './../syntax/member-expression.js';
import CallExpression from './../syntax/call-expression.js';
import ReturnStatement from './../syntax/return-statement.js';

export default
  function (ast) {
    estraverse.traverse(ast, {
      enter: functionDetector
    });

    estraverse.traverse(ast, {
      enter: classMaker
    });

    estraverse.replace(ast, {
      enter: classReplacement
    });
  }

var functions = [];

function createClass(_function) {
  if (typeof _function.class === 'undefined') {
    let createdClass = new ClassDeclaration();
    createdClass.name = _function.id.name;

    let constructor = new MethodDefinition();
    constructor.name = 'constructor';
    constructor.body = _function.node.body;
    constructor.leadingComments = _function.node.leadingComments;

    _function.class = createdClass;
    _function.node._class = createdClass;

    createdClass.body.addMethod(constructor, true);
  }
}

function functionDetector(node, parent) {

  if (node.type === 'FunctionDeclaration') {
    let id = node.id;
    functions.push({
      id: id,
      parent: parent,
      node: node
    });
  } else if (node.type === 'VariableDeclarator' && node.init && node.init.type === 'FunctionExpression') {
    parent._replace = node.init;
    let id = node.id;
    functions.push({
      id: id,
      parent: parent,
      node: node.init
    });
  }

}

function classMaker(node, parent) {

  if (node.type === 'AssignmentExpression') {

    if (node.left.object && node.left.object.property && node.left.object.property.name === 'prototype') {

      let functionName = node.left.object.object.name;

      for (let _function of functions) {

        if (_function.id.name === functionName) {
          createClass(_function);

          let method = node.right;
          let createdMethod = new MethodDefinition();
          createdMethod.leadingComments = parent.leadingComments;

          if (method.type === 'Identifier') {

            createdMethod.body =
              new ReturnStatement(
                new CallExpression(
                  new MemberExpression(
                    node.right,
                    new Identifier('apply')
                  ), [
                    new ThisExpression(),
                    new Identifier('arguments')
                  ]
                )
{'fraction_non_alphanumeric': 0.12751677852348994, 'fraction_numerical': 0.0003835091083413231, 'mean_word_length': 2.4293228139381986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4985813', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1376, 'n_words': 319}