简体   繁体   English

更漂亮的重新格式化单行if语句会导致eslint错误:为什么?

[英]Prettier re-formatting single line `if` statements causing eslint errors: why?

I am hoping to run Prettier on an existing codebase that is also using eslint. 我希望在也使用eslint的现有代码库上运行Prettier。

There are many places where single-line if s exist and I want to leave them intact, but Prettier keeps changing them to multi-line without braces, which of course causes an error. 在很多地方, if存在单行if ,我想保持它们完整无缺,但是Prettier会不断将它们更改为多行而没有大括号,这当然会导致错误。

It's going from: if (...) throw new Error(...) 它来自于: if (...) throw new Error(...)

To: 至:

if (...)
  throw new Error(...)

What is the magic combination of rules to let Prettier ignore these? 让Prettier忽略这些规则的神奇组合是什么?

You are using bracketless if statements. 您正在使用无括号的if语句。 You also forgot the semicolons. 您还忘记了分号。 Bracketless if statements are unreliable. 如果声明不可靠,则不要使用括号。 Adding braces after will get rid of your problem: 之后添加大括号将消除您的问题:

if (...) {
   throw new Error(...);
}

This also works: 这也适用:

if (...) {throw new Error(...);}

You need to change your maximum allowed line length, the default is 80. That's the only reason Prettier would wrap your bracketless if to multiple lines. 您需要更改允许的最大行长,默认为80。这是Prettier将多行换行的唯一原因。 The intended behavior is that if statements without brackets are on single line: GitHub Issue . 预期的行为是,如果不带括号的语句放在单行上: GitHub Issue

You can change the max line length in your .prettierrc file: 您可以在.prettierrc文件中更改最大.prettierrc

{
    "printWidth": 80
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM