How do I traverse comments with babelTraverse
?
babelTraverse(nodes, {
CommentBlock: (path) => {
console.log(path)
},
CommentLine: (path) => {
console.log(path)
}
})
Error: You gave us a visitor for the node type CommentBlock but it's not a valid type
The
CommentBlock
andCommentLine
are not part of theprogram.body
in theast
returned by the babel parser. These comment types live outside of the program body. I am assuming that is why we get the Type error when we addCommentLine
andCommentBlock
.The comments for a node can be accessed, using
traverse
, as follows: