I'm trying to write a simple babel plugin, but I am having a hard time traversing a matched node with a nested visitor. I would like to find all require
calls in a module that require a certain module and then apply some transformation in the same scope.
To illustrate this with a contrived example I'd like to transform source code like:
const f = require('foo-bar');
const result = f() * 2;
into something like:
const result = 99 * 2; // as i "know" that calling f will always return 99
I was trying to do the following:
module.exports = ({ types: t }) => ({
visitor: {
CallExpression(path) {
if (path.node.callee.name === 'require'
&& path.node.arguments.length === 1
&& t.isStringLiteral(p.node.arguments[0])
&& path.node.arguments[0].value === 'foo-bar'
) {
const localIdentifier = path.parent.id.name;
// if i print here it will show me that it successfully
// found all require calls
p.scope.traverse({
Identifier(subp) {
// this will never run at all
if (subp.name === localIdentifier) {
console.log('MATCH!');
}
}
});
}
}
}
});
Is my approach flawed or is there something I need to do differently from a code perspective?