Skip to content

Is there a way to add or remove nodes in the source code? #4970

Closed Answered by overlookmotel
SKT1yang asked this question in Q&A
Discussion options

You must be logged in to vote

Usually you'd use oxc_ast::visit::VisitMut for mutating AST (oxc_traverse::Traverse would also work, but that crate's API is likely to be reworked considerably in near future, and is only really intended for internal use within Oxc at present).

But for your simple use case, either of those is overkill. I'd suggest just parsing using oxc_parser, and then insert whatever nodes you want onto start of program.body. Please see the parser example, and then you can create new nodes using oxc_ast::AstBuilder.

If you then need to print the AST back to JS text, see codegen example.

Hope that helps.

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by SKT1yang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants