Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix just one rule using eslint

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],   } }; 
like image 817
soarinblue Avatar asked Dec 15 '17 08:12

soarinblue


People also ask

How do you set rules in ESLint?

ESLint comes with a large number of built-in rules and you can add more rules through plugins. You can modify which rules your project uses either using configuration comments or configuration files. To change a rule setting, you must set the rule ID equal to one of these values: "off" or 0 - turn the rule off.

How do I disable a specific ESLint rule?

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.

How do I automatically fix ESLint errors?

If you have the ESLint extension installed you can use CTRL+SHIFT+P to open the Command Palette. Then search for ESLint: Fix all auto-fixable Problems and press ENTER (or RETURN ).

Can ESLint fix break code?

While ESLint (when running with --fix ) tries hard not to break your code functionally, any time you're manipulating your code in the time between writing it and running it you are putting yourself at risk of changing the behavior of your codebase.


2 Answers

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]' 
like image 183
Stefan Becker Avatar answered Oct 01 '22 03:10

Stefan Becker


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.

like image 32
Lucas Caton Avatar answered Oct 01 '22 04:10

Lucas Caton