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

Support OXC estree flavour #15

Open
Zn4rK opened this issue Sep 20, 2023 · 2 comments
Open

Support OXC estree flavour #15

Zn4rK opened this issue Sep 20, 2023 · 2 comments

Comments

@Zn4rK
Copy link

Zn4rK commented Sep 20, 2023

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
const helloWorld = "Hello World";
{
  "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
{
"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
Copy link
Owner

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

@Zn4rK
Copy link
Author

Zn4rK commented Oct 4, 2024

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.

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

No branches or pull requests

2 participants