Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Created January 29, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swissmanu/76413b4de9b0d137ef42 to your computer and use it in GitHub Desktop.
Save swissmanu/76413b4de9b0d137ef42 to your computer and use it in GitHub Desktop.
An ESLint formatter which display statistics for errors and warnings, but outputs only detailed information about errors.
/**
* An ESLint formatter which shows only errors. Warnings are displayed only in form of an aggregated
* total number.
*
* Example Output: Only Warnings:
*
* >
* > ✖ 98 problems (0 errors, 98 warnings)
* >
*
* Example Output: Errors and Warnings:
*
* >
* > /files/index.js
* > 1:1 error Expected indentation of 1 tab character but found 0 indent
* >
* > ✖ 99 problems (1 error, 98 warnings)
* >
*/
var defaultFormatter = require('./node_modules/eslint/lib/formatters/stylish');
var chalk = require('chalk');
var crossIcon = '\u2716';
function pluralize(word, count) {
return (count === 1 ? word : word + "s");
}
module.exports = function(results) {
var output = '\n';
var errorResults = [];
var total = 0;
var numberOfWarnings = 0;
var numberOfErrors = 0;
var errorOutput;
if(results.length === 0) { return ''; }
results.forEach(function(result) {
var errorMessages;
if(result.fatal) {
errorResults.push(result);
return;
}
errorMessages = result.messages.filter(function(message) { return message.severity === 2; });
total += result.messages.length;
numberOfErrors += errorMessages.length;
numberOfWarnings += result.messages.length - errorMessages.length;
if(errorMessages.length > 0) {
errorResults.push(Object.assign({}, result, { messages: errorMessages }));
}
});
if(errorResults.length > 0) {
errorOutput = defaultFormatter(errorResults);
output += errorOutput.substr(0, errorOutput.lastIndexOf(crossIcon));
}
output += chalk[numberOfErrors > 0 ? 'red' : 'yellow'].bold([
crossIcon, ' ', total, pluralize(' problem', total),
' (', numberOfErrors, pluralize(' error', numberOfErrors), ', ',
numberOfWarnings, pluralize(' warning', numberOfWarnings), ')\n'
].join(''));
return output;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment