Does SWC remove comments in the code when transforming a JS file's AST and converting it back to a c

Hello friends, I'm quite new to Rust. Recently, I used OXC in my Elixir project with NIF to modify JS files in my CLI. Unfortunately, after making changes to the JS file, all the comments above functions or within the code are removed, which is a major issue for me. Before diving into SWC, I wanted to ask if this library exhibits the same behavior. My next question is whether SWC supports converting the entire AST to JSON and vice versa, particularly from JSON back to AST. My current requirement is focused on JS files. Thanks in advance!
5 Replies
kdy1
kdy13w ago
Yeap, but you should use Rust API, and you should modify AST instead of the string if you want to preserve comments
Shahryar
ShahryarOP3w ago
Thank you, yes I meant SWC rust function, and do you have info about my second question? AST --> to Json and Json --> to AST @kdy1
kdy1
kdy13w ago
Enable serde-impl of swc_ecma_ast and just use serde_json. Actually it's how the ES parser is tested. https://github.com/swc-project/swc/blob/6c2bb13f9d9ec522a938d8aba535a8a93758e43d/crates/swc_ecma_parser/tests/js.rs#L29-L71
GitHub
swc/crates/swc_ecma_parser/tests/js.rs at 6c2bb13f9d9ec522a938d8aba...
Rust-based platform for the Web. Contribute to swc-project/swc development by creating an account on GitHub.
Shahryar
ShahryarOP3w ago
Soooo much thank you!!! My last question As my understanding (basic rust) it is serialize, is there any deserialize option? @kdy1 Sorry I am very new to rust, I find some deserialize in rustdoc. but I do not know it covers or not Thank you in advance
kdy1
kdy13w ago
The parser test suite also deserializes the json to ensure that serde implementation is correct.

Did you find this page helpful?