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

Refactor trailing comment adjustment #10380

Merged
merged 7 commits into from Sep 3, 2019

Conversation

banga
Copy link
Contributor

@banga banga commented Aug 31, 2019

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

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

Shrey Banga added 2 commits August 31, 2019 10:46
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
- Only modify trailingComments if necessary
- Update snapshots of a couple of affected tests
@babel-bot
Copy link
Collaborator

babel-bot commented Aug 31, 2019

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

@@ -38,6 +38,48 @@ export default class CommentsParser extends BaseParser {
this.state.leadingComments.push(comment);
}

adjustCommentsAfterTrailingComma_(node: Node, elements: Node[]) {
Copy link
Member

Choose a reason for hiding this comment

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

Why the trailing underscore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I wanted to mark it as a private function and out of habit followed the convention from another repo. I don't see a convention in babel so I'll just drop the underscore.

// comma separated list of nodes to be the trailing comments on the last
// element
if (firstChild) {
switch (node.type) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also SequenceExpression? (I didn't check)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think SequenceExpression will allow trailing comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I skimmed through https://github.com/babel/babylon/blob/master/ast/spec.md and looks like ImportDeclaration and ArrayPattern also allow trailing commas. Should I add those?

Copy link
Member

Choose a reason for hiding this comment

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

Yeas please! And also ExportNamedDeclaration and ObjectPattern

@nicolo-ribaudo nicolo-ribaudo added pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories PR: Internal 🏠 A type of pull request used for our changelog categories area: comments labels Aug 31, 2019
nicolo-ribaudo
nicolo-ribaudo previously approved these changes Aug 31, 2019
Shrey Banga added 3 commits August 31, 2019 14:26
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.
/* One */ Foo
/* Two */,
/* Three */
} /* Four */ from "foo";
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't "Four" be an innerComment of ExportNamedDeclaration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be better, but from what I can tell, there's no way to figure out which comments are before the closing bracket and which ones are after. Before the PR, the last three comments get attached as leadingComments of foo, so we were at least not dropping them. So I could remove handling of ImportDeclaration and ExportDeclaration from this PR if you prefer the prior logic.

Copy link
Member

Choose a reason for hiding this comment

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

I tried to call adjustCommentsAfterTrailingComma right before eating the } in parseNamedImportSpecifiers, instead of doing so in processComment. By doing so, Four gets attached as a leadingComments of "foo".

I'm ok with either behaviors (leadingComments of "foo" or trailingComments of Foo); attaching it as an innerComment is too hard with the current comments attachment logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. Comment attachment currently could only be invoked at finishNode, and it will definitely misclassify some inner comments as long as there are multiple whitespaces between the end location of any two nodes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, looks like something like #9084 is needed to overhaul comment parsing properly, but there might be challenges around backwards compatibility.

I'm slightly more inclined to keep them as trailingComments on Foo, because I expect there to be more examples of trailing comments before the } than after, so I'll leave this in.

if (this.state.leadingComments.length === 0) {
return;
}
if (!this.state.commentPreviousNode) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this.state.commentPreviousNode will always be non-empty as long as elements is not empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah good point.

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.

Thanks

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 3, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 3, 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 PR: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants