Skip to content

fix: always put a space or newline after an added open brace - #841

Merged
dsherret merged 1 commit into
mainfrom
fix-flip-limit
Nov 1, 2026
Merged

dsherret merged 1 commit into
mainfrom
fix-flip-limit

Conversation

@dsherret

@dsherret dsherret commented Nov 1, 2026

Copy link
Copy Markdown
Member

A body with braces gets the space after the open brace from one condition (spaceCondition) and the newline after it from another (newLineCondition). After the printer went back to an earlier point, the newline could be resolved from where the statements ended before, so neither was printed:

  } else {resolutionCache == null
      ? void 0
      : resolutionCache.forEach((resolutions, filePath) =>
        resolutions.forEach((resolution, name, mode) => callback(resolution, name, mode, filePath))
      );}

In some files this also made openBrace and the isMultiLineForMultiLine of the arguments around it keep flipping until the re-evaluation limit was hit (ex. protobuf.js's protobuf.min.js), so the output was whatever they stopped at.

The newline is now used when there are braces, the body has content, and there's no space after the open brace. The space is resolved earlier in the same pass, so the two can't disagree.

Changes in output

On the deno corpus, this changes the output of 174 files. The ones I looked at went from the above to a newline after the open brace (ex. } else {try { in a web platform test and } else {resolutionCache == null in TypeScript's typescript.js). The files that change when formatted again went from 330 to 317, and no file hits the re-evaluation limit any more.

One file (prettier's parser-flow.mjs) now takes 13 formats to stop changing instead of fewer than 10, so the corpus test now allows 20.

A body with braces gets a space after the open brace from one condition and a
newline after it from another. After the printer went back to an earlier point,
the newline could be resolved from where the statements ended before, so neither
was printed (ex. `} else {try {`), which then also made the open brace and the
arguments around it keep flipping until the re-evaluation limit was hit.

The newline is now used when there are braces, the body has content, and there's
no space after the open brace.

The corpus test now allows 20 formats to stop changing, since one file takes 13
formats with this.
@dsherret
dsherret merged commit 8b5833a into main Nov 1, 2026
1 check passed
@dsherret
dsherret deleted the fix-flip-limit branch November 1, 2026 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant