Sunday, June 10, 2018

Can I prevent Babel from traversing code inserted by a plugin?

Leave a Comment

I'm building a plugin that inserts enterFunction() in front of every existing function call by calling path.insertBefore. So my code is transformed from:

myFunction(); 

To:

enterFunction(); myFunction(); 

The problem is that when I insert the node Babel once again traverses the inserted node. Here's the logging output:

'CallExpression', 'myFunction'
'CallExpression', 'enterFunction'

How can I prevent Babel from entering the enterFunction call expression and its children?

This is the code I'm currently using for my Babel plugin:

function(babel) {     return {         visitor: {             CallExpression: function(path) {                 console.log("CallExpression", path.node.callee.name)                 if (path.node.ignore) {                     return;                 }                 path.node.ignore = true                  var enterCall = babel.types.callExpression(                     babel.types.identifier("enterFunction"), []                 )                 enterCall.ignore = true;                 path.insertBefore(enterCall)             }         }     } } 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment