Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier + Airbnb's ESLint config

Recently, I've started using Visual Studio Code for my editor and found the Prettier - JavaScript formatter. I think it's a great plugin because it helps me keep my code looking nice.

I set up Airbnb's ESLint config and have found that to be super helpful.

Here's the catch. The Airbnb ESLint config I'm currently running doesn't play nice with Prettier. For example, for JavaScript string, Prettier is formatted to include double ticks and Airbnb's ESLint like single ticks. When I format the code using Prettier, then Airbnb's ESLint doesn't agree.

I know Kent Dodds has done some work with ESLint configs, among others, example here.

But I can't seem to find a solution that lets me use the magic of Prettier to format my code to Airbnb's ESLint.

like image 631
Jiovan Melendez Avatar asked Sep 13 '17 15:09

Jiovan Melendez


People also ask

Can you use Prettier and ESLint together?

extends: [ // ... 'eslint:recommended', "prettier" // Make sure this is the last ], // ... } Now, you can run Prettier and ESLint together without any side effects. You can also run Prettier and ESLint one after another like on the command line by defining them as npm scripts.

Does Airbnb ESLint work on TypeScript?

Airbnb configuration doesn't support Typescript ESLint 3.0 yet, so I will install TypeScript ESLint 2.34. Typescript ESLint 2.34 doesn't support ESLint 7 yet, so I will install ESLint 6.


2 Answers

I think eslint-config-prettier was created just for this purpose: https://prettier.io/docs/en/eslint.html#turn-off-eslint-s-formatting-rules

Basically it turns off all rules that have to do with code styling because prettier will take care of it anyway.

So you just install this config along with any other desired eslint config (like eslint-config-airbnb) and in your eslint configuration file you just add it to the extends field. For example:

{   "extends": ["airbnb", "prettier"] } 
like image 199
timetowonder Avatar answered Oct 08 '22 01:10

timetowonder


Here are a few ways to do it, in order of recommendation. I would prefer the first approach as you won't ever have to bother with other rules that might conflict in future.

i) Install eslint-config-prettier and extend from it in .eslintrc. Doing this turns off some of the formatting-related rules within ESLint that might conflict with Prettier:

{   "extends": [     "airbnb",     "prettier"   ] } 

ii) Adding "singleQuote": true to the .prettierrc config file:

{   "singleQuote": true,   ... } 

iii) Adding a --single-quote command line option when you invoke Prettier:

$ prettier --single-quote ... 

iv) Turn off ESLint's quotes rule within your .eslintrc config file (and potentially others that might conflict):

{   "rules": {     "quotes": "off",     ...   } } 
like image 38
Yangshun Tay Avatar answered Oct 08 '22 01:10

Yangshun Tay