1

I am hoping to run Prettier on an existing codebase that is also using eslint.

There are many places where single-line ifs exist and I want to leave them intact, but Prettier keeps changing them to multi-line without braces, which of course causes an error.

It's going from: if (...) throw new Error(...)

To:

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

What is the magic combination of rules to let Prettier ignore these?

Jazzy
  • 6,029
  • 11
  • 50
  • 74

2 Answers2

0

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. The intended behavior is that if statements without brackets are on single line: GitHub Issue.

You can change the max line length in your .prettierrc file:

{
    "printWidth": 80
}
FINDarkside
  • 2,102
  • 1
  • 18
  • 26
-1

You are using bracketless if statements. 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(...);}
  • 2
    If this is bad for JavaScript, perhaps it needs to stop supporting it officially. Supporting a syntax and expecting developers not to use it makes no sense to me. I use bracketless single line if statements when they only contain something like `continue`. I also do not use semicolons. – Yega Aug 16 '20 at 15:30