Skip to content

Conversation

@fs0414
Copy link
Contributor

@fs0414 fs0414 commented Dec 14, 2025

Description

Fixes an issue where comments in empty function parameters were incorrectly moved after the fat arrow operator (=>) or into the function body.

The problem was caused by handleCommentInEmptyParens not being included in handleOwnLineComment. When a comment is on its own line inside empty parameters, it was processed by handleOwnLineComment, but since handleCommentInEmptyParens was only in handleRemainingComment, the comment was incorrectly attached to the function body as a leading comment.

Additionally, handleLastFunctionArgComments was being evaluated before the empty params case, causing it to match and move the comment to the function body.

Example:

  • Input:
(
  // foo
) => {}
  • Before fix:
() =>
  // foo
  {};
  • After fix:
(
  // foo
) => {};

Solution:

  1. Added handleCommentInEmptyParens to handleOwnLineComment before handleLastFunctionArgComments
  2. Added formatting logic to preserve line breaks for line comments while keeping block comments inline

Changes

  • src/language-js/comments/handle-comments.js: Added handleCommentInEmptyParens to handleOwnLineComment, added condition to exclude comments in return value parentheses
  • src/language-js/print/function-parameters.js: Added formatting for dangling line comments with proper indentation and line breaks

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/javascript/18482.md file following changelog_unreleased/TEMPLATE.md.
  • I've read the contributing guidelines.

Fixes #18208

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 14, 2025

Open in StackBlitz

yarn add https://pkg.pr.new/@prettier/plugin-hermes@18482.tgz
yarn add https://pkg.pr.new/@prettier/plugin-oxc@18482.tgz
yarn add https://pkg.pr.new/prettier@18482.tgz

commit: a0c9ec1

@netlify
Copy link

netlify bot commented Dec 14, 2025

Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 73e0a08
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/693ea352de6e2f00086f3d55
😎 Deploy Preview https://deploy-preview-18482--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@netlify
Copy link

netlify bot commented Dec 14, 2025

Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 276491e
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/693ea36f6f00fb0008f07281
😎 Deploy Preview https://deploy-preview-18482--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@netlify
Copy link

netlify bot commented Dec 14, 2025

Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a0c9ec1
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6942b2bfcf47fe000870fbcc
😎 Deploy Preview https://deploy-preview-18482--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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.

Comment within the arrow function's empty parameters was placed incorrectly after the fat arrow operator

1 participant