Skip to content

Prettier — formatting nobody argues about

Level: 101 · for anyone who has argued about semicolons

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: Prettier throws your layout away and reprints the code from its syntax tree, with two exceptions it keeps on purpose: blank lines, several collapsing into one, and an object with a line break after {, which it never joins back onto one line.

Keywords: Prettier, .prettierrc, prettier-ignore, printWidth, objectWrap

What the finished page will answer

  • What does Prettier do to quotes, semicolons, trailing commas and a 120-character call, with no configuration at all?
  • Which short object stays on several lines and which joins onto one, depending only on the newline after {?
  • Which quotes does Prettier choose for a string that contains an apostrophe, and why is that not always the singleQuote setting?
  • What does prettier --check exit with for a formatted and an unformatted file, and what does // prettier-ignore keep?
  • What does semi: false put at the start of a line that begins with ( or [, and why?

Examples it will need

  • formatting_prettier_reprints_sh.sh — a messy file before and after prettier: quotes, semicolons, trailing commas, a kept multi-line object and collapsed blank lines
  • formatting_prettier_check_exit_sh.sh — prettier --check's exit status on a formatted and an unformatted file

See also

Sources to start from