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

Babel-Types Alias for All Nodes #11252

Open
aherlihy opened this issue Mar 12, 2020 · 4 comments
Open

Babel-Types Alias for All Nodes #11252

aherlihy opened this issue Mar 12, 2020 · 4 comments

Comments

@aherlihy
Copy link

@aherlihy aherlihy commented Mar 12, 2020

Feature Request

  • I would like to work on this feature!

Is your feature request related to a problem? Please describe.
I need to do type inference in a custom babel plugin in and in order to determine the type I need a visitor method that will be called for every non-leaf node (but for simplicity am asking for a method that is called for every node). Right now it looks like the closest thing is Expression but that doesn't apply to every node. Apologies if this exists, I could not find anything.

Describe the solution you'd like
An alias, maybe called "Node" that will be called for every node in the tree when visiting. No clear drawbacks as long as it's documented clearly so it doesn't lead to overuse and slow code.

Teachability, Documentation, Adoption, Migration Strategy
Would also make pretty-printing the AST very simple.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Mar 12, 2020

Hey @aherlihy! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@aherlihy

This comment has been minimized.

Copy link
Author

@aherlihy aherlihy commented Mar 12, 2020

I realize you can call exit and it will call for each node, apologies for the unnecessary issue

@aherlihy aherlihy closed this Mar 12, 2020
@aherlihy aherlihy reopened this Mar 13, 2020
@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Mar 13, 2020

You can also use enter instead of exit: enter is the default behavior for visitors, and would match the hypothetical Node visitor.

{
  enter(path) { ... }
}

// is the same a what this would do

{
  Node(path) { ... }
}

While

{
  exit(path) { ... }
}

// is the same a what this would do

{
  Node: { exit(path) { ... } }
}
@aherlihy

This comment has been minimized.

Copy link
Author

@aherlihy aherlihy commented Mar 13, 2020

Thank you! However I need to visit the children in depth-first postfix order, i.e. I need to make sure all the leaves are visited before I visit any parents. If I use enter, won't it do it in prefix order so the parent nodes' enter method would be called before the childrens' enter method?

The issue I'm having with using exit is that the exit method is called before the node-specific visitor exit method. What I want to do in the generic all-nodes method needs to happen instead of, or at least after, the defined visitor exit methods.

What I'm trying to accomplish is a bit of type inference, so I would decorate certain leaf nodes with a type (my custom type, not babel type) within the node-specific visitor methods. Then for each generic node, I would check if the custom type of the child is defined and if so, then decorate that node with the type. That way my tree in the end will have some additional type information for each visited node that I can use for my translations, and I can avoid doing extra passes to figure out what the custom types of the children are from the parent method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.