Transforming $.now
CallExpression
node
1 step: find a Visitor:
visitor: {
CallExpression(path) {
// …
}
}
CallExpression
is $.now
2 step: ensure that the Input:
/*
$.now();
^ ^^^
*/
Visitor:
const { callee, arguments: args } = path.node;
if (!t.isMemberExpression(callee)) {
return;
}
if (
callee.object.name === "$" &&
callee.property.name === "now"
) {
// yes …
}
$.now
into Date.now
3 step: transforming For simplicity we can directly mutate the callee
object.
Diff:
-$.now();
+Date.now();
Visitor:
// Rename $.now to Date.now
callee.object = t.identifier("Date");