<commit_before>'use strict';

/* global -Promise */
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var _ = require('underscore');
var esprima = require('esprima');
var caniuse = require('./caniuse.js');

var omittedTokenTypes = {
  'Boolean': false,
  'Identifier': false,
  'Keyword': false,
  'Null': false,
  'Numeric': false,
  'Punctuator': true,
  'String': true,
  'RegularExpression': false
};

// Uses Esprima to parse a JavaScript file into tokens
var parseFile = function (rawContents) {
  return esprima.parse(rawContents, {
    loc: true,
    range: false,
    raw: false,
    tokens: true,
    comment: false,
    attachComment: false
  }).tokens;
};

// Returns relevant tokens by their value
var sortTokens = function (tokens) {
  var sortedTokens = {};
  _.each(tokens, function (token) {
    // Omits tokens we don't care about
    if (omittedTokenTypes[token.type]) { return; }

    if (_.isUndefined(sortedTokens[token.value])) {
      sortedTokens[token.value] = [token];
    } else {
      sortedTokens[token.value].push(token);
    }
  });
  return sortedTokens;
};

// Remove tokens that are not found in the caniuse-db
var filterUnrecognizedTokens = function (tokens) {
  var keywords = caniuse.keywords;

  return _.reduce(keywords, function (usableTokens, keyword) {
    if (tokens[keyword]) {
      usableTokens[keyword] = tokens[keyword];
    }
    return usableTokens;
  }, {});
};

var getUsableTokens = function (filename) {
  return fs.readFileAsync(filename, 'utf-8')
    .then(parseFile)
    .then(sortTokens)<commit_msg>Remove useless comments<commit_after>'use strict';

/* global -Promise */
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var _ = require('underscore');
var esprima = require('esprima');
var caniuse = require('./caniuse.js');

var omittedTokenTypes = {
  'Boolean': false,
  'Identifier': false,
  'Keyword': false,
  'Null': false,
  'Numeric': false,
  'Punctuator': true,
  'String': true,
  'RegularExpression': false
};

var parseFile = function (rawContents) {
  return esprima.parse(rawContents, {
    loc: true,
    range: false,
    raw: false,
    tokens: true,
    comment: false,
    attachComment: false
  }).tokens;
};

var sortTokens = function (tokens) {
  var sortedTokens = {};
  _.each(tokens, function (token) {
    // Omits tokens we don't care about
    if (omittedTokenTypes[token.type]) { return; }

    if (_.isUndefined(sortedTokens[token.value])) {
      sortedTokens[token.value] = [token];
    } else {
      sortedTokens[token.value].push(token);
    }
  });
  return sortedTokens;
};

var filterUnrecognizedTokens = function (tokens) {
  var keywords = caniuse.keywords;

  return _.reduce(keywords, function (usableTokens, keyword) {
    if (tokens[keyword]) {
      usableTokens[keyword] = tokens[keyword];
    }
    return usableTokens;
  }, {});
};

var getUsableTokens = function (filename) {
  return fs.readFileAsync(filename, 'utf-8')
    .then(parseFile)
    .then(sortTokens)
{'fraction_non_alphanumeric': 0.14445541296479106, 'fraction_numerical': 0.0006581112207963146, 'mean_word_length': 3.7874015748031495, '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': '1911788', 'n_tokens_mistral': 980, 'n_tokens_neox': 984, 'n_words': 241}