Skip to content

Fix unstable own line comment in a for-in or for-of head - #19895

Open
Kjubikstronk wants to merge 3 commits into
prettier:mainfrom
Kjubikstronk:for-of-comments
Open

Fix unstable own line comment in a for-in or for-of head#19895
Kjubikstronk wants to merge 3 commits into
prettier:mainfrom
Kjubikstronk:for-of-comments

Conversation

@Kjubikstronk

Copy link
Copy Markdown
Contributor

Description

js/for-of/comments.js is one of the unstableTests entries in tests/config/format-test/failed-format-tests.js. This makes it stable and removes it from the list.

An own line comment in a for-in or for-of head is unstable because printing it turns it into an end of line comment, and the two placements take different branches:

// Input
for (x
//c
in y);

// Prettier stable (first format)
for (x in //c
y);

// Prettier stable (second format)
for (x in y); //c

The comment starts out on its own line between left and right, so it becomes a leading comment of right and prints right after in. That puts it at the end of a line, so the next format makes it a trailing comment of left instead and moves it out of the head.

There is no line inside the head to keep it on, so it ends up outside either way. This attaches it to left immediately, which is where the second format already put it. Block comments were unstable the same way and are fixed by the same change:

for (x
/*c*/
in y);   // was: for (x in /*c*/\ny);  then: for (x /*c*/ in y);

Note the old first format was not just unstable but mis-indented β€” y); was printed at column 0.

The handler is registered in the own line list only. End of line and remaining comments already attach to left by default and are unchanged:

for (x in //c
y);                  // -> for (x in y); //c   (unchanged)
for (x /*c*/ in y);  // -> for (x /*c*/ in y); (unchanged)
for (x in /*c*/ y);  // -> for (x in /*c*/ y); (unchanged)

ForStatement is not included β€” its head already breaks, so own line comments there are stable and print in place. Comments before left, and comments before the body, are also untouched, the latter being what the existing handleForXStatementComments already handles.

The resulting fixpoint keeps the comment on its own line, so it differs from the old second format output for these inputs. Since the old first and second formats disagreed, neither was a settled output.

One related case is still unstable, unchanged by this PR and failing the same way on main:

for (
/*c*/
x of y);

It has no preceding node, so it needs different handling than this and I would rather not fold it in here.

Across the whole tests/format run this changes exactly one snapshot, the fixture itself, and the failing set is the same 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 c2c3f47
πŸ” Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a863936c6346e0008073eac
😎 Deploy Preview https://deploy-preview-19895--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/plugin-hermes@19895.tgz
yarn add https://pkg.pr.new/@prettier/plugin-oxc@19895.tgz
yarn add https://pkg.pr.new/@prettier/plugin-yuku@19895.tgz
yarn add https://pkg.pr.new/prettier@19895.tgz

commit: e1e78ee

@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 e1e78ee
πŸ” Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a90b41445b3b80008527e47
😎 Deploy Preview https://deploy-preview-19895--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.

1 participant