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

Retain trailing comments in array expressions #10369

Merged
merged 5 commits into from Aug 28, 2019
Merged

Conversation

banga
Copy link
Contributor

@banga banga commented Aug 26, 2019

This is a proposed fix for #10368
with a simple test.

Q                       A
Fixed Issues? Fixes #10368
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This is based on the fix from #8488 but for array expressions. I see some opportunity for code reuse here, but wanted to keep the changes isolated.

This is a proposed fix for babel#10368
with a simple test.
@babel-bot
Copy link
Collaborator

babel-bot commented Aug 26, 2019

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11403/

@existentialism existentialism added area: comments PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser labels Aug 27, 2019
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

I see some opportunity for code reuse here, but wanted to keep the changes isolated.

If you think that it's worth it, please open a PR after that this one is merged!

},
{
"type": "CommentLine",
"value": " Three",
Copy link
Contributor

@JLHwung JLHwung Aug 27, 2019

Choose a reason for hiding this comment

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

🤔 I think Three should not be the trailing comment of the string literal, instead it should be of the array expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good catch. I'll try fixing that.

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

@banga Thanks.

I think that the CallExpression arguments may also be affected, but we could open a new PR to address that. And ideally we can refactor to have CallExpression and ArrayExpression shared the same routines.

fn(
  a,
  b,
   /* comment */
)
/* outer */

@banga
Copy link
Contributor Author

banga commented Aug 28, 2019

Yeah, sounds good

@JLHwung JLHwung merged commit 8a775a3 into babel:master Aug 28, 2019
banga added a commit to banga/babel that referenced this pull request Aug 28, 2019
* Retain trailing comments in array expressions

This is a proposed fix for babel#10368
with a simple test.

* Move lastElement in the block where it's used

* Test trailing comment after array expression

* Don't move comments after the array expression

* Retain trailing comment after the array expression
@JLHwung
Copy link
Contributor

JLHwung commented Aug 29, 2019

@banga Thanks, landed in 8a775a3. Feel free to open a new PR for CallExpression arguments or you would like to refactor the code after.

@banga
Copy link
Contributor Author

banga commented Aug 29, 2019

Thanks! Will do in a couple of days.

banga pushed a commit to banga/babel that referenced this pull request Aug 31, 2019
Following up from babel#10369

- Unify the logic for adjusting trailing comments into a separate
  function
- Use it for all three cases, which fixes a bug for ObjectExpressions
  and CallExpressions
- Update tests to check for the fixed bug
JLHwung pushed a commit that referenced this pull request Sep 3, 2019
* Refactor trailing comment adjustment

Following up from #10369

- Unify the logic for adjusting trailing comments into a separate
  function
- Use it for all three cases, which fixes a bug for ObjectExpressions
  and CallExpressions
- Update tests to check for the fixed bug

* Fix tests

- Only modify trailingComments if necessary
- Update snapshots of a couple of affected tests

* Drop the underscore in adjustCommentsAfterTrailingComma_

* Handle ArrayPattern

* Handle ObjectPattern

* Handle import and export declarations

These have to be handled a bit differently, because the  node is visited after the  and before the declaration. So we intercept when we are going from the last specifier to the source node.

* Remove unnecessary check
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 2, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: comments outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Comment after the last item in an array literal is dropped if there's a trailing comma
5 participants