Code coverage report for src/formatters/stylish.js

Statements: 50% (17 / 34)      Branches: 17.86% (5 / 28)      Functions: 16.67% (1 / 6)      Lines: 50% (17 / 34)      Ignored: none     

All files » src/formatters/ » stylish.js
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96          1           1                   1 1 1   1   1       1         1                                           1   1   1   1 1         1 1                                             1  
'use strict';
 
/**
 * @type {Chalk.ChalkModule}
 */
var chalk = require('chalk');
 
/**
 * @param {MESSAGE} message
 * @return {string}
 */
function getMessageType(message) {
    if (message.level === 'WARN') {
        return chalk.yellow('warning');
    }
    if (message.level === 'ERROR') {
        return chalk.red('error');
    }
    return chalk.cyan('info');
}
 
module.exports = function (warnings/*, config*/) {
    var _ = require('lodash');
    var table = require('text-table');
    //var verbosity = false;
    var UNICODE_HEAVY_MULTIPLICATION_X = '\u2716';
 
    function pluralize(n, single, plural) {
        return n === 1 ? single : plural;
    }
 
    function lineText(line) {
        return line < 1 ? '' : line;
    }
 
    // context.report(JSON.stringify(warnings, undefined, 2));
    var output = table(
        warnings.map(function (message) {
            return [
                '',
                message.file || '',
                lineText(message.line || 0),
                lineText(message.column || 0),
                getMessageType(message),
                // message.message.replace(/\.$/, ""),
                message.msg || ''
                // chalk.gray(message.ruleId)
            ];
        }),
        {
            align: ['', 'r', 'l'],
            stringLength: function (str) {
                return chalk.stripColor(str).length;
            }
        }
        //}
    );
 
    var buf = [];
 
    buf.push(output);
 
    var grouped = _.groupBy(warnings, 'level');
 
    var errCount = grouped.ERROR ? grouped.ERROR.length : 0;
    var warnCount = grouped.WARN ? grouped.WARN.length : 0;
    //var infoCount = grouped.INFO ? grouped.INFO.length : 0;
 
//    buf.push(errCount + ' ' + warnCount + ' ' + infoCount + '\n');
 
    Eif (errCount === 0 && warnCount === 0) {
        buf.push(chalk.green('React templates done'));
    } else {
        var msg = [];
        if (errCount > 0) {
            msg.push(errCount + ' ' + pluralize(errCount, 'error', 'errors'));
        } else {
            msg.push(warnCount + ' ' + pluralize(warnCount, 'warning', 'warnings'));
        }
        buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + msg.join(', ')));
        if (errCount > 0) {
            buf.push(chalk.red('React templates failed due to errors'));
        } else {
            buf.push(chalk.yellow('React templates done with warnings'));
        }
    }
 
//                context.report(JSON.stringify(grouped, undefined, 2));
//    if (grouped.ERROR && grouped.ERROR.length > 0) {
////        throw new Error(errorMessages.VERIFY_FAILED.format(grouped.ERROR.length, pluralize(grouped.ERROR.length, 'error', 'errors')));
//    } else {
//        buf.push(chalk.red.bold(UNICODE_HEAVY_MULTIPLICATION_X + ' ' + warnings.length + ' ' + pluralize(warnings.length, 'problem', 'problems')) + '\n');
//        buf.push('React templates done with warnings\n');
//    }
    return buf.join('\n');
};