sarsamurmu / estree-toolkit

Tools for working with ESTree AST
https://estree-toolkit.netlify.app/
MIT License
54 stars 1 forks source link

Support OXC estree flavour #15

Open Zn4rK opened 1 year ago

Zn4rK commented 1 year ago

Another fast ESTree parser is oxc.

https://github.com/web-infra-dev/oxc#-ast-and-parser

However the generated ESTree AST slightly differs:

The Oxc AST differs slightly from the estree AST by removing ambiguous nodes and introducing distinct types. For example, instead of using a generic estree Identifier, the Oxc AST provides specific types such as BindingIdentifier, IdentifierReference, and IdentifierName. This clear distinction greatly enhances the development experience by aligning more closely with the ECMAScript specification.

But it would be pretty awesome if it was supported!

The additional distinct types is quite a big change I would think:

OXC ESTree AST ```js const helloWorld = "Hello World"; ``` ```json { "type": "Program", "start": 0, "end": 34, "sourceType": { "language": { "typeScript": { "isDefinitionFile": false } }, "moduleKind": "module", "variant": "standard", "alwaysStrict": false }, "directives": [], "hashbang": null, "body": [{ "type": "VariableDeclaration", "start": 0, "end": 33, "kind": "const", "declarations": [{ "type": "VariableDeclarator", "start": 6, "end": 32, "id": { "type": "BindingPattern", "kind": { "type": "BindingIdentifier", "start": 6, "end": 16, "name": "helloWorld" }, "typeAnnotation": null, "optional": false }, "init": { "type": "StringLiteral", "start": 19, "end": 32, "value": "Hello World" }, "definite": false }], "modifiers": null }] } ```
Regular ESTree AST ```json { "type": "Program", "start": 0, "end": 33, "body": [ { "type": "VariableDeclaration", "start": 0, "end": 33, "declarations": [ { "type": "VariableDeclarator", "start": 6, "end": 32, "id": { "type": "Identifier", "start": 6, "end": 16, "name": "helloWorld" }, "init": { "type": "Literal", "start": 19, "end": 32, "value": "Hello World", "raw": "\"Hello World\"" } } ], "kind": "const" } ], "sourceType": "module" } ```

I haven't found another library for traversing a OXC compatible ESTree in Javascript yet.

The node types are available in https://github.com/web-infra-dev/oxc/tree/main/crates/oxc_ast/src

sarsamurmu commented 2 months ago

Aren't there performance overhead in FFIs? As far as I know oxc uses napi-rs to communicate between rust and js.

Zn4rK commented 2 weeks ago

There's a performance overhead, but parsing a big document with OXC is still faster than what I do today. I have a transformer implemented in SWC, but I would like to replace that with JS instead. My initial experiments tell me that I can shave of a few ms by doing it this way.