typescripteslintbracketsprettier

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


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"
}

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();
}

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.

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. 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.

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?


Solution

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

    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.

    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 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.

    And for why it is like this:

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

    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

    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.