46

I want to use eslint command flag --fix to just fix one rule. And I tried the command: eslint --fix --config ./.eslintrcsomerules.js .. But it does not work. How to achieve the object?

My .eslintrcsomerules.js file is below:

module.exports = {
  'rules': {
    'indent': [2, 2],
  }
};
Naresh Kumar
  • 1,706
  • 1
  • 14
  • 26
soarinblue
  • 1,517
  • 3
  • 21
  • 30

4 Answers4

21

To fix the issues caused by just one rule, you need to combine --fix --rule with --no-eslintrc. Otherwise your rule will just be merged with your existing configuration and all fixable issues will be corrected. E.g.

$ eslint --no-eslintrc --fix --rule 'indent: [2, 2]'

Depending on your setup you'll need to re-add mandatory settings from your ESLint configuration to the command line for the lint run to succeed. In my case I had to use

$ eslint --no-eslintrc --parser babel-eslint --fix --rule 'indent: [2, 2]'
Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
  • More mandatory settings you might need to add can be found here: https://eslint.org/docs/latest/use/command-line-interface – hb20007 Jul 27 '23 at 18:09
  • In my case, it was `eslint --no-eslintrc --parser babel-eslint --plugin react --parser-options ecmaVersion:12 --parser-options sourceType:module --parser-options ecmaFeatures:{jsx:true} --fix --rule 'indent: [2, 2]'` – hb20007 Jul 27 '23 at 18:15
17

I struggled with this. I was using create-react-app and it was very difficult to turn off all the rules but keep all the configs to parse ES6/TS/etc.

This library was exactly what I wanted! I can see how many errors there are for each rule and selectively fix one rule at a time
https://github.com/IanVS/eslint-nibble

Nick Graham
  • 575
  • 5
  • 12
  • 9
    I'm the author of eslint-nibble, I'm glad you find it useful! Under the hood, it uses https://github.com/IanVS/eslint-filtered-fix, which can also be used directly if you're just looking for a simple CLI tool. – IanVS Jan 07 '21 at 02:23
14

You can pass a --rule flag with the rule you want to fix. For example:

eslint --fix --rule 'quotes: [2, double]' .

Check out the official documentation for more information.

The list of available rules might also be helpful.

Lucas Caton
  • 3,027
  • 1
  • 24
  • 34
  • 1
    Another example: `yarn lint --fix --rule '@typescript-eslint/member-delimiter-style: error'` – gvlax Apr 30 '20 at 17:15
  • 1
    `npm run lint --fix --rule '"arrow-parens": ["warn", "as-needed"]` is not working , any idea, @Lucas Caton – Achy97 Aug 10 '20 at 10:21
1

The answers suggesting ignoring .eslintrc with

eslint --no-eslintrc --fix --rule 'indent: [2, 2]'

may fail if your .eslintrc contains preprocessors without which you get errors, such as

Parsing error: The keyword 'import' is reserved

I recommend using eslint-interactive, which will let you select the rules you want to fix:

yarn install -D eslint-interactive
eslint-interactive . # This will interactively guide you.
yarn remove -D eslint-interactive
Jozef Mikušinec
  • 193
  • 2
  • 10