Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active July 31, 2023 17:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branneman/5ea56969966332ab528ab5ab2d1a5b4e to your computer and use it in GitHub Desktop.
Save branneman/5ea56969966332ab528ab5ab2d1a5b4e to your computer and use it in GitHub Desktop.
Group-By-Count ESLint errors
// :: (String, String) => String
const spawn = require('child_process').spawnSync;
// :: String => [String]
const getRules = raw => raw
.split('\n')
.map(line => line.trim())
.filter(line => !!line)
.filter(line => line[0] !== '/' && line[0] !== '✖')
.map(line => line.match(/[a-z-]+$/)[0]);
// :: [String] => Object
const groupByCount = list => {
const dict = {};
list.forEach(item => {
dict[item] = dict[item] || 0;
dict[item]++;
});
return dict;
};
// :: Object => [Object]
const sortByValue = dict => {
const arr = [];
for (const key in dict) {
if (!dict.hasOwnProperty(key)) continue;
arr.push([key, dict[key]]);
}
arr.sort((a, b) => a[1] < b[1] ? 1 : a[1] > b[1] ? -1 : 0);
return arr;
};
// :: String => [String]
const app = str => sortByValue(groupByCount(getRules(str)));
const output = spawn('eslint', ['.'], {encoding: 'utf8'}).stdout;
console.log(app(output));
eslint . | grep -v "^[\/✖]" | grep -v '^$' | perl -ne 's/(?:.+) ([\w-]+$)/$1/g; print;' | sort | uniq -c | sort
@andrekovac
Copy link

andrekovac commented Feb 1, 2019

count-lint-errors.sh does not identify the following lines as identical warnings (perhaps because of the quotes):

image

P.S. Very nice command though!!

@sakura2uuu
Copy link

it is because of the / in type of error.
Updated script:
eslint . | grep -v "^[\/✖]" | grep -v '^$' | perl -ne 's/(?:.+) ([\/\w-]+$)/$1/g; print;' | sort | uniq -c | sort

@macalinao
Copy link

If your package name has @ in it, use this instead:

 % cat linter-output.txt | grep -v "^[\/✖]"  | grep -v '^$' | perl -ne 's/(?:.+) ([@\/\w-]+$)/$1/g; print;' | sort | uniq -c | sort

@avasuro
Copy link

avasuro commented Mar 30, 2021

Very useful script, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment