{"id":16949711,"url":"https://github.com/kermanx/js_to_oxc","last_synced_at":"2025-03-17T08:37:10.482Z","repository":{"id":250315755,"uuid":"834110322","full_name":"kermanx/js_to_oxc","owner":"kermanx","description":"Convert JS source to Oxc AST builder in Rust","archived":false,"fork":false,"pushed_at":"2024-11-27T14:33:42.000Z","size":231,"stargazers_count":30,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-27T21:37:09.081Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://kermanx.github.io/js_to_oxc/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kermanx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-26T12:46:10.000Z","updated_at":"2025-02-01T19:02:17.000Z","dependencies_parsed_at":"2024-09-07T14:51:26.247Z","dependency_job_id":"fcb97459-bd94-4c57-a7de-0c8ac79642cc","html_url":"https://github.com/kermanx/js_to_oxc","commit_stats":null,"previous_names":["kermanx/js_to_oxc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kermanx%2Fjs_to_oxc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kermanx%2Fjs_to_oxc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kermanx%2Fjs_to_oxc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kermanx%2Fjs_to_oxc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kermanx","download_url":"https://codeload.github.com/kermanx/js_to_oxc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243852485,"owners_count":20358271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T21:55:52.611Z","updated_at":"2025-03-17T08:37:10.468Z","avatar_url":"https://github.com/kermanx.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# js_to_oxc\n\nConvert JS source to Oxc AST builder in Rust. Useful for injecting JS code when developing Rolldown plugins.\n\n[**Online version**](https://KermanX.github.io/js_to_oxc)\n\n### Example\n\n```js\nconsole.log('Hello, World!')\n```\n\nWill be converted to:\n\n```rust\nself.ast_builder.expression_call(\n  SPAN,\n  self.ast_builder.vec1(\n    Argument::from(\n      self.ast_builder\n        .expression_string_literal(SPAN, \"Hello, World!\"),\n    ),\n  ),\n  Expression::from(\n    self.ast_builder.member_expression_static(\n      SPAN,\n      self.ast_builder\n        .expression_identifier_reference(SPAN, \"console\"),\n      self.ast_builder.identifier_name(SPAN, \"log\"),\n      false,\n    ),\n  ),\n  NONE,\n  false,\n)\n```\n\nThe execution result of the generated Rust code is the AST of the given JS code.\n\nThis tool supports both expression mode and program mode.\n\n### Holes\n\nThis tool supports holes in the JS code. Identifier starting with `$` will be treated as a hole. For example:\n\n```js\nlog($1)\n```\n\nWill be converted to:\n\n```rust\nself.ast_builder.expression_call(\n  SPAN,\n  self.ast_builder.vec1(\n    Argument::from(\n      __1__  // hole\n    ),\n  ),\n  self.ast_builder\n    .expression_identifier_reference(SPAN, \"log\"),\n  NONE,\n  false,\n)\n```\n\n### Motivation\n\nI was porting a plugin from [Rollup](https://rollupjs.org) to [Rolldown](https://rolldown.rs). The plugin injects some **dynamic** JS code in the transform hook. In Rollup, the plugin simply uses string concatenation to inject the JS code. However, in Rolldown, the plugin needs to build the AST of the JS code - which is about 20 times longer than the implementation in Rollup. So, I decided to write this tool to convert the JS code to Oxc AST builder code.\n\nWhy not use a Rust macro which accepts JS and generates AST? Because the JS code is quite dynamic, and the macro will be too complex to write and maintain.\n\n\u003e [Here](https://github.com/rolldown/rolldown/blob/main/crates/rolldown_plugin_glob_import/src/lib.rs#L150-L306) is a Rolldown plugin that uses lots of code to generate a dynamic AST, which is a good example of the use case of this tool.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkermanx%2Fjs_to_oxc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkermanx%2Fjs_to_oxc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkermanx%2Fjs_to_oxc/lists"}