Prettier 强制无卷曲的单行 if 语句位于同一行,而不是 below

Prettier forces curlyless, single-line if-statement to be on the same line instead for below

阅读条件语句的 docs for EsLint, I'm using the rule curly set to warning for multiple or nested 行后。

"rules": {
  "curly":["warn", "multi-or-nest"],
  "quotes":"warn"
}

它按预期工作但是当代码让我满意时,Prettier 给我一个错误提示条件后的语句应该放在一行上,这不是我想要的(我也不想要使用不必要的卷曲)。

// Preferred style
if(condition)
  doSomething();

if(condition) {
  doSomething();
  doSomethingElse();
}

// Prettier style
if(condition) doSomeSome();

if(condition) {
  doSomething();
  doSomethingElse();
}

我已经检查了 Prettier 的文档,但最接近我所寻找的是 bracketSpacing,这根本不是我想要的。据我所知,关于多行包围,没有别的了。

如何让 Prettier 的行为符合我的喜好? (第二个问题:除了官方文档,我在哪里可以找到更多关于其他规则的信息?)

注意。 somewhat similar question 前一段时间的提问没有得到答案,评论建议发帖者接受 Prettier 希望的方式(即强制执行多余的卷曲)。计算机告诉我喜欢什么,而不是我告诉它我的 some_rc.json 文件,这对我来说似乎是错误的。

我还发现 a lengthy discussion 证明自 2017 年以来就需要这样的选项,所以我倾向于假设它已经实现(因为我很难相信设计师只是忽视了的愿望)。我错了吗?

答案是否定的。正如 Issue 你 link 所说:

We are now past 1.0 and we're not going to change core printing like this one anymore. Sorry!

If you want code to be printed on the next line, you can add {}. Feel free to fork prettier if you really want this behavior.

但更深层次的一点是,Prettier 非常自以为是。我完全明白为什么这个词会让非英语母语的人感到困惑。它确实有点像“其他人有很高的意见”。但“自以为是”意味着“坚信自己的观点”,在软件世界中意味着“故意缺乏配置”。

The Prettier Option Philosophy 页面总结了他们的政策:

Prettier has a few options because of history. But we won’t add more of them.... Prettier is not a kitchen-sink code formatter that attempts to print your code in any way you wish. It is opinionated.

为什么会这样:

By far the biggest reason for adopting Prettier is to stop all the ongoing debates over styles.

对于许多球队来说,这是一个值得的目标。 Go 语言著名的是 gofmt ,它根本不需要任何选项。几乎每个人都在每个文件上使用它,因此无论您在哪个团队,Go 代码的格式始终完全相同。您不能就制表符或空格或卷曲位置或其他任何问题争论不休。 Go 看起来像 gofmt 所说的那样,即使在某些情况下它有点奇怪。回去工作。 :D

并非每个团队都会喜欢 Prettier 的选择,这很好。许多团队(几乎可以肯定 大多数 团队)不使用它。但是,如果您确实使用了它,那么您注册的不仅仅是一个工具;您正在注册他们的风格选择。