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

How do I use visitor in oxc-parser using typescript ? #6244

Closed
rizrmd opened this issue Oct 2, 2024 · 5 comments
Closed

How do I use visitor in oxc-parser using typescript ? #6244

rizrmd opened this issue Oct 2, 2024 · 5 comments
Assignees
Labels
C-enhancement Category - New feature or request

Comments

@rizrmd
Copy link

rizrmd commented Oct 2, 2024

Provided example is using rust, how do I do it in typescript/js ?

@rizrmd rizrmd added the C-enhancement Category - New feature or request label Oct 2, 2024
@overlookmotel
Copy link
Contributor

overlookmotel commented Oct 2, 2024

Currently Oxc is focused on providing JS APIs based on "source text in, source text out". i.e. all AST manipulation occurs on Rust side.

For this reason, the oxc-parser npm package does not currently provide a visitor. It will do in future, but it's going to take a while - it's more complicated than it sounds.

Of course, you can write JS/TS code to traverse/explore the AST in whatever way you would like, but we don't provide an "out of the box" AST visitor (yet).

The JS-side AST almost entirely aligns with ESTree, so you could try using another library which provides an ESTree AST visitor/transformer. However, the AST does not align exactly with ESTree at present - it is our intent that it will (#2854), but we're not there yet. There are also currently no TS type definitions for Oxc's AST (again, more difficult than it sounds - good Rust-JS interop is quite a hard problem).

Sorry that's not the answer that you want, but I hope this info helps somewhat.

@Boshen
Copy link
Member

Boshen commented Oct 2, 2024

@rizrmd where have you looked and what is lacking from our current documentation?

@Boshen Boshen reopened this Oct 2, 2024
@rizrmd
Copy link
Author

rizrmd commented Oct 2, 2024

@rizrmd where have you looked and what is lacking from our current documentation?

Mainly here: https://oxc.rs/docs/guide/usage/parser.html
I did search issues, but most of them are discussing rust based visitor.

Also, there are prior works from swc like woodpile and swc-walk.

  • Woodpile use wasm-pack to call rust visitor using js.
  • swc-walk wraps acorn-walk using js custom visitor that conforms with swc terms.

Both are good enough, the problem is the size in wasm.
swc takes +/- 15-20mb uncompressed, while oxc only 600kb. I am impressed!

I tried to parse oxc result using swc-walk, surprisingly it works for simple code. But there are some differences in node.type, and several node properties. I just add/modify them to works, but I think this is still a hack...

@rizrmd
Copy link
Author

rizrmd commented Oct 2, 2024

Currently Oxc is focused on providing JS APIs based on "source text in, source text out". i.e. all AST manipulation occurs on Rust side.

For this reason, the oxc-parser npm package does not currently provide a visitor. It will do in future, but it's going to take a while - it's more complicated than it sounds.

Of course, you can write JS/TS code to traverse/explore the AST in whatever way you would like, but we don't provide an "out of the box" AST visitor (yet).

The JS-side AST almost entirely aligns with ESTree, so you could try using another library which provides an ESTree AST visitor/transformer. However, the AST does not align exactly with ESTree at present - it is our intent that it will (#2854), but we're not there yet. There are also currently no TS type definitions for Oxc's AST (again, more difficult than it sounds - good Rust-JS interop is quite a hard problem).

Sorry that's not the answer that you want, but I hope this info helps somewhat.

No worries, understandable. Thanks for explaining.

@Boshen
Copy link
Member

Boshen commented Oct 2, 2024

👍 so the question is regarding visitor in js, and we lack one. We need to document as such. Thank you for the input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category - New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants