Use Prettier to format code

Beautiful is a self-righteous code formatter. This is a great way to maintain a consistent code format for you and your team, and supports many different languages out of the box.

Beautiful entry

Beautiful is a self-righteous code formatter.

Prettier logo

It supports many different ready-made syntaxes, including:

versusPlug-inYou can use it for Python, PHP, Swift, Ruby, Java, etc.

It is integrated with the most popular code editors including VS Code, Sublime Text, Atom, etc.

Prettier is very popular. As of February 2018, Prettier has been downloaded more than 3.5 million times.

The most important link you need to learn more about Prettier is

Fewer options

I recently learned Go, one of the best things about Go isgofmt, This is an official tool that can automatically set the code format according to common standards.

Around 95% of Go code looks exactly the same because this tool can be easily implemented, and because Go maintainers define the style for you, you are more likely to adapt to the standard without having to stick to your own style. Such as tabs and spaces, or where to put the opening parenthesis.

This sounds like a limitation, but it is actually very powerful. All Go code looks the same.

What's more beautiful isgofmtFor the rest of the world.

It has very few options, andMost decisions have been made for youSo you can stop arguing about styles and trivial matters and focus on the code.

Difference from ESLint

ESLintIt is a lint, it is not only formatted, but also highlights some errors due to the static analysis of the code.

It is a valuable tool that can be used with Prettier.

ESLint also emphasizes formatting issues, but because it is much more configurable, everyone can use a different set of formatting rules. Beautiful things provide common ground for everyone.

Now you can customize some content, such as:

  • Label width
  • Use of single and double quotes
  • Row number
  • Use of trailing commas

And some others, but Prettier tries to keep the number of these customization items within a controllable range to avoid over customization.

installation

The beautiful thing can be run from the command line, you can install it with the following commandyarnornpm.

Another great use case for Prettier is to run for you on PRGitRepository, for exampleGitHub.

If you use a supported editor, the best practice is to use Prettier directly from the editor and run the Prettier format every time you save.

For example, this is the Prettier extension for VS Code:https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

Suitable for beginners

If you think Prettier is only suitable for teams or professional users, then you will miss the value proposition of this tool.

Good style will develop good habits.

Formatting is a topic that beginners usually ignore, but having a clear and consistent formatting is the key to success as a new developer.

Also, even if you start usingJavaScript2 weeks ago, with Prettier, your code-in terms of style-looks like code written in JS by JavaScript Guru since 1998.


More devtools tutorials: