Skip to content

Fix unstable assignment when a member chain object has a leading comment - #19882

Open
Kjubikstronk wants to merge 3 commits into
prettier:mainfrom
Kjubikstronk:idempotency-10851
Open

Fix unstable assignment when a member chain object has a leading comment#19882
Kjubikstronk wants to merge 3 commits into
prettier:mainfrom
Kjubikstronk:idempotency-10851

Conversation

@Kjubikstronk

Copy link
Copy Markdown
Contributor

Description

Fixes #10851.

A comment on the object of a member chain is printed before the whole chain, because the printer adds the parentheses and printComments then wraps the result:

const generate = (
  // @ts-expect-error requireOutside Babel transform
  requireOutside("@babel/generator") as typeof import("@babel/generator")
).default;

After one format the comment leads the right-hand side, which is a state the original source was not in:

const generate = // @ts-expect-error requireOutside Babel transform
(requireOutside("@babel/generator") as typeof import("@babel/generator"))
  .default;

So the second format takes a different branch β€” hasLeadingOwnLineComment is now true, chooseLayout returns break-after-operator, and the value is indented. Two formats, two outputs.

chooseLayout now looks down the member chain for that comment, so the first format already produces what the second one would:

const generate =
  // @ts-expect-error requireOutside Babel transform
  (requireOutside("@babel/generator") as typeof import("@babel/generator"))
    .default;

This also puts the @ts-expect-error back above the line it suppresses, rather than leaving it stranded after the =.

The same shape shows up whenever the object needs parentheses β€” as, await, and binary expressions all reproduce it, and all three are covered in the fixture.

Worth flagging

The one existing snapshot this changes, typescript/prettier-ignore/issue-14238.ts, was recording the unstable output:

export const foo = // prettier-ignore
(bar as Baz).qux;

Feeding that text back through main gives a different result, so that snapshot could not survive a second format either. It now records the stable form.

I did not touch the parenthesis/comment ordering in ast-to-doc.js, which is where the comment actually moves. That looked like a much larger change than the bug warranted, and this fix makes the output stable without it β€” but if you would rather see it solved there, say so and I will look.

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.

A comment on the object of a member chain is printed before the whole chain,
so after one format it leads the right-hand side of the assignment. The next
format then saw a leading own-line comment, picked break-after-operator, and
indented what the first pass had not.

The layout now looks down the member chain for that comment, so the first
format already produces what the second one would.
@netlify

netlify Bot commented Aug 18, 2026

Copy link
Copy Markdown

βœ… Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
πŸ”¨ Latest commit 8f112d3
πŸ” Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a849afaf9b1030008a69086
😎 Deploy Preview https://deploy-preview-19882--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/plugin-hermes@19882.tgz
yarn add https://pkg.pr.new/@prettier/plugin-oxc@19882.tgz
yarn add https://pkg.pr.new/@prettier/plugin-yuku@19882.tgz
yarn add https://pkg.pr.new/prettier@19882.tgz

commit: 23c7bcf

@netlify

netlify Bot commented Aug 18, 2026

Copy link
Copy Markdown

βœ… Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
πŸ”¨ Latest commit 23c7bcf
πŸ” Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a849f624d72f10008e402a6
😎 Deploy Preview https://deploy-preview-19882--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.

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.

Unstable comment (never stabilizing) on type assertion

1 participant