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

File comment is lost when removing first statement #44

Open
sophiebits opened this issue Aug 23, 2015 · 8 comments
Open

File comment is lost when removing first statement #44

sophiebits opened this issue Aug 23, 2015 · 8 comments

Comments

@sophiebits
Copy link

If you have

/**
 * @providesModule Foo.react
 */

var React = require('React');

and remove the require statement, the docblock is lost. This causes build errors and I had to manually fix up a bunch of files with the last codemod I wrote to restore these.

@fkling
Copy link
Contributor

fkling commented Aug 23, 2015

Mmh. I assume the comment is attached to the VarableDeclaration, so when you remove the node, the comment is removed as well. You could save the comment first and reattach it to the file node: http://felix-kling.de/esprima_ast_explorer/#/kGspd21ZnD/1.

Maybe we can do something to make this easier, but not sure yet what.


We should definitely provide an API that makes it easy to get, add and remove comments on nodes.

@sophiebits
Copy link
Author

Yeah, I figured as much. Maybe we could just special-case the first comment? It seems rare that you'd ever want to move or drop it (especially at FB).

@udnisap
Copy link
Contributor

udnisap commented Dec 19, 2015

I can work on it to detach the first comment a file from the first statement and add as a node. Is this desirable?

@DrewML
Copy link
Contributor

DrewML commented Jun 21, 2016

@fkling What are your thoughts on adding a recipes section to the documentation (similar to this). I've found a few examples of use-cases in the past digging through the issues on this repo that would have been great to have documented.

I know there are already links to the codemod repos in the bottom of the readme, but specific recipes could be an added bonus.

@fkling
Copy link
Contributor

fkling commented Jun 21, 2016

@DrewML: Sounds like an excellent idea to me!

@slorber
Copy link
Contributor

slorber commented Jan 9, 2017

Hey,

By chance could someone take a look at this issue?

5to6/5to6-codemod#18

The problem here for me is that sometimes the codemod does try to restore the leading comment, but that leading comment was not removed in the first place, leading to a duplicate leading comment.

I'm not a JSCodeShift/Recast expert but couldn't we make sure the actual leading comment gets removed before restoring it, or restoring it only when necessary?

@ptbrowne
Copy link
Contributor

ptbrowne commented Jan 11, 2017

@slorber Yes it seems that if the first node was not modified, the first comment would be duplicated.

The solution is to check if the first node has changed : http://astexplorer.net/#/hb6iLO9hTe/48

Modify "underscore" to anything else (like "lodash") in the original source code so that the node is not removed and you will see that the comment is not duplicated.

@danielo515
Copy link

What is the rationale to attach comments to other nodes? Why can't they live as independent nodes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants