fix(js): Fix comment placement in empty function params#18482
fix(js): Fix comment placement in empty function params#18482sorafujitani wants to merge 14 commits intoprettier:mainfrom
Conversation
commit: |
✅ Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for prettier ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
| ) === ")", | ||
| }), | ||
| danglingComments, | ||
| hasDanglingLineComment && danglingComments ? softline : "", |
There was a problem hiding this comment.
I don't think this change is need.
There was a problem hiding this comment.
@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
There was a problem hiding this comment.
#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 syntaxExplanation for why these changes are needed:
- indent: hasDanglingLineComment - Enables indentation only for line comments
- 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))) || |
There was a problem hiding this comment.
Removed the redundant !("body" in enclosingNode) check
b5a97d9
There was a problem hiding this comment.
We don't check body since isInArgumentOrParameterParentheses already fixed the logic.
|
Please add tests for your changes. |
added |
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
handleCommentInEmptyParensnot being included inhandleOwnLineComment. When a comment is on its own line inside empty parameters, it was processed byhandleOwnLineComment, but sincehandleCommentInEmptyParenswas only inhandleRemainingComment, the comment was incorrectly attached to the function body as a leading comment.Additionally,
handleLastFunctionArgCommentswas being evaluated before the empty params case, causing it to match and move the comment to the function body.Example:
Solution:
handleCommentInEmptyParenstohandleOwnLineCommentbeforehandleLastFunctionArgCommentsChanges
src/language-js/comments/handle-comments.js: AddedhandleCommentInEmptyParenstohandleOwnLineComment, added condition to exclude comments in return value parenthesessrc/language-js/print/function-parameters.js: Added formatting for dangling line comments with proper indentation and line breaksChecklist
docs/directory).changelog_unreleased/javascript/18482.mdfile followingchangelog_unreleased/TEMPLATE.md.Fixes #18208