Skip to content

Instantly share code, notes, and snippets.

@bjacobel
Created July 22, 2016 19:27
Show Gist options
  • Save bjacobel/e7f213b2687ea24507f8ac7bdabbc9df to your computer and use it in GitHub Desktop.
Save bjacobel/e7f213b2687ea24507f8ac7bdabbc9df to your computer and use it in GitHub Desktop.
Fail ESLint if number of violations exceeds threshold
eslint --format eslint-formatter-threshold.js .
var thresholds = require('./eslint-thresholds.json');
module.exports = function(results) {
'use strict';
var frequency = {},
thresholdViolations = [],
allViols = 'total';
results.forEach(function(file) {
file.messages.forEach(function(err) {
var rule = err.ruleId;
if (frequency[rule]) {
frequency[rule] = frequency[rule] + 1;
} else {
frequency[rule] = 1;
}
if (frequency[allViols]) {
frequency[allViols] = frequency[allViols] + 1;
} else {
frequency[allViols] = 1;
}
});
});
// To recount current error numbers for the thresholds file, uncomment the following line:
// console.log(JSON.stringify(frequency, null, 4));
Object.keys(frequency).forEach(function(error) {
// If a threshold for this error isn't listed in eslint-thresholds.json, assume it is 0
var errorThreshold = 0 | thresholds[error];
if (frequency[error] > errorThreshold) {
thresholdViolations.push(
'Too many ' + error + ' violations (found ' + frequency[error] + ', max ' + errorThreshold + ')'
);
}
});
if (thresholdViolations.length > 0) {
thresholdViolations.sort(function(a, b) {
if (a.substr(9, 5) === 'total') {
return 1;
} else {
return a <= b ? -1 : 1;
}
});
thresholdViolations.forEach(function(line) {
console.log(line); // eslint-disable-line no-console
});
process.exit(1);
} else {
process.exit(0);
}
};
{
"quote-props": 2947,
"total": 28543,
"max-len": 1227,
"no-underscore-dangle": 835,
"no-undef": 2822,
"no-restricted-syntax": 83,
"no-param-reassign": 935,
"wrap-iife": 205,
"no-use-before-define": 688,
"strict": 411,
"global-require": 9,
"consistent-return": 204,
"no-return-assign": 111,
"no-unused-expressions": 44,
"dot-notation": 332,
"no-unused-vars": 658,
"no-sequences": 26,
"camelcase": 2421,
"no-floating-decimal": 7,
"indent": 3723,
"brace-style": 254,
"newline-per-chained-call": 97,
"no-console": 143,
"vars-on-top": 1974,
"no-extra-bind": 3,
"no-shadow": 266,
"no-alert": 59,
"eqeqeq": 456,
"no-unreachable": 1,
"curly": 209,
"radix": 43,
"no-script-url": 2,
"one-var-declaration-per-line": 78,
"no-mixed-spaces-and-tabs": 4354,
"no-new": 21,
"no-useless-escape": 102,
"default-case": 37,
"no-loop-func": 9,
"no-unneeded-ternary": 26,
"no-cond-assign": 34,
"no-throw-literal": 44,
"no-redeclare": 131,
"guard-for-in": 24,
"no-empty": 6,
"null": 4,
"no-native-reassign": 2,
"block-scoped-var": 2321,
"no-array-constructor": 1,
"no-shadow-restricted-names": 72,
"no-nested-ternary": 6,
"no-constant-condition": 8,
"no-with": 1,
"no-useless-concat": 29,
"no-inner-declarations": 3,
"no-dupe-keys": 2,
"yoda": 8,
"no-irregular-whitespace": 6,
"no-multi-spaces": 6,
"import/no-unresolved": 1,
"no-new-wrappers": 1,
"no-extend-native": 4,
"no-multi-str": 2,
"no-ex-assign": 1,
"array-callback-return": 1,
"no-regex-spaces": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment