Skip to content

fix(js): Fix comment placement in empty function params#18482

Closed
sorafujitani wants to merge 14 commits intoprettier:mainfrom
sorafujitani:feature/cmt--empty-allow-func
Closed

fix(js): Fix comment placement in empty function params#18482
sorafujitani wants to merge 14 commits intoprettier:mainfrom
sorafujitani:feature/cmt--empty-allow-func

Conversation

@sorafujitani
Copy link
Copy Markdown
Contributor

@sorafujitani sorafujitani 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
Copy Markdown

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: f1a7472

@netlify
Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

netlify bot commented Dec 14, 2025

Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f1a7472
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6953c121440e5c000814bfd7
😎 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.

) === ")",
}),
danglingComments,
hasDanglingLineComment && danglingComments ? softline : "",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is need.

Copy link
Copy Markdown
Contributor Author

@sorafujitani sorafujitani Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker
tested removing these changes, but the tests fail
without them. The indent and softline additions are necessary to:

  • Properly indent line comments in empty params: (// foo\n) => {}(\n // foo\n) => {}
  • Add line break after comments

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#18540 should already fixed it.

Copy link
Copy Markdown
Contributor Author

@sorafujitani sorafujitani Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker

#18540 corrected the detection logic, but printer-side changes still appear to be necessary.

Problem description: By default, printDanglingComments doesn't perform indentation. For line comments within empty parameters, you must explicitly set indent: true:

// Without using indentation options
printDanglingComments(path, options, { filter })
// Output result: (// foo) => {}  ❌ Syntax error

// With indentation option set to true
printDanglingComments(path, options, { filter, indent: true })
// Output result:
//   // foo
// ) => {}  ✅ Correct syntax

Explanation for why these changes are needed:

  1. indent: hasDanglingLineComment - Enables indentation only for line comments
  2. softline - Inserts a newline after the comment to ensure the closing parenthesis ) appears on the next line

For block comments, these changes aren't necessary as they work correctly without them: (/* foo */) => {} ✅

Test case: tests/format/js/comments/function/empty-params-with-comment.js

  • ✅ Passes when changes are applied
  • ❌ Fails when changes are omitted (line comments remain unindented)

// Make sure comment is before the function body (not in return value parentheses)
(!("body" in enclosingNode) ||
!enclosingNode.body ||
locStart(comment) < locStart(enclosingNode.body))) ||
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the redundant !("body" in enclosingNode) check
b5a97d9

Copy link
Copy Markdown
Contributor Author

@sorafujitani sorafujitani Dec 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker
Removing !("body" in enclosingNode) causes lint errors
since some nodes (e.g., TSCallSignatureDeclaration) don't have a body property. It's
needed as a type guard.

this commit adds type guards
038a62c

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check body since isInArgumentOrParameterParentheses already fixed the logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

81b7e66
fixed

@fisker
Copy link
Copy Markdown
Member

fisker commented Dec 30, 2025

Please add tests for your changes.

@sorafujitani
Copy link
Copy Markdown
Contributor Author

sorafujitani commented Dec 30, 2025

変更内容に対するテストケースを追加してください。

added
d80ce0a

@sorafujitani sorafujitani requested a review from fisker December 30, 2025 05:21
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

2 participants