Skip to content

estree codegen #11495

@leaysgur

Description

@leaysgur

Background

Boshen:
I wonder whether we should start forking and combining things to get things printed correct on the js side 😰
To complete the user story for oxc-parser-> transfer to js -> modify ast -> print
https://discord.com/channels/1079625926024900739/1080712024683708466/1379710015539249273

I happened to be researching sveltejs/esrap for another task, so we thought it might be a good choice if it could be integrated easily.

Prerequisite

oxc-parser currently produces:

  • ESTree AST for JavaScript
  • TS-ESTree AST for TypeScript

And it also supports JSX and several Stage 3 proposals.

How could we implement this?

At first I planned to use esrap as an optionalDependency of oxc-parser.

However, after trying it I found that:

  • It does NOT support JSX
  • It even fails to print simple TS code such as let a: string | null = null
    • Some of them have been fixed recently, but it still has missing
  • We would also need to attach comments to nodes manually

Another issue is that sveltejs/esrap expects the AST produced by sveltejs/acorn-typescript, which is similar to @typescript-eslint/typescript-estree(oxc-parser outputs), but it seems slightly different.

Look for other implementations?

In short, the most robust way to turn a (TS-)ESTree AST into code today is Prettier. 😅

But if we choose Prettier, the main question is runtime performance? And it might be too much(too pretty) for this use-case.

If we do not choose Prettier, we could:

  • Fork(or use extend API if possible?) esrap, astring or estree-util-to-js
  • Add TS and|or JSX support
  • Update comment attaching logic

Of course, the best option depends on how much effort we want to invest, but in any case Prettier will likely give more intuitive results than the alternatives.

However, if the comments do not have to be handled so strictly, performance seems to be better with these.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory - New feature or requestE-Help WantedExperience level - For the experienced collaboratorsneeds-discussionRequires a discussion from the core team

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions