Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to programmatically suppress all ESLint errors on a line by line basis?

If I add any given ESLint rule, for example no-param-reassign in an existing codebase I'm likely to get many violations.

Is there a good way to programmatically add on a line by line bases suppressions to all existing violations?

In the example case:

// eslint-diable-next-line no-param-reassign
param = foo;

To clarify

I do want the rule in my project, guarding all new code we write. I don't want to fix all the old code that is emitting violations by hand (I want a script to do that for me or eslint itself if possible). This is why I would like to suppress all existing violations but respect all new violations. My main goal is to comply with the new rule as fast as possible to get the value from it on all new code. I don't mind old lingering suppressed violations.

like image 596
Red Mercury Avatar asked Mar 11 '20 03:03

Red Mercury


People also ask

How do I ignore all ESLint errors?

Ignore multiple files or folders To turn off ESLint in the whole file, you can add /* eslint-disable */ in the first line of that file. Alternatively, you can create a file . eslintignore in the root catalog.

How do I automatically fix ESLint errors?

You can set up ESLint to run auto-fix every time you press CTRL+S (or COMMAND+S ). For ESLint to work correctly on file same, you must change the Visual Studio Code preferences. Go to File > Preferences > Settings (or Code > Preferences > Settings).

How do I disable ESLint for a specific line?

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment. On a single line: const message = 'foo'; console. log(message); // eslint-disable-line no-console // eslint-disable-next-line no-console console.


3 Answers

I was having this same issue with a bunch of react-hooks/exhaustive-deps lint warnings ended up using the json formatter and a script to insert the eslint disable comment. I ran yarn lint . -f json -o warnings.json to get the json list of lints and then this

const json = require('./warnings.json');
const fs = require('fs');

json.forEach(({ filePath, messages, source }) => {
  // if there is no source we have nothing that needs to be eslint-ignore'd
  if (!source) {
    return;
  }

  const data = source.split('\n');

  // if the source has multiple lines which need to be eslint-ignored our offset changes per addition
  // offset is 1 because line numbers start at 1 but index numbers in an array start at 0
  let offset = 1;

  // group errors/warnings by line because we want to have one eslint disable comment with all the rules to disable
  const groupedMessages = messages.reduce((acc, next) => {
    const prevMessages = acc[next.line] ? acc[next.line] : [];
    // some lines may have the same rule twice
    const duplicateRuleForLine = prevMessages.find(message => message.ruleId === next.ruleId);
    // ignore jsx and graphql lint rules
    const applicableRule = next.ruleId && !next.ruleId.includes('jsx') && !next.ruleId.includes('graphql');

    // ignore the eslint-ignore addition for duplicates and non applicable rules
    if (duplicateRuleForLine || !applicableRule) {
      return acc;
    }

    return {
      ...acc,
      [next.line]: [...prevMessages, next],
    };
  }, {});

  Object.entries(groupedMessages).forEach(([line, messages]) => {
    // grouped ignores
    const ignore = `// eslint-disable-next-line ${messages.map(({ ruleId }) => ruleId).join(' ')}`;
    data.splice(line - offset, 0, ignore);
    offset--;
  });

  const updated = data.join('\n');

  fs.writeFile(filePath, updated, function(err) {
    if (err) return console.log(err);
  });
});

Worked out well for me, although I wish these comments I inserted were auto-formatted.

like image 119
Jonathan Portorreal Avatar answered Sep 20 '22 08:09

Jonathan Portorreal


I use this typically: https://github.com/amanda-mitchell/suppress-eslint-errors

Getting jscodemod configured for your repo is a bit tough, but worth it once you get it working. A lot of powerful codemods out there.

like image 20
user15185791 Avatar answered Sep 22 '22 08:09

user15185791


I tried suppress-eslint-errors npm answered by @user15185791, but it did not work with ESLint8. But, I was able to find https://github.com/mizdra/eslint-interactive from that npm issue. So far this wonderful npm is working perfectly.

The execution is as follows command.

yarn add -D eslint-interactive
yarn eslint-interactive src
yarn remove eslint-interactive
like image 26
katsusuke Avatar answered Sep 21 '22 08:09

katsusuke