Skip to content

Q: How to manipulate nodes+parent_nodes in AST & generate the tree back to JS code? #3254

Answered by RiESAEX
ShayRubach asked this question in Q&A
Discussion options

You must be logged in to vote
struct MyVisitor;
impl swc_ecma_visit::VisitMut for MyVisitor {
    fn visit_mut_call_expr(&mut self, expr: &mut CallExpr) {
        expr.visit_mut_children_with(self);
        match &mut expr.callee { 
            Callee::Expr(callee) => match &mut **callee { // callee is &mut Box<Expr>
                Expr::Ident(Ident { sym, .. }) => {
                    if sym.to_string() == "Foo" {
                        *expr = CallExpr {
                            span: DUMMY_SP,
                            callee: swc_ecma_ast::Callee::Expr(callee.take()), // here we want a Box<Expr>, and callee will not use in other place, so we take() it, we got a Box<Expr>,and the origin memory of callee wil…

Replies: 3 comments 18 replies

Comment options

You must be logged in to vote
9 replies
@ShayRubach
Comment options

@RiESAEX
Comment options

@ShayRubach
Comment options

@RiESAEX
Comment options

@ShayRubach
Comment options

Comment options

You must be logged in to vote
1 reply
@ShayRubach
Comment options

Answer selected by kdy1
Comment options

You must be logged in to vote
8 replies
@RiESAEX
Comment options

@ShayRubach
Comment options

@kdy1
Comment options

@ShayRubach
Comment options

@kdy1
Comment options

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