refactor: shorten AST builder code#10418
Conversation
How to use the Graphite Merge QueueAdd the label graphite: merge-when-ready to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
✅ Deploy Preview for rolldown-rs canceled.
|
There was a problem hiding this comment.
Pull request overview
This PR continues the oxc 0.138 AST-construction migration by refactoring call sites to use the newer shortened new_* constructors (e.g. Expression::new_call_expression, Statement::new_expression_statement) instead of manual enum wrapping + ::boxed(...). The goal is primarily to reduce verbosity while keeping generated AST identical.
Changes:
- Replaced many
EnumVariant(Type::boxed(...))AST constructions withType::new_*/Enum::new_*helpers across core, HMR, and plugin crates. - Updated common AST utility helpers (
ast_factory, binding pattern/property extensions) to use the shortened construction APIs. - Adjusted JSON-to-AST object property key construction to use
PropertyKey::new_string_literaldirectly.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/rolldown/src/utils/tweak_ast_for_scanning.rs | Switches statement/expression construction to Statement::new_* / Expression::new_* helpers. |
| crates/rolldown/src/stages/link_stage/generate_lazy_export.rs | Uses shortened PropertyKey construction for object properties. |
| crates/rolldown/src/module_finalizers/rename.rs | Simplifies assignment-target identifier construction. |
| crates/rolldown/src/module_finalizers/mod.rs | Large set of AST-construction refactors for finalizer-generated code paths (imports/exports/runtime helpers). |
| crates/rolldown/src/module_finalizers/impl_visit_mut.rs | Refactors statement construction in VisitMut transformations. |
| crates/rolldown/src/hmr/utils.rs | Uses shortened constructors for HMR-injected AST fragments. |
| crates/rolldown/src/hmr/impl_traverse_for_hmr_ast_finalizer.rs | Refactors traversal-produced AST nodes to use new_* helpers (try stmt, calls, literals). |
| crates/rolldown/src/hmr/hmr_ast_finalizer.rs | Refactors HMR finalizer AST building to shortened constructors. |
| crates/rolldown_plugin_vite_web_worker_post/src/ast_visitor.rs | Updates plugin’s AST visitor to shortened member/object/property constructors. |
| crates/rolldown_plugin_vite_build_import_analysis/src/ast_visit.rs | Refactors import-helper insertion to use Statement::new_import_declaration. |
| crates/rolldown_plugin_vite_build_import_analysis/src/ast_utils.rs | Updates preload-call construction and related AST helpers to use new_* APIs. |
| crates/rolldown_plugin_lazy_compilation/src/runtime_injector.rs | Refactors lazy-compilation runtime injection helper AST construction. |
| crates/rolldown_ecmascript_utils/src/extensions/ast_ext/binding_property_ext.rs | Refactors binding-property → assignment-target-property conversion to new constructors. |
| crates/rolldown_ecmascript_utils/src/extensions/ast_ext/binding_pattern_ext.rs | Refactors binding-pattern conversions and expression emission to new constructors. |
| crates/rolldown_ecmascript_utils/src/ast_factory.rs | Refactors shared AST factory helpers (then/arrow/call/member/property/statement builders) to new constructors. |
| crates/rolldown_common/src/ecmascript/json_to_program.rs | Uses PropertyKey::new_string_literal directly for JSON object keys. |
Merging this PR will not alter performance
Comparing Footnotes
|
4e5b677 to
8e20bae
Compare
Merge activity
|
Follow-on after #10018. First of a series of PRs all grouped around 2 themes: 1. Shorten code using AST builder methods. 2. Reduce string allocations. The new AST builder methods allow shortened syntax. Utilize the shorter methods wherever possible. Before: ```rust ast::Statement::ExpressionStatement(ast::ExpressionStatement::boxed(span, call_expr, &self.ast_builder)) ``` After: ```rust ast::Statement::new_expression_statement(span, call_expr, &self.ast_builder) ``` Note: Diff is quite large, but these changes were almost entirely produced using the same [deterministic codemod](https://github.com/oxc-project/oxc/tree/overlookmotel/ast-builder-codemod) we used to migrate Oxc repo to new AST builder, with only some final tweaks by an LLM. I've reviewed it all and, even though I'm unfamiliar with Rolldown's codebase, I'm pretty confident it looks correct.
8e20bae to
9886244
Compare
Follow-on after #10018. The new AST builder methods allow passing arrays where a `Vec` is required. Use this facility to remove a lot of repetitious boilerplate. Before: ```rust FunctionBody::new( SPAN, oxc::allocator::Vec::new_in(&self.ast_builder), oxc::allocator::Vec::from_value_in(try_stmt, &self.ast_builder), &self.ast_builder, ) ``` After: ```rust FunctionBody::new(SPAN, [], [try_stmt], &self.ast_builder) ``` Note: Like #10418, diff is quite large, but these changes were almost entirely produced using the same [deterministic codemod](https://github.com/oxc-project/oxc/tree/overlookmotel/ast-builder-codemod) we used to migrate Oxc repo to new AST builder, with only some final tweaks by an LLM. I've reviewed it all and, even though I'm unfamiliar with Rolldown's codebase, I'm pretty confident it looks correct.

Follow-on after #10018.
First of a series of PRs all grouped around 2 themes:
The new AST builder methods allow shortened syntax. Utilize the shorter methods wherever possible.
Before:
After:
Note: Diff is quite large, but these changes were almost entirely produced using the same deterministic codemod we used to migrate Oxc repo to new AST builder, with only some final tweaks by an LLM. I've reviewed it all and, even though I'm unfamiliar with Rolldown's codebase, I'm pretty confident it looks correct.