all files / src/formatters/ simple-detail.js

19.05% Statements 8/42
0% Branches 0/26
0% Functions 0/8
19.05% Lines 8/42
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                                 
// Template Author Sindre Sorhus @eslint
// https://github.com/sindresorhus/eslint-stylish
'use strict';
var chalk = require('chalk');
var table = require('text-table');
var c = require('./helpers/characters');
 
var tableSettings = {
  align: ['', '', 'r'],
  stringLength: function (str) {
    return chalk.stripColor(str).length;
  }
};
 
function pluralize(word, count) {
  return (count === 1 ? word : word + 's');
}
 
function simpleDetail(results) {
  var totalErrors = 0;
  var totalWarnings = 0;
  var output = '';
  var cleanMsg = '';
  var messageTime = chalk.gray('(' + new Date().toLocaleTimeString() + ')');
 
  results.forEach(function (result) {
    var messages = result.messages;
    var warnings = 0;
    var errors = 0;
    if (!messages.length) {
      return;
    }
 
    var tableText = table(
      messages.map(function (message) {
        function getMessageType(msg) {
          if (msg.fatal || msg.severity === 2) {
            totalErrors++;
            errors++;
            return chalk.red(c.x);
          }
 
          totalWarnings++;
          warnings++;
          return chalk.yellow(c.ex);
        }
 
        return ['',
          getMessageType(message),
          message.line || 0,
          message.column || 0,
          chalk.dim(message.message.replace(/\.$/, '')),
          chalk.gray(message.ruleId || '')];
      }), tableSettings);
 
    output += chalk.white.underline(result.filePath) + ' (' + chalk.red(errors) + '/' + chalk.yellow(warnings) + ')' + c.endLine;
    output += tableText.split(c.endLine).map(function (el) {
      return el.replace(/(\d+)\s+(\d+)/, function (m, p1, p2) {
        return chalk.gray(p1 + ':' + p2);
      });
    }).join(c.endLine) + c.endLine + c.endLine;
  });
 
  if(totalErrors) {
    output += chalk.red(c.x + ' ' + totalErrors + ' ' + pluralize('error', totalErrors)) + ' ';
  }
  if (totalWarnings) {
    output += chalk.yellow(c.ex + ' ' + totalWarnings + ' ' + pluralize('warning', totalWarnings)) + ' ';
  }
 
  if(results.length > 1 || results.length === 0) {
    cleanMsg = chalk.green(c.check + ' Clean') + ' ' + messageTime + c.endLine;
  }
 
  output = (totalErrors || totalWarnings) ? output + messageTime + c.endLine : cleanMsg;
 
  return output;
}
 
module.exports = simpleDetail;