Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel plugin (Visitor pattern) - How it works

I want to do two replacements in my babel plugin. And second replacement should only happen after first one is done.

module.exports = function(babel) {
    const t = babel.types;
    return {
        visitor: {
            FunctionExpression: function(path) {
                //Conversion to arrow functions
                path.replaceWith(t.arrowFunctionExpression(path.node.params, path.node.body, false));
            },
            ThisExpression: function(path) {
                //Converting all this expressions to identifiers so that it won't get translated differently
                path.replaceWith(t.identifier("this"));
            }
        }
    };
}

In the AST tree of my "FunctionExpression" the "ThisExpression" exists somewhere down the tree. I want first conversion to happen only after the second conversion is done. How do I achieve this.?

like image 367
Aftab Khan Avatar asked Oct 20 '25 12:10

Aftab Khan


1 Answers

I figured it out. Best place to understand how to write babel plugins. Here

module.exports = function(babel) {
    const t = babel.types;
    return {
        visitor: {
            FunctionExpression: {
                enter: function(path) {
                    path.traverse(updateThisExpression);
                    //Conversion to arrow functions
                    let arrowFnNode = t.arrowFunctionExpression(path.node.params,
                        path.node.body, false);
                    path.replaceWith(arrowFnNode);
                }
            }
        }
    };
}

const updateThisExpression = {
    ThisExpression: {
        enter: function(path) {
            //Converting all this expressions to identifiers so that
            //it won't get translated differently
            path.replaceWith(t.identifier("this"));
        }
    }
};

You write another visitor object which you use to traverse within the "FunctionExpression" visitor.. ;)

like image 69
Aftab Khan Avatar answered Oct 22 '25 01:10

Aftab Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!