estree-toolkit icon indicating copy to clipboard operation
estree-toolkit copied to clipboard

Support OXC estree flavour

Open Zn4rK opened this issue 1 year ago • 2 comments

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

Zn4rK avatar Sep 20 '23 21:09 Zn4rK

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

sarsamurmu avatar Aug 10 '24 18:08 sarsamurmu

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.

Zn4rK avatar Oct 04 '24 11:10 Zn4rK