Question

Prettier custom rules (formatter)?

Posted May 29, 2021 110 views
VS Code

Is there a way to set a full custom rule for prettier code formatter?

For example, if you don’t like this CSS coding style:

Body {
color: red;
}

But you like it that way:

Body
{
color:red;
}

No space after : and a new line for { and }

Or in HTML prettier removes new lines after opening a DIV like:

<div>
<p>My paragraph Text</p>
<p>My paragraph Text</p>
</div>

But I would not remove my custom new lines like:

<div>

<p>My paragraph Text</p>

<p>My paragraph Text</p>

</div>

Prettier makes the Code like this:

<div>
<p>My paragraph Text</p>

<p>My paragraph Text</p>
</div>

In between, prettier “allows” new lines.
But inside something like a DIV, the first and last element “touch” the opening and ending DIV.
So basically for HTML I would like to disable the deletion of new blank lines.

Is there a way to create a “coding style template” for each language so that prettier follow that “rules”?!

Or is there an other extension to do that?

Every company have their own “coding style”.
90% is the same, but some small things are different.

For example 4 new blank lines, after each Function to separate them better from each other.
But if those lines get removed, you can’t use prettier…

So basically I (we) search for a full customization Coding style (formatter) Extension for VSCode.

Thanks for reading, and sry for the long text.

Best wishes, and stay healthy.
Benjamin Schäfer

2 comments
  • There is limited tweaking of rules for Prettier. The point of Prettier is that it is an opinionated code formatter. So don’t expect much customization. Your best bet is probably to look for an alternative.

  • @lem0ndragon
    Strange… When I’m not logged in I can click on “Reply” but when I’m login to this site the “Reply” Button disapeared. :-D
    Anyways…

    I thought about that.
    Prettier is “nice and quick”, but maybe not what I’m searching for.
    I (or we) use the { } brackets in a new own line to make it more visible (like shown above).
    And that the new lines get removed in HTML Code isn’t that good either…

    Do you know any other VSCode extensions which can create custom code parser?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

You can customize your prettier configurations by adding a .prettierrc file in your project root directory.

Read more here about config for languages - https://prettier.io/docs/en/configuration.html