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
and CommentLine
are not part of the program.body
in the ast
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 add CommentLine
and CommentBlock
.
The comments for a node can be accessed, using traverse
, as follows:
traverse(ast, {
ClassDeclaration(path) {
console.log(path.node.leadingComments);
console.log(path.node.trailingComments);
},
});
Seems like you can't traverse that way but you can access comments with:
nodes.comments
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.