Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditorConfig vs. Eslint vs. Prettier: Is it worthwhile to use them all?

I am trying to set up some tools to help maintain consistency in a codebase used by multiple developers. Is it necessary to use EditorConfig, ESlint and Prettier all together? As far as I understand, EditorConfig is used to set coding styles/rules, ESlint is used to ensure code is formatted consistently by throwing warnings if code does not follow rules, and prettier is used to automatically format code based on the rules. However, I believe you can customize rules in prettier, which in turns does the job of EditorConfig. Is this true? What is the best combination of tools to use to maintain consistent code?

like image 371
PBandJen Avatar asked Jan 21 '18 04:01

PBandJen


People also ask

Should I use both ESLint and Prettier?

Benefits of using Prettier and ESLint As mentioned earlier, whereas Prettier takes care of your code formatting, ESLint takes care of your code style. The former does everything automatically for you. If you have set up Prettier, you can configure it to format your file on saving it.

Do I need EditorConfig with Prettier?

You can integrate EditorConfig in your CI toolchain as well so there is no need for Prettier.

Why You Should Use Prettier?

By far the biggest reason for adopting Prettier is to stop all the on-going debates over styles. It is generally accepted that having a common style guide is valuable for a project and team but getting there is a very painful and unrewarding process.

Does ESLint run Prettier?

Prettier can be run as a plugin for ESLint, which allows you to lint and format your code with a single command. Anything you can do to simplify your dev process is a win in my book.


1 Answers

In my experience, the best combination is all 3, and here's why:

EditorConfig: This helps your editor produce code that looks like your style guide as you go. While this isn't strictly necessary in order to achieve your goals, it's nice if you're always looking at code that follows the same coding styles. Otherwise if you don't have EditorConfig, as you're typing your editor will auto-format differently to the rest of the code base, which is confusing. Of course if you've set up prettier it'll fix it before it goes into your code base, but still, why would you want to look at it in one format while you're writing it and then have it switch when you go to commit? Might as well be consistent.

Prettier: Automatically formats your code. I like to set it up to do this when I stage my files for a commit, so that it's physically impossible for me to commit code that doesn't match my style guide.

ESLint: So why would you want a linter too? Because ESLint does more than just style. It picks up when you declare variables you don't use, or reference things that aren't defined, amongst a few other niceties. So while its role diminishes somewhat compared to the days before prettier, it's still useful to have in a project to catch the other errors.

Hope that helps!

like image 73
KevinBrownTech Avatar answered Oct 27 '22 00:10

KevinBrownTech