<commit_before>'use strict';

var esprima = require('esprima');
var estraverse = require('estraverse');
var _ = require('lodash');
var vfs = require('vinyl-fs');
var glob = require('glob-to-vinyl');
var mapS = require('map-stream');

function parseOptions(opts) {
  var defaultOpts = {
    // default options
  };
  return _.merge(defaultOpts, (opts || {}));
}

function findDependencies(source, opts) {
  opts = parseOptions(opts);

  var mapService = {};

  estraverse.traverse(esprima.parse(source), {
    leave: function(node, parent) {

      if(isService(node)) {
        buildTree(parent, mapService);
      }

    }
  });

  return mapService;
}

function buildTree(node, map) {

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

    var name  = ((node.callee || {}).property || {}).name;
    var service  = ((node.arguments || [])[0] || {}).value;


    if(service) {
      map[service] = {
        name: service,
        type: name,
        checksum: ''
      };

    var args = [];

    args = _.map(
        _.flatten(node
          .arguments
          .map(function (item) {
            return item.params;
          })
          .filter(Boolean), true),
        function (item) {
          return {
            name: item.name,
            checksum: ''
          };
        });

      map[service].dependencies = args;
    }
  }

}

var path = require('path');
var crypto = require('crypto');
function checksum(str, algorithm, encoding) {
    return crypto
        .createHash(algorithm || 'sha1')
        .update(str, 'utf8')
        .digest(encoding || 'hex');
}

function bindCheckSum(config, cb) {
  glob('./src/js/**/**/*.js', function (err, files) {

    if(err) {
      throw err;
    }

    var name;
    files
      .forEach(function (file) {
        name = path.basename(file.path, '.json').split('.')[0];
        if('index' !== name) {
          (config[name] || {}).checksum = checksum(file.contents.toString());
        }
      });

    cb(config);
  });
}

function isService(node) {
  return node.type === 'MemberExpression' && /factory|service|provider|filter|directive|config|value|run/.test(node.property.name);
}

function isAngularModuleStatement(node) {
  return node.type === 'MemberExpression' && node.object.name === 'angular' && node.property.name === 'module';
}

function isNgModuleDeclaration(node) {
  return node.type === 'CallExpression' && node.callee.name === 'angularModule' && node.arguments.length > 0 && node.arguments[0].value === 'ng';
}

module.exports = {
  load: findDependencies,
  checksum: bindCheckSum
};
module.exports.isAngularModuleStatement = isAngularModuleStatement;
module.exports.isNgModuleDeclaration = isNgModuleDeclaration;
<commit_msg>Attach the checkSum per dependency<commit_after>'use strict';

var esprima = require('esprima');
var estraverse = require('estraverse');
var _ = require('lodash');
var vfs = require('vinyl-fs');
var glob = require('glob-to-vinyl');
var mapS = require('map-stream');

function parseOptions(opts) {
  var defaultOpts = {
    // default options
  };
  return _.merge(defaultOpts, (opts || {}));
}

function findDependencies(source, opts) {
  opts = parseOptions(opts);

  var mapService = {};

  estraverse.traverse(esprima.parse(source), {
    leave: function(node, parent) {

      if(isService(node)) {
        buildTree(parent, mapService);
      }

    }
  });

  return mapService;
}

function buildTree(node, map) {

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

    var name  = ((node.callee || {}).property || {}).name;
    var service  = ((node.arguments || [])[0] || {}).value;


    if(service) {
      map[service] = {
        name: service,
        type: name,
        checksum: ''
      };

    var args = [];

    args = _.map(
        _.flatten(node
          .arguments
          .map(function (item) {
            return item.params;
          })
          .filter(Boolean), true),
        function (item) {
          return {
            name: item.name,
            checksum: ''
          };
        });

      map[service].dependencies = (args || [])
        .filter(function (dep) {
          return dep.name.charAt(0) !== '$';
        });
    }
  }

}

var path = require('path');
var crypto = require('crypto');
function checksum(str, algorithm, encoding) {
    return crypto
        .createHash(algorithm || 'sha1')
        .update(str, 'utf8')
        .digest(encoding || 'hex');
}

function bindCheckSum(config, cb) {
  glob('./src/js/**/**/*.js', function (err, files) {

    if(err) {
      throw err;
    }

    var name;
    files
      .forEach(function (file) {
        name = path.basename(file.path, '.json').split('.')[0];
        if('index' !== name) {
          (config[name] || {}).checksum = checksum(file.contents.toString());
        }
      });

    cb(attachCheckSumPerDependency(config));
  });
}

function attachCheckSumPerDependency(data) {

  Object
    .keys(data)
    .forEach(function (service) {

      data[service].dependencies = data[service]
        .dependencies
        .map(function (req) {

          if(req.name) {
            req.checksum = (data[req.name] || {}).checksum;
          }
          return req;
        });

    });

  return data;
}

function isService(node) {
  return node.type === 'MemberExpression' && /factory|service|provider|filter|directive|config|value|run/.test(node.property.name);
}

function isAngularModuleStatement(node) {
  return node.type === 'MemberExpression' && node.object.name === 'angular' && node.property.name === 'module';
}

function isNgModuleDeclaration(node) {
  return node.type === 'CallExpression' && node.callee.name === 'angularModule' && node.arguments.length > 0 && node.arguments[0].value === 'ng';
}

module.exports = {
  load: findDependencies,
  checksum: bindCheckSum
};
module.exports.isAngularModuleStatement = isAngularModuleStatement;
module.exports.isNgModuleDeclaration = isNgModuleDeclaration;

{'fraction_non_alphanumeric': 0.16683587140439932, 'fraction_numerical': 0.0021996615905245345, 'mean_word_length': 2.7152734129478318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6549048', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1817, 'n_words': 353}