<commit_before>'use strict';

var parser = exports;
var esprima = require('esprima');
var node_path = require('path');
var fs = require('fs');
var util = require('util');


// Parses a file and get its dependencies and code
parser.parse = function (path, options, callback) {
  if (!parser._guess_is_js(path)) {
    return callback(null, {
      path: path,
      require: [],
      resolve: [],
      async: []
    });
  }

  parser.read(path, function (err, content) {
    if (err) {
      return callback(err);
    }

    parser._lex_js(content.toString(), function (err, ast) {
      if (err) {
        return callback({
          code: 'ERROR_PARSE_JS',
          message: 'Error parsing "' + path + '": ' + err.stack,
          data: {
            path: path,
            error: err
          }
        });
      }

      var dependencies = {
        normal: [],
        resolve: [],
        async: []
      };
      try {
        parser._parse_dependencies(ast, dependencies, options);
      } catch(e) {
        return callback({
          code: 'WRONG_USAGE_REQUIRE',
          message: 'Error parsing dependencies: ' + e.message,
          data: {
            path: path,
            error: e
          }
        });
      }

      if (options.comment_require) {
        parser._parse_comments(ast, dependencies, options);
      }

      callback(null, {
        // code: content,
        path: path,
        require: parser._make_unique(dependencies.normal),
        resolve: parser._make_unique(dependencies.resolve),
        async: parser._make_unique(dependencies.async)
      });
    });
  });
};


parser._make_unique = function (array) {
  return array.reduce(function (prev, current) {
    if (!~prev.indexOf(current)) {
      prev.push(current);
    }
    return prev;
  }, []);
};


var REGEX_EXT = /\.([a-z0-9]+)$/i;

// @returns {Boolean}
parser._guess_is_js = function (path) {
  var match = path.match(REGEX_EXT);
  return match
    ? match[1] === 'js'
    // if there is no extension
    : true;
};


parser.read = function (path, callback) {
  fs.readFile(path, function (err, content) {
    if (err) {
      return callback({
        code: 'ERROR_READ_FILE',
        message: 'Error reading module "' + path + '": ' + err.stack,
        data: {
          path: path,
          error: err
        }
      });
    }

    callback(null, content);
  });
};


// Parses the content of a javascript to AST
parser._lex_js = function (content, callback) {
  var ast;
  try {
    ast = esprima.parse(content, {
      loc: true,
      comment: true
    });
  } catch(e) {
    return callback(e);
  }

  callback(null, ast);
};


// Parses AST and returns the dependencies
parser._parse_dependencies = function (node, dependencies, options) {
  // Only arrays or objects has child node, or is a sub AST.
  if (!node || Object(node) !== node) {
    return;
  }

  parser._check_dependency_node(node, function (node) {
    return node.type === 'CallExpression'
      && node.callee.type === 'Identifier'
      && node.callee.name === 'require';
  }, dependencies.normal, options, true)

  || options.require_resolve && parser._check_dependency_node(node, function (node) {
    return node.type === 'CallExpression'<commit_msg>parser: parser will no longer read file, #23<commit_after>'use strict';

var parser = exports;
var esprima = require('esprima');
var node_path = require('path');
var fs = require('fs');
var util = require('util');


// Parses a file and get its dependencies and code
// @param {String} content
// @param {String} path
// @param {Object} options
// @param {function()} callback
parser.parse = function (path, content, options, callback) {
  if (!parser._guess_is_js(path)) {
    return callback(null, {
      path: path,
      require: [],
      resolve: [],
      async: []
    });
  }

  parser._lex_js(content, function (err, ast) {
    if (err) {
      return callback({
        code: 'ERROR_PARSE_JS',
        message: 'Error parsing "' + path + '": ' + err.stack,
        data: {
          path: path,
          error: err
        }
      });
    }

    var dependencies = {
      normal: [],
      resolve: [],
      async: []
    };

    try {
      parser._parse_dependencies(ast, dependencies, options);
    } catch(e) {
      return callback({
        code: 'WRONG_USAGE_REQUIRE',
        message: 'Error parsing dependencies: ' + e.message,
        data: {
          path: path,
          error: e
        }
      });
    }

    if (options.comment_require) {
      parser._parse_comments(ast, dependencies, options);
    }

    callback(null, {
      // code: content,
      path: path,
      require: parser._make_unique(dependencies.normal),
      resolve: parser._make_unique(dependencies.resolve),
      async: parser._make_unique(dependencies.async)
    });
  });
};


parser._make_unique = function (array) {
  return array.reduce(function (prev, current) {
    if (!~prev.indexOf(current)) {
      prev.push(current);
    }
    return prev;
  }, []);
};


var REGEX_EXT = /\.([a-z0-9]+)$/i;

// @returns {Boolean}
parser._guess_is_js = function (path) {
  var match = path.match(REGEX_EXT);
  return match
    ? match[1] === 'js'
    // if there is no extension
    : true;
};


// Parses the content of a javascript to AST
parser._lex_js = function (content, callback) {
  var ast;
  try {
    ast = esprima.parse(content, {
      loc: true,
      comment: true
    });
  } catch(e) {
    return callback(e);
  }

  callback(null, ast);
};


// Parses AST and returns the dependencies
parser._parse_dependencies = function (node, dependencies, options) {
  // Only arrays or objects has child node, or is a sub AST.
  if (!node || Object(node) !== node) {
    return;
  }

  parser._check_dependency_node(node, function (node) {
    return node.type === 'CallExpression'
      && node.callee.type === 'Identifier'
      && node.callee.name === 'require';
  }, dependencies.normal, options, true)

  || options.require_resolve && parser._check_dependency_node(node, function (node) {
    return node.type === 'CallExpression'
{'fraction_non_alphanumeric': 0.1452991452991453, 'fraction_numerical': 0.0013149243918474688, 'mean_word_length': 2.4070548712206046, 'pattern_counts': {'":': 3, '<': 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': '2429363', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1856, 'n_words': 468}