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

ESTree compatible AST #2123

Open
sosukesuzuki opened this issue Aug 21, 2021 · 17 comments
Open

ESTree compatible AST #2123

sosukesuzuki opened this issue Aug 21, 2021 · 17 comments

Comments

@sosukesuzuki
Copy link
Contributor

Describe the feature

Support ESTree compatible AST.

Some options:

  • SWC output ESTree compatible AST directly.
  • New tool like swc-to-estree

Babel plugin or link to the feature description

Nothing.

Additional context
Add any other context about the problem here.

I'm a maintainer of Prettier Code Formatter and I'm developing to use SWC as a parser for Prettier (Disclaimer: It is a 100% personal activity and not affiliated with any organization.) Prettier already has a code base to handle ESTree compatible ASTs well. So, if SWC supports ESTree, it will be easier. Also, ESTree is the de facto standard for JavaScript ASTs, and by supporting it, many other tools may be able to use SWC.

related: #1392

@sosukesuzuki
Copy link
Contributor Author

@kdy1 What do you think about this?

@jdalton
Copy link

jdalton commented Aug 23, 2021

@sosukesuzuki Just a heads up, the parse APIs are being deprecated. But I've asked if there'll be a way to support this scenario in the future as I've seen some perf improvements on my end (replacing acorn with swc+ little AST adapters).

@kdy1
Copy link
Member

kdy1 commented Aug 23, 2021

I made a mistake. parse will be preserved to help some tools like ast explorer.

@sosukesuzuki
Copy link
Contributor Author

Is it expected that third party libraries will only use the swc parser?

@kdy1
Copy link
Member

kdy1 commented Aug 23, 2021

No, but rust <-> js is very slow and it overwhelms time used for actual work.

@sosukesuzuki
Copy link
Contributor Author

I see. Out of curiosity, you are doing integration SWC and Next.js at vercel? At there, how to integrate Rust land and JS land?

@kdy1
Copy link
Member

kdy1 commented Aug 23, 2021

Yes. We are working on it. We are going to reimplement all transforms in rust, and call transform() from js.

@sosukesuzuki
Copy link
Contributor Author

I found https://github.com/vercel/next.js/tree/canary/packages/next/build/swc. It looks good, thanks

@sosukesuzuki
Copy link
Contributor Author

sosukesuzuki commented Aug 24, 2021

@kdy1 (The main topic of this issue) What do you think about making swc's AST consistent with ESTree, which is the de facto standard?

@kdy1
Copy link
Member

kdy1 commented Aug 24, 2021

What's the advantage if I make it compatible with estree? Rust does not have GC, and estree-like ast will degrade performance.

@sosukesuzuki
Copy link
Contributor Author

I did not know about the performance drawbacks. The advantage is that it is much easier for existing tools such as ESLint and Prettier to use swc as a parser.

@kdy1
Copy link
Member

kdy1 commented Aug 24, 2021

I see. But actually, passing ast to javascript from rust is very slow. Parser itself is fast, but json serialization/deserialization is the problematic part, and I'm not sure about the way to make it fast.
Maybe lazy accessors can be a solution, but I didn't try it.

@jdalton
Copy link

jdalton commented Aug 24, 2021

If you provide the string the user can do things like fast-json. This allows for them to choose which serializer (JSON.parse or otherwise) to use.

@kdy1
Copy link
Member

kdy1 commented Aug 24, 2021

@jdalton If it's fast enough, I think we can support babel plugins. So I think it is worth benchmarking.

@jdalton
Copy link

jdalton commented Aug 24, 2021

As a test of raw parse power I tried parsing babylon.max.js which s 10.6MB of JS.
It takes acorn ~700ms and swc ~2 seconds, so I can see what you mean about the Rust->JS transition.

@overlookmotel
Copy link
Contributor

Please see #2175 if you're interested. I've done some experiments into speeding up parse().

@vjpr
Copy link

vjpr commented Sep 30, 2021

@jdalton (replacing acorn with swc + little AST adapters)

Could you share this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants