Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint ignore specific rule for a specific directory

Tags:

eslint

Is it possible with ESLint to ignore one specific rule for an entire directory?

In my case, I would like to ignore import/prefer-default-export for a directory named commonComponents

like image 666
Kevin Amiranoff Avatar asked Dec 24 '16 20:12

Kevin Amiranoff


People also ask

How do I disable the ESLint rule in a folder?

0+ use "ignorePatterns": []. You can tell ESLint to ignore specific files and directories using ignorePatterns in your config files.

How do I disable ESLint for multiple lines?

For those converting eslint-disable-next-line to eslint-disable (for multiple lines), remember two things. 1. /* */ instead of // 2. It's eslint-disable and not eslint-disable-next-line . Just reiterating coz I did the same and had to search many more things due to the 2nd point.


2 Answers

ESLint configuration (.eslintrc) files are hierarchical:

ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.

You can disable the import/prefer-default-export rule for the commonComponents directory by creating a .eslintrc file with the following content in that directory:

{     "rules": {         "import/prefer-default-export": "off"     } } 
like image 171
cartant Avatar answered Sep 29 '22 11:09

cartant


You can also use the "overrides" key to declare rules for different glob patterns.

Have a read of Configuration Based on Glob Patterns

Sometimes a more fine-controlled configuration is necessary, for example if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the overrides key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., app/**/*.test.js).

I use this to remove the no-unused-expressions rule from my test files like so;

"overrides": [{   "files": [ "*.spec.js" ],   "rules": {     "no-unused-expressions": 0   } }] 
like image 33
Patrick Avatar answered Sep 29 '22 10:09

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!