15

Prettier formats if statement without curley braces into one line.

This means that this :

function getErrorMessage(response) {
    let errorMessage = null;

    if (!response.originalError.response) 
        errorMessage = 'network error';
    else 
        errorMessage = response.originalError.response.data.errorMessage;

    return errorMessage;
}

becomes this :

function getErrorMessage(response) {
    let errorMessage = null;

    if (!response.originalError.response) errorMessage = 'network error';
    else errorMessage = response.originalError.response.data.errorMessage;

    return errorMessage;
}

which is FAR more unreadable.

Is there a way of disabling this?

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
  • 2
    Not an answer to your question (which I upvoted), but your function can be made simpler and even more readable (and ever so slightly more efficient), by eliminating your `errorMessage` variable entirely. https://gist.github.com/flimzy/78be389b85d37ba7536daf5e858b8ca7 – Jonathan Hall Jan 23 '19 at 11:35
  • 1
    yes, i agree, just wanted to use my code as an illustration – Oliver Watkins Jan 23 '19 at 11:39
  • 4
    Possibly not what you want to hear, but how about adding curly braces? IMO they should be there anyway. – Biffen Jan 23 '19 at 11:57
  • 4
    yes i added curly braces. That fixed it. But I don't understand why prettier would do such an ugly formatting – Oliver Watkins Jan 23 '19 at 13:13
  • @OliverWatkins Might be a case of passive-aggressive "my way or the highway" to enforce the style of superfluous paratheses in combination with (and here's my evil assumption) ridicule alternative. It's like saying *you don't have to use curlies on single-statement* but then adding a "proof" of it being wrong by *but this is the ugliness you'll get*. It's such a great package so it's a shame they are so inflexible. See the answer for details. – Konrad Viltersten Jan 15 '21 at 07:50

2 Answers2

6

As asked in a similar question, it turns out that the answer is that you can not and will not be able to.

As for the WFT that an average senses, well... Apparently, opinionated doesn't mean respected and well-considered in opinion of many. It means that it's implementing the author's opinion.

So, surprisingly, the unexpected thing isn't the lack of configurability but rather that there are any options to be set at all! Go figure... Someone should create a new package called EvenPrettier or FexiblyPrettier and fork in more options. If I only knew how, I'd do it.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
0

I finally ended up using Beautify - HookyQR extension for vscode https://marketplace.visualstudio.com/items?itemName=HookyQR.beautify

Example Settings

File: .jsbeautifyrc

{
    "brace_style": "collapse,preserve-inline",
    "max_preserve_newlines": 2,
    "end_with_newline": false
}

Example Code

File: a.js

function dfs(start) {

    if (start > n)
        return;

    ans.push(start);

    for (let i = 0; i <= 9; i++)
        dfs(start * 10 + i);

}
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140