Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing inner comments in function expressions #13825

Merged
merged 2 commits into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-generator/src/generators/methods.ts
Expand Up @@ -84,6 +84,7 @@ export function _functionHead(this: Printer, node: any) {
}
this.word("function");
if (node.generator) this.token("*");
this.printInnerComments(node);

this.space();
if (node.id) {
Expand Down
@@ -0,0 +1,4 @@
const f = function /*foo*/ () {};
const g = async function /*foo*/ () {};
const h = function* /*foo*/ () {};
const i = async function* /*foo*/ () {};
Copy link
Contributor

@JLHwung JLHwung Oct 7, 2021

Choose a reason for hiding this comment

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

Can you add a new test case?

const j = function (/*foo*/) {};

it will be printed as

const j = function /*foo*/ () {};

I think we don't have to fix that. It is the nature of using AST for comment attachment targets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have pushed another commit adding this case.

I also added cases for other positions of comments in async + generator functions.

const x = async /*foo*/ function () {};
const y = function/*foo*/* () {};

Similarly, these don't come out quite the same as the input, but at least the comment isn't omitted entirely now.

@@ -0,0 +1,3 @@
{
"retainLines": true
}
@@ -0,0 +1,4 @@
const f = function /*foo*/ () {};
const g = async function /*foo*/ () {};
const h = function* /*foo*/ () {};
const i = async function* /*foo*/ () {};