使⽤Prettier格式化代码
漂亮的⼊门 (Introduction to Prettier)
Prettier is an opinionated code formatter.
漂亮的是⼀个⾃以为是的代码格式化程序。
It supports a lot of different syntax out of the box, including:
它⽀持许多不同的现成语法,包括:
JavaScript
JavaScript
Flow,
流,
CSS, SCSS, Less
CSS,SCSS,更少
JSON
JSON格式
Markdown
降价促销
and with you can use it for Python, PHP, Swift, Ruby, Java and more.
并且带有您可以将其⽤于Python,PHP,Swift,Ruby,Java等。
It integrates with the most popular code editors, including VS Code, Sublime Text, Atom and more.它与最流⾏的代码编辑器集成在⼀起,包括VS Code,Sublime Text,Atom等。
Prettier is hugely popular, as in February 2018 it has been downloaded over 3.5 million times.
Prettier⾮常受欢迎,截⾄2018年2⽉,它已被下载超过350万次。
The most important links you need to know more about Prettier are
您需要进⼀步了解Prettier的最重要链接是
更少的选择 (Less options)
I learned Go recently and one of the best things about Go is gofmt, an official tool that automatically formats your code according to common standards.
我最近学习了Go,Go的最好的功能之⼀就是gofmt ,这是⼀个官⽅⼯具,可以根据通⽤标准⾃动格式化代码。
95% (made up stat) of the Go code around looks exactly the same, because this tool can be easily enforced and since the style is defined for you by the Go maintainers, you are much more likely to adapt to that standard instead of insisting on your own style. Like tabs vs spaces, or where to put an opening bracket.
95%左右的Go代码看起来完全⼀样,因为此⼯具可以轻松实施,并且由于Go维护⼈员为您定义了样式,因此您更有可能适应该标准,⽽不必坚持根据⾃⼰的风格。 如制表符与空格,或在何处放置左括号。
This might sound like a limitation, but it’s actually very powerful. All Go code looks the same.
这听起来像⼀个限制,但实际上⾮常强⼤。 所有Go代码看起来都⼀样。
Prettier is the gofmt for the rest of the world.
漂亮的gofmt是世界其他地⽅的东西 。
It has very few options, and most of the decisions are already taken for you so you can stop arguing about style and little things, and focus on your code.
visual studio和vs code的区别
它只有很少的选择,并且⼤多数决定已经为您做出,因此您可以停⽌争论样式和⼩事,⽽专注于代码。
与ESLint的区别 (Difference with ESLint)
is a linter, it does not just format, but it also highlights some errors thanks to its static analysis of the code.
是⼀个 ,不仅格式化,⽽且由于对代码进⾏静态分析,因此还突出了⼀些错误。
It is an invaluable tool and it can be used alongside Prettier.
它是⼀种宝贵的⼯具,可以与Prettier⼀起使⽤。
ESLint also highlights formatting issues, but since it’s a lot more configurable, everyone could have a different set of formatting rules. Prettier provides a common ground for all.
ESLint还强调了格式设置问题,但由于它的可配置性要强得多,因此每个⼈都可能有不同的格式设置规则集。 漂亮的东西为所有⼈提供了共同点。
Now, there are a few things you can customize, like:
现在,您可以⾃定义⼀些内容,例如:
the tab width
标签宽度
the use of single quotes vs double quotes
单引号与双引号的使⽤
the line columns number
⾏列号
the use of trailing commas
尾随逗号的使⽤
and some others, but Prettier tries to keep the number of those customizations under control, to avoid becoming too customizable.
以及其他⼀些,但是Prettier试图控制这些⾃定义项的数量,以避免过于⾃定义。
安装 (Installation)
Prettier can run from the command line, and you can install it using or .
Prettier可以从命令⾏运⾏,您可以使⽤或进⾏安装。
Another great use case for Prettier is to run it on PRs for your repositories, for example on .
Prettier的另⼀个很好⽤例是在您的存储库的PR上运⾏它,例如在 。
If you use a supported editor the best thing is to use Prettier directly from the editor, and the Prettier formatting will be run every time you save.
如果使⽤受⽀持的编辑器,最好的做法是直接从编辑器使⽤Prettier,并且每次保存时都会运⾏Prettier格式。
例如,这是VS Code的Prettier扩展名: ://marketplace.visualstudio/items?itemName=esbenp.prettier-vscode
适合初学者 (Prettier for beginners)
If you think Prettier is just for teams, or for pro users, you are missing a good value proposition of this tool.
如果您认为Prettier仅适⽤于团队或专业⽤户,那么您会错过此⼯具的价值主张。
A good style enforces good habits.
好的风格会养成良好的习惯。
Formatting is a topic that’s mostly overlooked by beginners, but having clean and consistent formatting is key to your success as a new developer.
格式化是⼀个初学者通常忽略的主题,但是拥有清晰⼀致的格式化是成功成为新开发⼈员的关键。
Also, even if you started using 2 weeks ago, with Prettier your code - style wise - will look just like code written from a JavaScript Guru writing JS since 1998.
同样,即使您在两周前开始使⽤ ,使⽤Prettier时,您的代码(在样式⽅⾯也是如此)看起来就像是⾃1998年以来由JavaScript Guru编写JS的代码⼀样。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论