oxc-project / oxc

⚓ A collection of JavaScript tools written in Rust.
https://oxc.rs
MIT License
12.29k stars 446 forks source link

feat(parser,codegen): print comment #1046

Closed Boshen closed 3 weeks ago

Boshen commented 1 year ago

Research

After studying babel, esbuild and prettier, I'm leaning towards storing extra data for linking ast node and comments, the data is processed and saved in the parser.

References:

We have the following requirements:


Proposal

  1. Port https://github.com/babel/babel/blob/main/packages/babel-parser/ast/comment-attachment.md
  2. Add a storage in Trivias for associating ast nodes, preferably AST node ID (we can use the allocator address API today) to ASTKind
Boshen commented 8 months ago

https://github.com/evanw/esbuild/blob/cc74e6042a9f573bf58e1e3f165ebda70af4ad3b/internal/js_printer/js_printer.go#L281

SKT1yang commented 2 months ago

Is it possible to configure parsing and retain all comments

ArnaudBarre commented 1 month ago

I'll need at least the esbuild behaviour to make the transformer a viable Vite plugin!

airhorns commented 1 month ago

Just to weigh in a little to help prioritize -- without comments, the isolatedDeclarations transform is not super useful in its current form. I think anyone building.d.ts files for consumption intends for them to be consumed by humans, like .d.ts files within a node modules that power typechecking and autocomplete. Stripping all the nice documentation or explanation about these files makes those artifacts far far less useful for humans, and much different than the ones that the TypeScript compiler itself would produce.

I know this is probably a super annoying and invasive change to make to oxc but as someone who adopted and then had to un-adopt the isolatedDeclarations transform because of this missing piece, it'd be really great to get actual drop in compatibility with tsc!

Boshen commented 3 weeks ago

Codegen now prints useful comments such as jsdoc, vite / webpack specials, pure annotations.

The remaining issue is print all comments, which is really difficult, I'll revisit them when requirements arises. Close as finished in the meantime.

ArnaudBarre commented 3 weeks ago

Yeah you can get to at least 38m downloads without printing comments 😁