More flexibility for on-exit logic and state
dummdidumm opened this issue · 1 comments
Calling next
only works when one single node calls it. Things get out of desired order when _
calls next
but a named node, does, too.
To work around this - and in order to support most cases of merged visitors - I propose to special-case returning a function from the AST. It's not interpreted as a transformed node, instead it's interpreted as "this function runs when walking out of this node again". The alternative would be to push the function to a new provided method onExit
.
walk(node, {}, {
Foo(node) {
// stuff on the way in
return () => // stuff on the way out
},
// or
Bar(node, { onExit }) {
// stuff on the way in
onExit(() => // stuff on the way out
}
To also support the case where merged visitors want to set state for the inner nodes, a new method nextState
(subject to bikeshedding) should be provided which essentially does what next
does but without calling visit
right away and without erroring if it's called multiple times (states would be merged).
Closing as it's most of the time the desired behavior to have _
influence the static visitor, and for merging it's possible to implement it without new capabilities.