与 Linters 集成
Linter 通常不仅包含代码质量规则,还包含文体规则。使用 Prettier 时,大多数风格规则都是不必要的,但更糟糕的是 - 它们可能与 Prettier 发生冲突!如 Prettier 与 Linter 中所述,使用 Prettier 来解决代码格式问题,并使用 linters 来解决代码质量问题。
¥Linters usually contain not only code quality rules, but also stylistic rules. Most stylistic rules are unnecessary when using Prettier, but worse – they might conflict with Prettier! Use Prettier for code formatting concerns, and linters for code-quality concerns, as outlined in Prettier vs. Linters.
幸运的是,通过使用这些预制配置,很容易关闭与 Prettier 冲突或不必要的规则:
¥Luckily it’s easy to turn off rules that conflict or are unnecessary with Prettier, by using these pre-made configs:
查看上面的链接,了解有关如何安装和设置的说明。
¥Check out the above links for instructions on how to install and set things up.
注意
¥Notes
在 Internet 上同时搜索 Prettier 和你的 linter 时,你可能会找到更多相关项目。通常不建议使用这些方法,但在某些情况下可能很有用。
¥When searching for both Prettier and your linter on the Internet you’ll probably find more related projects. These are generally not recommended, but can be useful in certain circumstances.
首先,我们有插件可以让你像运行 linter 规则一样运行 Prettier:
¥First, we have plugins that let you run Prettier as if it was a linter rule:
这些插件在 Prettier 刚刚出现时特别有用。通过在你的 linters 中运行 Prettier,你不必设置任何新的基础设施,你可以为 linters 重新使用你的编辑器集成。但是现在你可以运行 prettier --check .
,而且大多数编辑器都有 Prettier 支持。
¥These plugins were especially useful when Prettier was new. By running Prettier inside your linters, you didn’t have to set up any new infrastructure and you could re-use your editor integrations for the linters. But these days you can run prettier --check .
and most editors have Prettier support.
这些插件的缺点是:
¥The downsides of those plugins are:
你最终会在编辑器中看到很多红色的波浪线,这很烦人。Prettier 应该让你忘记格式 - 而不是当面谈论它!
¥You end up with a lot of red squiggly lines in your editor, which gets annoying. Prettier is supposed to make you forget about formatting – and not be in your face about it!
它们比直接运行 Prettier 慢。
¥They are slower than running Prettier directly.
它们只是一层间接层,事情可能会中断。
¥They’re yet one layer of indirection where things may break.
最后,我们拥有运行 prettier
的工具,然后通过运行 eslint --fix
立即对文件进行 lint。
¥Finally, we have tools that run prettier
and then immediately lint files by running, for example, eslint --fix
on them.
如果 Prettier 输出的某些方面使 Prettier 对你完全无法使用,那么这些将很有用。然后你可以让 eslint --fix
为你解决这个问题。缺点是这些工具比只运行 Prettier 慢得多。
¥Those are useful if some aspect of Prettier’s output makes Prettier completely unusable to you. Then you can have for example eslint --fix
fix that up for you. The downside is that these tools are much slower than just running Prettier.