I would like to write a Babel plugin that translates a block-statement as first argument to function foo
into an arrow function body.
I would like to transform
foo({var a = 1;})
into
foo(() => {var a = 1;})
I tried the following:
visitor: {
CallExpression(path) {
if (path.node.callee.name == 'foo') {
if (path.node.hasOwnProperty('_alreadyVisited')) return;
path.replaceWith( t.callExpression( path.node.callee,
[t.arrowFunctionExpression([], path.node.arguments[0])] ) );
path.node['_alreadyVisited'] = true;
}
}
}
But it seems my plugin rule is not even considered, as Babel.transform already fails before with:
Uncaught SyntaxError: unknown: Unexpected keyword 'var'
Any ideas would be greatly appreciated.