Skip to content

Fix unstable trailing comment on a nested parenthesized assignment - #19893

Merged
fisker merged 8 commits into
prettier:mainfrom
Kjubikstronk:nested-paren-assign
Aug 20, 2026
Merged

fisker merged 8 commits into
prettier:mainfrom
Kjubikstronk:nested-paren-assign

Conversation

@Kjubikstronk

Copy link
Copy Markdown
Contributor

Description

Fixes the remaining case in #19271.

#19276 made (a = c /* comment */); stable by attaching the trailing comment to the ExpressionStatement. The nested form was left out and is still unstable on main:

// Input
assigned = (a = c /* comment */);

// First format
assigned = a = c /* comment */;

// Second format
assigned = a = c; /* comment */

The second format is already the fixpoint, so this makes the first one produce it too.

handleParenthesizedExpressionTrailingComment does fire here — it just attaches the comment inward to c. Once the parentheses are dropped the comment is at the end of the statement, and the next run attaches it to the statement instead, which is where the instability comes from.

Attaching to the statement on the first pass needs the statement, and the handler only had enclosingNode. For a chain like x = y = (a = c /* comment */) that is the middle assignment, not the outermost, so walking up is the only way to reach it. decorateComment already builds the ancestor chain and threads it through the recursion for canAttachComment, it just wasn't returned, so this returns it and walks up through the right hand side of each enclosing assignment.

Scope is deliberately narrow, since #19273 was reverted once already. Only assignment chains that end in an ExpressionStatement are affected — everywhere else the parentheses are kept, so nothing was unstable to begin with:

const v = (a = c /* comment */);        // unchanged, parens kept
function f() { return (a = c /* c */); } // unchanged, parens kept
assigned = (a, b /* comment */);         // unchanged, sequences unaffected

Two related variants are still unstable, both unchanged by this PR and both failing identically on main:

assigned = (a = (c /* comment */));      // comment parenthesized around `c`
assigned = (a = c /* comment */), other; // inside a sequence

They need a wider change than this one and I'd rather not fold them in here.

parenthesized-trailing-comment-unstable.js existed only for this case, with a TODO to move it back once stable, so it's removed along with its failed-format-tests.js entry and the case is enabled in parenthesized-trailing-comment.js.

Full tests/format run has the same failures before and after.

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.

@netlify

netlify Bot commented Aug 19, 2026

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 0a805cf
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a86339f2b36100009fdeab3
😎 Deploy Preview https://deploy-preview-19893--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 19, 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: f729799

@netlify

netlify Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Name Link
🔨 Latest commit f729799
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a86819a60b05b000802a876
😎 Deploy Preview https://deploy-preview-19893--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.

@fisker

fisker commented Aug 20, 2026

Copy link
Copy Markdown
Member

Great work! Thank you!

@fisker
fisker merged commit 24a616d into prettier:main Aug 20, 2026
34 checks passed
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.

2 participants