“什么是一个在 Prettier 中可以完成但在 EsLint 中无法完成的规则示例?”

huangapple go评论60阅读模式
英文:

What is an example of a Prettier rule that can't be done in EsLint?

问题

我很好奇,我看到像这样的评论...

> 正如之前提到的,Prettier负责代码格式化,而ESLint负责代码风格。前者会自动完成所有工作。如果你已经设置了Prettier,可以配置它在保存文件时格式化文件。这样,你就不需要再担心代码格式了。由于Prettier有很强的主观意见,你只能进行一些小的配置。
>
> 后者,ESLint,不打算自动执行代码风格修复。相反,ESLint会警告你代码中的问题。

但显然,这里的“打算”做了很多工作,例如,ESLint可以处理很多风格方面的问题,如行长度缩进换行等。

事实上,我在这里看到的大部分内容都有ESLint的替代方案。那么,如果我已经使用ESLint,为什么要使用Prettier呢?有哪些Prettier特定的样式规范强制执行的示例呢?

英文:

I am curious, I see comments like this...

> 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. That way, you never need to worry about your code formatting anymore. Since Prettier is highly opinionated, you can do only minor configurations.
>
> The latter, ESLint, isn't intended to perform code style fixes automatically though. Instead, ESLint warns you about code smells

But clearly there is a lot of work being done by intended here for example eslint can handle plenty of stylistic stuff like line length, indent, new lines, etc

In fact most of the stuff I see here has an eslint alternative. So why would I use prettier if I already use lint? What are the examples of prettier specific enforcement of styling?

答案1

得分: 2

关于你提到的一些规则,特别是缩进或换行,这并不重要,ESlint和prettier都会导致相同的结果。但是max-len和printWidth的工作方式显然不同。max-len只会计算字符数,并在超过限制时报错,但需要手动修复错误。

根据prettier文档 ,这里是printWidth选项的工作原理的对比:

Prettier的printWidth选项不同。它不是硬性的行长度限制。它只是告诉Prettier大致希望行有多长。Prettier会生成较短和较长的行,但通常会努力满足指定的printWidth。

此外,prettier会自动修复行长度。例如,看下面的代码:

function myVeryCoolFunctionWithLongName(myFirstFunctionArgument, mySecondFunctionArgument, myThirdFunctionArgument) {
}

使用默认的prettier设置,它将被更改为:

function myVeryCoolFunctionWithLongName(
  myFirstFunctionArgument,
  mySecondFunctionArgument,
  myThirdFunctionArgument,
) {}

而ESLint会报错。

总结一下,虽然ESLint在检测格式问题方面做得不错,但prettier在自动修复代码以满足一致格式方面更加智能。再次仔细阅读你引用的这句话:

ESLint并不打算自动执行代码样式修复

进一步参考:

英文:

For some rules you mentioned, namely indent or new lines, it doesn't matter, both ESlint and prettier lead to the same result. However max-len and printWidth work significantly different. max-len will just count the number of characters and throws an error if it's exceeded, but the error needs to be manually fixed.

According to the prettier docs, here is how the printWidth option works in contrast:

> Prettier’s printWidth option does not work the same way. It is not the hard upper allowed line length limit. It is a way to say to Prettier roughly how long you’d like lines to be. Prettier will make both shorter and longer lines, but generally strive to meet the specified printWidth.

Additionally, prettier automatically fixes the line length of your lines. For example, see the following code:

function myVeryCoolFunctionWithLongName(myFirstFunctionArgument, mySecondFunctionArgument, myThirdFunctionArgument) {
}

With default prettier settings, it will be changed to:

function myVeryCoolFunctionWithLongName(
  myFirstFunctionArgument,
  mySecondFunctionArgument,
  myThirdFunctionArgument,
) {}

Whereas ESLint would just throw an error.

To summarize, while ESLint does a decent job in detecting formatting issues, prettier is much smarter in fixing your code to meet a consistent format. Read very closely again the sentence you cited:

> ESLint isn't intended to perform code style fixes automatically

Further reference:

答案2

得分: 0

如prettier文档所述:

> Linters有两类规则:
>
> 格式规则:例如max-len,no-mixed-spaces-and-tabs,keyword-spacing,comma-style…
>
> Prettier减轻了对这整个规则类别的需求!
> Prettier将以一致的方式从头开始重新打印整个程序,所以程序员不可能再犯错了 “什么是一个在 Prettier 中可以完成但在 EsLint 中无法完成的规则示例?”
>
> 代码质量规则:例如no-unused-vars,no-extra-bind,no-implicit-globals,prefer-promise-reject-errors…
>
> Prettier对这类规则毫无帮助。它们也是由Linters提供的最重要的规则,因为它们很可能会捕获到代码中的真正错误!
>
> 换句话说,使用Prettier进行格式化,使用Linters来捕获错误!

事实上,prettier会更快速、更可预测地完成这项工作,因为它专为此而设计。
如果你使用ESLint进行格式化,就像用5磅的锤子来敲钉子一样。

用合适的工具做合适的工作

我强烈建议你观看Theo的视频,讨论这个主题。

查看更多:

英文:

As prettier docs mentioned:

> Linters have two categories of rules:
>
> Formatting rules: eg: max-len, no-mixed-spaces-and-tabs,
> keyword-spacing, comma-style…
>
> Prettier alleviates the need for this whole category of rules!
> Prettier is going to reprint the entire program from scratch in a
> consistent way, so it’s not possible for the programmer to make a
> mistake there anymore “什么是一个在 Prettier 中可以完成但在 EsLint 中无法完成的规则示例?”
>
> Code-quality rules: eg no-unused-vars, no-extra-bind,
> no-implicit-globals, prefer-promise-reject-errors…
>
> Prettier does nothing to help with those kind of rules. They are also
> the most important ones provided by linters as they are likely to
> catch real bugs with your code!
>
> In other words, use Prettier for formatting and linters for catching
> bugs!

In fact, prettier will do it faster and more predictable because it is designed to do so.
If you use ESLint for formatting, it would be like using a 5 pound hammer to drive a nail.

Use the right tool for the right job

I highly recommend you watch the video of Theo on this subject.

See more:

huangapple
  • 本文由 发表于 2023年6月6日 00:10:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定