estree-toolkit
estree-toolkit copied to clipboard
Can I prevent CallExpression.arguments being traversed?
In a visitor, I can do something like
CallExpression(path) {
path.get('callee').skip();
}
but since arguments
isn't a node,
path.get('arguments').skip()
doesn't return something I can use this way
path.get('arguments')
returns an array so you would have to run .skip()
on every element of the array like
path.get('arguments').forEach(p => p.skip())
Aha, super!
Is there a way to pass a specific state into one branch? Or is state always global for the visitor?
You can use NodePath.traverse
function in that case
I was stuck on a misunderstanding of how skip/travers/replaceWith interacted. I had something like this:
const { traverse, builders: b } = require('estree-toolkit');
const { parseModule } = require('meriyah');
const ast = parseModule('type(argument)')
traverse(ast, {
CallExpression: { enter(path) {
path.get('arguments').forEach(a => {
a.traverse({
Identifier: { enter(path) {
path.replaceWith(b.literal(path.node.name))
}},
})
a.skip()
})
}},
Literal: { enter(path) {
console.log('main:', path.node.value)
}},
})
and was surprised Literal
was reached even though I had called skip
, but the replaceWith
builds a path (I think) that wasn't marked with the skip
. Calling
path.replaceWith(b.literal(path.node.name)).skip()
got me what I wanted.