<commit_before>/*!
 * rethrow <https://github.com/jonschlinkert/rethrow>
 *
 * Copyright (c) 2015, <NAME>.
 * Licensed under the MIT License.
 */

'use strict';

var fs = require('fs');

/**
 * Re-throw the given `err` in context to the offending
 * template expression in `filename` at the given `lineno`.
 *
 * @param {Error} `err` Error object
 * @param {String} `filename` The file path of the template
 * @param {String} `lineno` The line number of the expression causing the error.
 * @param {String} `str` Template string
 * @api public
 */

function rethrow(err, filename, lineno, str) {
  if (!(err instanceof Error)) throw err;
  try {
    str = str || fs.readFileSync(filename, 'utf8');
  } catch (e) {
    rethrow(err, null, lineno);
  }

  lineno += 0;
  var lines = str.split('\n');
  var start = Math.max(lineno - 3, 0);
  var end = Math.min(lines.length, lineno + 3);

  // Error context
  var context = lines.slice(start, end).map(function (line, i) {
    var curr = i + start;
    return (curr == lineno ? '  > ' : '    ')
      + curr + '| '
      + line;
  }).join('\n');

  // Alter exception message
  err.path = filename;
  err.message = (filename || 'source') + ':'
    + lineno + '\n'
    + context + '\n\n'
    + err.message;
  throw err;
}

module.exports = rethrow;<commit_msg>improve formatting<commit_after>/*!
 * rethrow <https://github.com/jonschlinkert/rethrow>
 *
 * Copyright (c) 2015, <NAME>.
 * Licensed under the MIT License.
 */

'use strict';

var align = require('right-align');
var extend = require('extend-shallow');
var yellow = require('ansi-yellow');
var bgred = require('ansi-bgred');
var red = require('ansi-red');

/**
 * Re-throw the given `err` in context to the offending
 * template expression in `filename` at the given `lineno`.
 *
 * @param {Error} `err` Error object
 * @param {String} `filename` The file path of the template
 * @param {String} `lineno` The line number of the expression causing the error.
 * @param {String} `str` Template string
 * @api public
 */

function rethrow(options) {
  var opts = extend({before: 3, after: 3}, options);
  return function (err, filename, lineno, str, expr) {
    if (!(err instanceof Error)) throw err;

    lineno = lineno >> 0;
    var lines = str.split('\n');
    var before = Math.max(lineno - (+opts.before), 0);
    var after = Math.min(lines.length, lineno + (+opts.after));

    // align line numbers
    var n = align(count(lines.length + 1));

    lineno++;
    var pointer = errorMessage(err, opts);
    console.log(pointer)

    // Error context
    var context = lines.slice(before, after).map(function (line, i) {
      var num = i + before + 1;
      var msg = style(num, lineno);

      return msg('  > ', '    ')
        + n[num] + '| '
        + line + ' ' + msg(expr, '');
    }).join('\n');

    // Alter exception message
    err.path = filename;
    err.message = (filename || 'source') + ':'
      + lineno + '\n'
      + context + '\n\n'
      + (pointer ? (pointer + yellow(filename)) : styleMessage(err.message, opts)) + '\n';

    throw err.message;
  };
}

function style(curr, lineno) {
  return function(a, b) {
    return curr == lineno ? bgred(a || '') : (b || '');
  };
}

function styleMessage(msg, opts) {
  if (typeof opts.styleMessage === 'function') {
    return opts.styleMessage(msg);
  }
  return red(msg);
}

function count(n) {
  return Array.apply(null, {length: n}).map(Number.call, Number);
}

function errorMessage(err, opts) {
  if (opts.pointer === false) return '';
  var messageRe = /(?:(.+)is not defined|cannot read property)/i, m;
  var str = '';
  if (m = messageRe.exec(err.message)) {
    if (m[1]) {
      var prop = m[1].trim();
      str = 'variable `' + prop + '` is not defined';
    } else if (err.helper) {
      str = ' helper `' + err.helper.name
        + '` cannot resolve argument `'
        + err.helper.args[0] + '`';
    }
    str = red('Error: ' + str + ': ');
  }
  return str;
}

/**
 * Expose `rethrow`
 */

module.exports = rethrow;
{'fraction_non_alphanumeric': 0.17397806580259223, 'fraction_numerical': 0.005483549351944167, 'mean_word_length': 2.9189453125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 2, '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': '3445303', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1336, 'n_words': 391}