All files / lib/formatters text.js

100% Statements 22/22
100% Branches 12/12
100% Functions 5/5
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    2x   12x   2x 10x     9x     10x 10x   10x 9x 9x   9x 9x     10x 10x 4x 4x 4x     4x     10x        
'use strict';
 
const colors = require('colors/safe');
 
const pluralize = (word, size) => `${size} ${(size === 0 || size > 1 ? `${word}s` : word)}`;
 
module.exports = function TextFormatter() {
  return {
    lines: [],
    line: function(level, ruleName, err) {
      this.lines.push({ level, ruleName, err });
    },
    result: function() {
      let warnings = 0;
      let errors = 0;
 
      const output = this.lines.map(({ level, ruleName, err }) => {
        if (level === 'error') { errors++; }
        if (level === 'warning') { warnings++; }
 
        const colorLevel = level === 'error' ? colors.red('error') : colors.yellow('warning');
        return `  [${colorLevel}]\t${colors.grey(ruleName)}\t${err.message}`;
      });
 
      const problems = warnings + errors;
      if (problems > 0) {
        output.unshift('Gint found some issues:');
        output.push('');
        output.push(colors.red.bold(
          ` ✖ ${pluralize('problem', problems)} (${pluralize('error', errors)}, ${pluralize('warning', warnings)})`
        ));
        output.push('');
      }
 
      return output.join('\n');
    },
  };
};