Is there a way to get the unique errors reported from ESLint

Viewed 865

I am refactoring am existing project with TypeScript and React. I have introduced ESLint and there are over 300 errors reported across the code.

This is quite a large task as these cannot be fixed automatically with ESLint itself.

Is there a way to get the distinct error from the ESLint CLI output then use that rule to get the files that throw the rule error so I can fix it rule-by-rule rather than file-by-file?

2 Answers

Eslint supports custom formatters. So you can do something along the lines:

Add file lint-formatter.js

module.exports = results => {
  const byRuleId = results.reduce(
    (map, current) => {
      current.messages.forEach(({ ruleId, line, column }) => {
        if (!map[ruleId]) {
          map[ruleId] = [];
        }

        const occurrence = `${current.filePath}:${line}:${column}`;
        map[ruleId].push(occurrence);
      });
      return map;
    }, {}
  );

  return Object.entries(byRuleId)
    .map(([ruleId, occurrences]) => `${ruleId} (total: ${occurrences.length})\n${occurrences.join('\n')}`)
    .join('\n########################\n');
};

The above example groups errors/warnings by rule id, but of course everything in your hands.

Then run linter with your custom formatter:

eslint -f ./lint-formatter.js

Sample output:

object-curly-spacing (total: 2)
foo/bar.js:2:8
foo/bar.js:3:13
########################
no-trailing-spaces (total: 1)
foo/bar.js:7:11
########################
object-curly-newline (total: 2)
foo/bar.js:14:8
foo/bar.js:15:31
########################
space-infix-ops (total: 1)
foo/bar.js:18:29

I had 100s of eslint errors from a open source project.
Spent 2 hours in trying to fix them.

Below worked:

  1. Install eslint-nibble along with any dependencies, I have installed as below:
npm i -g eslint
npm i -g eslint-config-standard
npm i -g eslint-plugin-import
npm i -g eslint-plugin-markdown
npm i -g eslint-plugin-n
npm i -g eslint-plugin-promise
npm i -g eslint-nibble
  1. Now see by running eslint & eslint-nibble:

Normal eslint produces 100s of mixed warning & errors

eslint --fix **/*.js

eslint-nibble will show all errors group by rule.
Also asks which ones we type of eslint errors we want to see:

eslint-nibble **/*.js
Related