lib/cli.js
function logError(e) {
  var msg = typeof e === 'string' ? e : e.message;

  // esprima.parse errors are in the format 'Line 123: Unexpected token &'
  // we support both formats since users might replace the parser
  if ((/Line \d+:/).test(msg)) {
    // convert into "Error: <filepath>:<line> <error_message>"
    msg = 'Error: ' + msg.replace(/[^\d]+(\d+): (.+)/, e.file + ':$1 $2');
  } else {
    // babylon.parse errors are in the format 'Unexpected token (0:4)'
    var m = (/^([^\(]+)\s\((\d+):(\d+)\)/).exec(msg);
    if (m) {
      // convert into "Error: <filepath>:<line>:<char> <error_message>"
      msg = 'Error: ' + e.file + ':' + m[2] + ':' + m[3] + ' ' + m[1];
    } else if (msg.indexOf('Error:') < 0) {
      msg = 'Error: ' + (e.file ? e.file + ' ' : '') + msg;
    }
  }

  // set the error flag to true to use an exit code !== 0
  exports.exitCode = 1;

  // we don't call console.error directly to make it possible to mock during
  // unit tests
  exports.stderr.write(msg + '\n');

  if (e.stack) {
    exports.stderr.write(e.stack + '\n');
  }
}
function logError(e) {
  var msg = typeof e === 'string' ? e : e.message;

  // esprima.parse errors are in the format 'Line 123: Unexpected token &'
  // we support both formats since users might replace the parser
  if ((/Line \d+:/).test(msg)) {
    // convert into "Error: <filepath>:<line> <error_message>"
    msg = 'Error: ' + msg.replace(/[^\d]+(\d+): (.+)/, e.file + ':$1 $2');
  } else {
    // babylon.parse errors are in the format 'Unexpected token (0:4)'
    var m = (/^([^\(]+)\s\((\d+):(\d+)\)/).exec(msg);
    if (m) {
      // convert into "Error: <filepath>:<line>:<char> <error_message>"
      msg = 'Error: ' + e.file + ':' + m[2] + ':' + m[3] + ' ' + m[1];
    } else if (msg.indexOf('Error:') < 0) {
      msg = 'Error: ' + (e.file ? e.file + ' ' : '') + msg;
    }
  }

  // set the error flag to true to use an exit code !== 0
  exports.exitCode = 1;

  // we don't call console.error directly to make it possible to mock during
  // unit tests
  exports.stderr.write(msg + '\n');

  if (e.stack) {
    exports.stderr.write(e.stack + '\n');
  }
}
function logError ( e ) { var msg = typeof e === 'string' ? e : e . message ; // esprima.parse errors are in the format 'Line 123: Unexpected token &' // we support both formats since users might replace the parser if ( ( / Line \d+: / ) . test ( msg ) ) { // convert into "Error: <filepath>:<line> <error_message>" msg = 'Error: ' + msg . replace ( / [^\d]+(\d+): (.+) / , e . file + ':$1 $2' ) ; } else { // babylon.parse errors are in the format 'Unexpected token (0:4)' var m = ( / ^([^\(]+)\s\((\d+):(\d+)\) / ) . exec ( msg ) ; if ( m ) { // convert into "Error: <filepath>:<line>:<char> <error_message>" msg = 'Error: ' + e . file + ':' + m [ 2 ] + ':' + m [ 3 ] + ' ' + m [ 1 ] ; } else if ( msg . indexOf ( 'Error:' ) < 0 ) { msg = 'Error: ' + ( e . file ? e . file + ' ' : '' ) + msg ; } } // set the error flag to true to use an exit code !== 0 exports . exitCode = 1 ; // we don't call console.error directly to make it possible to mock during // unit tests exports . stderr . write ( msg + '\n' ) ; if ( e . stack ) { exports . stderr . write ( e . stack + '\n' ) ; } }
we are handling errors this way instead of prematurely terminating the program because user might be editing multiple files at once and error might only be present on a single file
we are handling errors this way instead of prematurely terminating the program because user might be editing multiple files at once and error might only be present on a single file