How do you run eslint for only a specific rule or set of rules - command line only

Viewed 8689

I know you can define rules in an .eslintrc file, but what if I just want to run eslint and check for one specific rule?

E.g. $ eslint helpme.js --rule some-important-rule

5 Answers

I don't know if this is the best way, but I was able to get this working:

$ eslint helpme.js --no-eslintrc --env "es6" --env "node" --parser-options "{ecmaVersion: 2018}" --rule "{some-important-rule: error}"

Note: With this method (ignoring .eslintrc completeley) you still have to add some stuff from .eslintrc like your environment and parser options.

If you want to use your .eslintrc file to keep your configuration (parser, plugin settings, etc), you can use eslint-nibble with the --rule=some-important-rule flag. This will respect your normal configuration, but only show you errors from that rule. There are some other flags as well like --no-interactive if you want to run this in something like a CI environment.

Disclaimer: I'm the creator of eslint-nibble.

Simple way to see single rule output while still using .eslintrc is to use grep:

$ eslint helpme.js | egrep "(^/|some\-important\-rule$)"

Try ESLint custom formatter.

It can be used to filter part of rules, files you want to pay attention to.

And you don't need to :

  • Edit your ESLint config file.
  • Use complicate command.

DEMO for filter files contain error which rules id is prop-types:

// ./eslint-file-path-formatter.js
const fs = require('fs');

function containRules(result, targetRuleId) {
  if (!result || !targetRuleId) {
    return false;
  }

  return result.messages.some((cur) => {
    // console.log(`cur?.ruleId = ${cur?.ruleId}`);
    if (cur?.ruleId?.includes(targetRuleId)) {
      return true;
    }
  });
}

module.exports = function (results, context) {
  const summary = [];
  results.forEach((cur) => {
    if (containRules(cur, 'prop-types')) {
      summary.push(`'${cur.filePath}',`);
    }
  });

  // fs.writeFileSync('eslint-error-files.txt', summary.join('\n'));
  // return 'Done Write';

  return summary.join('\n');
};

Usage:

eslint . -f ./eslint-file-path-formatter.js

Then this formatter will print all files name to console.

You can also write result to local files, do whatever you want.

Expanding on @matrik answer, this doesn't require me to define all eslint config again and also shows the file name.

eslint helpme.js | egrep "react/jsx-no-literals" -B 1
Related