The code we want to trasform:
2 ** 3; a ** b; a **b * c; a ** b ** c; (a+1) ** (b+1);
transform to:
Math.pow(2, 3); Math.pow(a, b); Math.pow(a, b) * c; Math.pow(a, Math.pow(b, c)); Math.pow(a+1, b+1);
Code:
export default function (babel) { const { types: t } = babel; return { name: "ast-transform", // not required visitor: { BinaryExpression (path) { console.log(t ) const isPowOpreator = path.node.operator === '**'; if(!isPowOpreator) { return; } const {left, right} = path.node; path.replaceWith(t.callExpression( // create Math.pow() node t.memberExpression( t.identifier('Math'), t.identifier('pow') ), [left, right] // pass in Math.pow(left, right) )) } } }; }