Skip to content

Fix range formatting expanding to a non-source-element node - #19880

Merged
fisker merged 5 commits into
prettier:mainfrom
Kjubikstronk:range-fmt
Aug 18, 2026
Merged

fisker merged 5 commits into
prettier:mainfrom
Kjubikstronk:range-fmt

Conversation

@Kjubikstronk

Copy link
Copy Markdown
Contributor

Description

Fixes #19795.

findSiblingAncestors walks up from the start node looking for a wider node that still ends inside the range, but it only compares offsets and never re-checks that what it lands on is something isSourceElement accepts. On the input from the issue it settles on the ArrowFunctionExpression, so the range ends up covering an expression, which prints as a standalone element and picks up a trailing semicolon:

beforeEach(() => {
  // c
  a();
};);

Adding the isSourceElement check to that loop fixes it. The same shape of bug hits object and class methods, except there the range lands on ObjectMethod/ClassMethod and formatting throws Missing semicolon rather than producing bad output. Fixtures for all three.

To check the blast radius I formatted 10 small sources at every possible start/end pair (about 7k ranges) and asserted the result reparses. On main, 280 ranges produced unparseable output and 154 threw; with this change both are zero, and every range that already worked produces byte-identical output.

The end-node loop just below has the same unchecked promotion. I couldn't find an input where it lands on anything but the root File, so I've left it as is.

Unrelated, but noticed while in here: calculateRange passes opts to findSiblingAncestors while findNodeAtOffset and the return value use locFunctions, which for JS is locEndWithFullText. Making that consistent changed nothing in the sweep, so I didn't fold it into this PR.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.
  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

findSiblingAncestors walked the start node's ancestor chain comparing only
offsets, so it could settle on a node that isSourceElement rejects. Printing
an expression as a standalone element appended a semicolon after the closing
brace, and object/class methods threw outright.
@netlify

netlify Bot commented Aug 17, 2026

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ccde31c
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a839c5e3efa310008001346
😎 Deploy Preview https://deploy-preview-19880--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify

netlify Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Name Link
🔨 Latest commit 3f39ff1
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a8432c053a4af0008f74787
😎 Deploy Preview https://deploy-preview-19880--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Aug 18, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

yarn add https://pkg.pr.new/@prettier/[email protected]
yarn add https://pkg.pr.new/@prettier/[email protected]
yarn add https://pkg.pr.new/@prettier/[email protected]
yarn add https://pkg.pr.new/[email protected]

commit: 3f39ff1

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.

Range formatting appends a stray token outside the range when the range starts with a comment in an arrow-function body

2 participants