swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.15k stars 1.23k forks source link

Provide AST count information without a separate visitor pass #9401

Open DavidHancu opened 2 months ago

DavidHancu commented 2 months ago

Describe the feature

When implementing a library that builds on top of SWC, we need to generate intermediary representations. This probably includes HashMaps or Vecs, which can benefit from a performance increase when initialized with the right capacity.

SWC should provide at least the following counts:

Babel plugin or link to the feature description

No response

Additional context

This should not be implemented as a separate pass because it would be wasteful. This should be implemented at the parser level and I'm willing to contribute this.

kdy1 commented 2 months ago

I think this is a good idea, but I'm not sure where we should store the metadata, and I'm not sure about the API, either.

DavidHancu commented 2 months ago

I think that a new function on the Parser could do: parse_module_with_metadata. This would return a struct with a Module field and a Metadata field. In the parser implementation, this would use a const boolean generic to ensure that the compiler optimizes the metadata creation out if it wasn't requested