EditorConfig vs. Eslint vs. Prettier:值得全部使用吗? [英] EditorConfig vs. Eslint vs. Prettier: Is it worthwhile to use them all?

查看:34
本文介绍了EditorConfig vs. Eslint vs. Prettier:值得全部使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一些工具来帮助保持多个开发人员使用的代码库的一致性.有必要同时使用 EditorConfig、ESlint 和 Prettier 吗?据我了解,EditorConfig 用于设置编码样式/规则,ESlint 用于确保代码格式一致,如果代码不遵循规则则抛出警告,prettier 用于根据规则自动格式化代码.但是,我相信您可以在 prettier 中自定义规则,这反过来完成了 EditorConfig 的工作.这是真的?用于保持代码一致性的最佳工具组合是什么?

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?

推荐答案

根据我的经验,最好的组合是全部 3,原因如下:

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

EditorConfig:这有助于您的编辑器生成看起来像您的风格指南的代码.虽然这对于实现您的目标并不是绝对必要的,但如果您总是查看遵循相同编码风格的代码,那就太好了.否则,如果您没有 EditorConfig,当您键入时,您的编辑器的自动格式将与代码库的其余部分不同,这会令人困惑.当然,如果你设置了更漂亮的,它会在它进入你的代码库之前修复它,但是,你为什么要在编写它时以一种格式查看它,然后在你去的时候让它切换承诺?最好保持一致.

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:那你为什么还要一个 linter?因为 ESLint 不仅仅是风格.当您声明不使用的变量或引用未定义的内容以及其他一些细节时,它就会出现.因此,虽然它的作用与以前更漂亮的日子相比有所减少,但在项目中捕获其他错误仍然很有用.

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.

希望有帮助!

这篇关于EditorConfig vs. Eslint vs. Prettier:值得全部使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆