Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
overlookmotel opened this issue Jun 11, 2024 · 5 comments · Fixed by #4684
Closed

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

overlookmotel opened this issue Jun 11, 2024 · 5 comments · Fixed by #4684
Assignees
Labels
P-high Priority - High

Comments

@overlookmotel
Copy link
Contributor

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.

  • If scope starts after some fields, scope is entered after enter_* and exited before exit_*.
  • If scope covers whole node, scope is entered before enter_* and exited after exit_*.

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:

  • Always enter the scope after enter_*.
  • Always exit the scope before exit_*.

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

@overlookmotel overlookmotel self-assigned this Jun 11, 2024
@Dunqing
Copy link
Member

Dunqing commented Jul 11, 2024

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

@Dunqing Dunqing transferred this issue from oxc-project/backlog Jul 11, 2024
@overlookmotel
Copy link
Contributor Author

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
Copy link
Contributor

rzvxa commented Jul 11, 2024

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
Copy link
Contributor Author

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.

Boshen pushed a commit that referenced this issue Jul 12, 2024
I'm going to be AFK today(till about 9 PM UTC). Meanwhile, I Didn't want to be a blocker so here we go.
It would fix the #4200 merge if you guys find it in the correct order otherwise feel free to close it.
@Dunqing Dunqing added the P-high Priority - High label Aug 6, 2024
Dunqing pushed a commit that referenced this issue Aug 6, 2024
Closes #4200.

Align `Traverse`'s behavior with `Visit` and `VisitMut`. For types with scopes, call `enter_*` before entering scope, and call `exit_*` after exiting scope.
@overlookmotel
Copy link
Contributor Author

Closed in #4684.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-high Priority - High
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants