Ali
Ali

Reputation: 514

Prettier not formatting empty line properly

In my React Native code below, I'm expecting prettier to remove the empty line but it doesn't.

screenOptions={{
  headerTitleStyle: {
    color: "#a41034",

    fontWeight: "bold",
  },

Why is that so?

Upvotes: 9

Views: 29528

Answers (2)

fruitloaf
fruitloaf

Reputation: 2892

  • in the file, in vsc, press F1
  • then type: Format Document With...
  • when options come up, on the bottom you will see SELECT DEFAULT FORMATER
  • select Prettier

also in your VSC, click on file -> preferences -> settings -> type: format default, there will be an option: Editor Default Fromatter (drop down), set this to Prettier

its just that some files like JSX/TSX need you to assign them a default formatter, otherwise they will use something like TypeScript formatter, and it will retain blank spaces.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160191

Because Prettier doesn't remove singular blank lines (in general).

From the "empty lines" docs:

[Prettier preserves] empty lines the way they were in the original source code. There are two additional rules:

  • Prettier collapses multiple blank lines into a single blank line.
  • Empty lines at the start and end of blocks (and whole files) are removed. (Files always end with a single newline, though.)

Additionally, multi-line objects are not collapsed into single-lines if there's a newline after the opening curly brace.

Upvotes: 7

Related Questions