oxc-project / oxc

⚓ A collection of JavaScript tools written in Rust.
https://oxc.rs
MIT License
10.53k stars 387 forks source link

`Traverse` enter scope after `enter_*`, exit before `exit_*` #4200

Closed overlookmotel closed 1 month ago

overlookmotel commented 2 months ago

Currently Traverse's timing of entering and exiting scopes is different for different node types. It depends on whether the scope covers all fields, or only a subset.

This means enter_* and exit_* for any node are always called with same scope at top of the scope stack. Good.

But the fact that current scope in enter_* and exit_* is different depending on the node makes it confusing.

Change this to be consistent for all node types:

If they need it, enter_* / exit_* can get the node's ScopeId from the node's scope_id field.

Dunqing commented 1 month ago

Currently, I want to use current_node_id to do something in leave_scope. But leave_scope after leave_node. This means that I can't get the node id corresponding to the scope

ast-codegen also needs to follow this. @rzvxa cc

overlookmotel commented 1 month ago

I want to use current_node_id to do something in leave_scope. But leave_scope after leave_node.

That's a different issue from this. Traverse doesn't have enter_scope, leave_scope, enter_node or leave_node.

rzvxa commented 1 month ago

Currently, I want to use current_node_id to do something in leave_scope. But leave_scope after leave_node. This means that I can't get the node id corresponding to the scope

ast-codegen also needs to follow this. @rzvxa cc

Previously we were mostly visiting scope before nodes that's why I made that the default of our generated Rust code. But I have to say it makes more sense to fire off the scope events inside of node events.

Does this order work for you: enter_node -> enter_scope -> walk -> leave_scope -> leave_node If it does let me know so I can submit a PR for it.

overlookmotel commented 1 month ago

Note to self: When making this change to Traverse, will need to alter some of the transforms which rely on ctx.current_scope_id() being the scope of the current node - whereas after this change it'll be the parent.

Take care - no tests for scopes in transformer at present, so missing something could silently break things.

Probably should rename it parent_scope_id to reflect this.

overlookmotel commented 1 month ago

Closed in #4684.