configure prettier to push curly braces on new lines & not clear empty lines
Asked Answered
J

2

16

I am using prettier with VSCode, How can I configure it to format my code like this :

function test() 
{

   if() 
      {

         MYCODE GOES HERE;

      }

}

I want the { and } on new lines, and an empty line after { and before }.

Currently, it moves the curly brackets to same lines if condition or function name, and also remove the empty lines after/before { and }.

Jacket answered 29/3, 2020 at 14:4 Comment(0)
H
22

Prettier is considered an "opinionated" formatter, which means it doesn't let you choose things like that. If you want more control over the formatting, you can use a different formatter.

The built-in VS code formatter allows you to do what you're looking for, just search the settings for "function new line" and similar options.

There are of course many other formatting extensions available in the VS code marketplace as well. Whichever you choose, you will have to select it has your default formatter in your VS code settings.

Hitandrun answered 29/3, 2020 at 14:27 Comment(0)
P
1

As mentioned in this answer, VS Code's formatter itself works quite well, but if you want this to be part of workflow, then using ESLint might make things easier. There's a rule called brace-style.

You can then run eslint ./path/to/your/file --fix to format your code, or eslint . --fix to format code in the entire project directory.

Disclaimer: I use ESLint for code formatting most of the time and it works for me. I actually use it to find & fix problems too so it's like killing two birds with one stone, but note that ESLint is more about finding problems in the code and fixing them, so using ESLint just for code formatting might not be the best idea.

Premier answered 17/4, 2021 at 3:0 Comment(2)
Did you really link to the answer directly above yours?Beatty
I'd say is preferable to explicitly link. I've seen threads where one says "the above answer" or "xxxx's answer", but answers gets reorderd over time and according to user preferences, and users may change name also. So, why not?Overkill

© 2022 - 2024 — McMap. All rights reserved.