skip to content

How does Prettier 3 decide where to break lines, and why is `printWidth` not a hard limit like ESLint's `max-len`?

level: middleimportance: should knowfreq 18%

answer

  1. parse, discard, reprint
  2. fits on one line or breaks
  3. 80 is a target
  4. long strings cannot be split
  5. trailing commas changed in 3.0

basics

~20 s

Prettier parses the file into an AST, throws away nearly all original layout and reprints it, breaking a construct only when it does not fit printWidth (default 80). That width is a goal, so some lines run longer.

solid answer

~40 s

Prettier parses the code, drops almost all of the original formatting and reprints the AST with its own rules. The printer builds an intermediate document, measures whether each group fits in `printWidth` columns and breaks it only when it does not. `printWidth` (default `80`) is therefore a target: lines can end up shorter or longer, and a long string, regular expression or comment cannot be broken at all. ESLint's `max-len` is the opposite kind of thing, a ceiling it reports, with no notion of a preferred length. A few original choices survive: blank lines (collapsed to one), a multi-line object whose first key sits on a new line, and string escapes. The rest is defaults plus very few options; in Prettier 3, `trailingComma` defaults to `"all"`, changed from `"es5"` in 3.0.0.

go deeper

for a junior

Recall that Prettier rewrites the whole file from its parsed structure and that printWidth defaults to 80. Know that a few line breaks you typed, such as blank lines, are kept.

for a middle

Explain the fit-or-break decision, why printWidth is a target unlike max-len, which original choices survive, and the Prettier 3 trailingComma default.

for a senior

Diagnose surprises: a long line that never wraps, an object that will not collapse, a diff full of commas after an upgrade, and say which option or edit fixes each.

for a principal

Defend a near-default configuration: every added option reopens style debates, so settle the few that matter and leave the rest to Prettier.

## Reprinting from the AST Prettier's docs put it plainly: it "disregards the original styling by parsing it away and re-printing the parsed AST with its own rules that take the maximum line length into account". In steps: 1. **Parse** the file into an abstract syntax tree (AST), the structure of the program with layout removed. 2. **Build an intermediate representation** of the output: groups that can be printed flat or broken across lines. The printer is based on Philip Wadler's "A prettier printer" algorithm. 3. **Measure and break**: for each group, if the flat form fits in the remaining width it is printed on one line, otherwise it breaks, and the inner groups are measured again. The consequence is that two developers who type the same program with different spacing and indentation get the same output, apart from the few choices Prettier deliberately preserves. The first requirement in Prettier's rationale is **correctness**: the output must behave exactly like the input. ## `printWidth` is a target, not a ceiling `printWidth` defaults to **80**. The options page warns that it "is not the hard upper allowed line length limit"; it says roughly how long you would like lines to be. Prettier makes shorter and longer lines while striving for that width. | | Prettier `printWidth` | ESLint `max-len` | |---|---|---| | Kind | preferred width the printer aims at | maximum a rule reports | | Fixes anything? | yes, by reprinting | no, it only reports | | Long string or comment | left as is, line stays long | reported | | Status in ESLint | not applicable | core rule deprecated since ESLint 8.53.0 | Lines over 80 columns typically appear where nothing can be broken: a long string literal, a regular expression, a URL in a comment. Setting `max_line_length` in `.editorconfig` also sets the print width unless a Prettier config overrides it. The docs advise against raising the width much above 80 for readability. ## What survives from the original source Prettier keeps a handful of author decisions because no rule can infer them: - **Blank lines** are preserved, but several in a row collapse into one, and blank lines at the start and end of blocks are removed. - **Multi-line objects**: if there is a newline between `{` and the first key, the object stays expanded even when it would fit. Removing that newline lets Prettier collapse it. Since 3.5.0 the `objectWrap` option (`"preserve"` by default, or `"collapse"`) controls this heuristic. - **String escapes** stay as written, and quotes are chosen to need the fewest escapes, double quotes unless `singleQuote` is set. - **`// prettier-ignore`** keeps the next node exactly as typed. ## Few options, on purpose The option philosophy page says Prettier "has a few options because of history. **But we won't add more of them.**" More options would turn style debates into debates about Prettier options, so formatting option requests are closed without discussion. The options teams do set, with their Prettier 3 defaults: - `printWidth`: `80` - `tabWidth`: `2`, and `useTabs`: `false` - `semi`: `true`; `false` adds semicolons only at the start of lines that could break automatic semicolon insertion - `singleQuote`: `false` (JSX has its own `jsxSingleQuote`) - `trailingComma`: `"all"`, the default since **3.0.0** (it was `"es5"`); `"none"` removes them - `endOfLine`: `"lf"`, the default since 2.0.0 - `arrowParens`: `"always"`, the default since 2.0.0 The trailing-comma change is the one that bites upgrades: a project with no config that moves from Prettier 2 to 3 sees new commas after the last argument of multi-line calls and parameter lists, and `prettier --check` fails until the code is rewritten or `"trailingComma": "es5"` is set to keep the old output. ## Why it matters in practice - Do not tune `printWidth` to silence a long line; if a string is long, Prettier will not split it, so only a human edit helps. - If you keep a line-length lint rule beside Prettier, keep its limit and `printWidth` in sync, or the two disagree. - When someone asks for an option to preserve their line breaks, the answer is no by design: preserving input formatting is "an option in disguise". - `printWidth` reaches into other languages differently: in Markdown, prose is left wrapped as written unless `proseWrap` is set to `"always"`, because some renderers treat line breaks as meaningful. - If a line-length value already lives in `.editorconfig` as `max_line_length`, Prettier uses it as the print width; a `printWidth` in the Prettier config takes precedence, so check both files before concluding that Prettier ignored your setting.

  • Why does a 140-character line survive `prettier --write` with `printWidth: 80`?
    Prettier breaks only at points the syntax allows. A single long string literal, a regular expression or a comment has no such point, so the printer leaves the line long; splitting the string is a code change a human must make.
  • How do you keep Prettier 2's trailing-comma output after upgrading to Prettier 3?
    Set `"trailingComma": "es5"` in the `.prettierrc`. Prettier 3.0.0 changed the default from `"es5"` to `"all"`, which adds commas after the last parameter and argument of multi-line function signatures and calls.

Prettier is a typesetter who re-sets every page from the manuscript's words, keeping only the author's paragraph breaks and a few marked choices, and aiming for a column width rather than cutting text at it.

  • printWidth is the maximum line length Prettier guarantees
  • Prettier only fixes the lines that break its rules
  • Prettier 3 still defaults trailingComma to es5
  • Raising printWidth will split a long string literal
  • Prettier will add an option if enough teams ask

Assumes Prettier 3 (3.9.9): trailingComma defaults to "all" since 3.0.0, and objectWrap exists since 3.5.0.