更漂亮的强制无卷曲的单行if语句在同一行上,而不是在下面 [英] Prettier forces curlyless, single-line if-statement to be on the same line instead for below

查看:87
本文介绍了更漂亮的强制无卷曲的单行if语句在同一行上,而不是在下面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读针对EsLint的文档后,我正在使用规则

After reading the docs for EsLint, I'm using the rule curly set to warning for multiple or nested rows of statements at conditionals.

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

它可以按预期工作,但是当代码令我满意时,Prettier给我一个错误,建议将条件后的语句放在一行上,这不是我想要的(我也不想使用)不必要的小冰壶.)

It works as expected but when the code is to my satisfaction, Prettier gives me an error suggesting that the statement after the conditional should be put on a single line, which isn't what I want (nor do I want to use the unnecessary curlies).

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

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

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

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

我已经检查了文档中是否有Prettier,但与我所寻找的最接近的东西是 bracketSpacing ,这根本不是我想要的.据我所知,关于多行包围,别无其他.

I've checked the docs for Prettier but the closest thing to what I look for is bracketSpacing, which isn't what I want at all. There's nothing else about multiline bracketting as far I could see.

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

How can I make Prettier behave to my liking? (Secondary question: where can I find more info about other rules, except in the official docs?)

NB.之前问过一个类似的问题,但没有答案和评论建议发布者接受Prettier希望的方式(即执行多余的小卷发).对我来说,计算机告诉我喜欢什么而不是我告诉我我的 some_rc.json 文件.

NB. A somewhat similar question asked a while ago got no answers and the comments suggested the poster to accept the way Prettier wants it to be (i.e. enforcing superfluous curlies). It seems wrong to me that the computer tells me what to like instead of me telling it my an some_rc.json file.

我还发现冗长的讨论证明有必要进行此类讨论选项自2017年以来,因此我倾向于假定它已被实施(因为我很难相信设计师只是忽略了愿望).我误会了吗?

I also found a lengthy discussion proving that there's been a need for such an option since 2017, so I'm inclined to assume that it's been implemented (as I find it hard to believe that the designers simply disregarded the wishes). Am I mistaken?

推荐答案

答案是否定的.正如问题一样,您链接注释:

The answer is no. As the Issue you link notes:

我们现在已经过1.0,我们不再打算像这样更改核心打印.抱歉!

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具有很高的自决心.我绝对明白为什么非英语母语人士会混淆该词.看起来确实像其他人有很高的评价".但是有主观的"是指意思是强烈相信自己的意见",并且在软件界意味着故意缺乏配置".

But the deeper point is that Prettier is highly opinionated. I definitely see why that word could be confusing for non-native English speakers. It does kind of look like "others have a high opinion." But "opinionated" means "believing strongly in ones own opinions," and in the software world means "intentionally lacking configuration."

漂亮的期权哲学页总结了他们的政策:

The Prettier Option Philosophy page sums up their policy:

由于历史原因,更漂亮有一些选择.但是我们不会添加更多它们....Prettier并不是试图以任何您希望的方式打印您的代码的厨房水槽代码格式化程序.这是有根据的.

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.

以及为什么会这样:

到目前为止,采用Prettier的最大原因是停止所有有关样式的正在进行的辩论.

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

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

It's a worthy goal for many teams. The Go language famously has gofmt which takes no options at all. Almost everyone uses it on every file, so Go code is always formatted exactly the same no matter what team you're on. You can't argue about tabs or spaces or curly placement or anything else. Go looks like what gofmt says it looks like, even if in some cases it's kind of weird. Get back to work. :D

并不是每个团队都会喜欢Prettier的选择,这很好.许多团队(几乎可以肯定是 most 个团队)都不使用它.但是,如果您确实使用它,就意味着您不仅要注册使用一种工具,还需要注册.您正在申请他们的款式选择.

Not every team is going to like Prettier's choices, and that's fine. Many teams (almost certainly most teams) don't use it. But if you do use it, you're not signing up for just a tool; you're signing up for their style choices.

这篇关于更漂亮的强制无卷曲的单行if语句在同一行上,而不是在下面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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