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

It seems we can't add a comment at the bottom of the target node? #547

Open
Indomi opened this issue Mar 3, 2023 · 2 comments
Open

It seems we can't add a comment at the bottom of the target node? #547

Indomi opened this issue Mar 3, 2023 · 2 comments

Comments

@Indomi
Copy link

Indomi commented Mar 3, 2023

with babylon parser

I can use j.commentLine() api to add a comment for node, but the result is, I can only control the comment with related code in the same line or before the code, if I need to add comments at the bottom of the node, it seems I can only find the siblings node which is after the target, and add comment to it?

hope to receive reply, thanks!

@ElonVolo
Copy link
Collaborator

The Recast library, which is responsible for printing the transformed output from jscodeshift, is limited in what it can do with comments. I'm guessing that this is what you're running into.

@Daniel15
Copy link
Member

Comments are tricky since "trivia" (whitespace and comments) are not actually part of a normal AST. Different systems represent it differently - sometimes you need a "full syntax tree" or "lossless syntax tree" to get the trivia, other times it's hacked into the AST as a property of a node (e.g. nodes own the comments directly above them, and all the whitespace after them until the next node).

I think with jscodeshift and the libraries it uses, the comments are there but nodes only own the comments above them, so you'd have to find the next sibling node and add a comment to it.

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

No branches or pull requests

3 participants