jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

Is there proper way to remove all comments?

Open glebcha opened this issue 5 years ago • 4 comments

Now I have to iterate comment nodes and modify values, but comment's signature is staying untouched. In:

// leading_comment
const definition = 'string'
/* block_comment */

Modification:

const j = api.jscodeshift
const root = j(file.source)

root.find(j.Comment).forEach(comment => (comment.value.value = ''))

Out:

//
const definition = 'string'
/**/

Expected:

const definition = 'string'

glebcha avatar Dec 05 '18 01:12 glebcha

This works:

return j(file.source).find(j.Comment).forEach(path => path.prune()).toSource();

https://astexplorer.net/#/gist/1ac239fc11f0c274540e680420313c34/d4a78b1e7ff1eff4f1828e939e545bbf78fde1aa

I was hoping that j(file.source).find(j.Comment).remove().toSource(); but it looks like remove is only defined for Nodes and a comment is not a Node. Maybe we can make it work though.

fkling avatar Dec 05 '18 02:12 fkling

This works:

return j(file.source).find(j.Comment).forEach(path => path.prune()).toSource();

Sorry, but with flow parser it became invalid line like export type type Notice = {}

glebcha avatar Dec 10 '18 10:12 glebcha

If this is still an issue for you, could you provide a concrete flow example for which this breaks?

fkling avatar Apr 03 '19 18:04 fkling

@fkling

This works:

return j(file.source).find(j.Comment).forEach(path => path.prune()).toSource();

https://astexplorer.net/#/gist/1ac239fc11f0c274540e680420313c34/d4a78b1e7ff1eff4f1828e939e545bbf78fde1aa

I was hoping that j(file.source).find(j.Comment).remove().toSource(); but it looks like remove is only defined for Nodes and a comment is not a Node. Maybe we can make it work though.

Although, it is working fine, but there is TS error: Argument of type  Type<Comment>  is not assignable to parameter of type  Type<ASTNode>, and the interesting moment is when instead of j.Comment I try to use j.CommentBlock/j.CommentLine it doesnt remove comments.

Vadko avatar Nov 20 '23 14:11 Vadko