refactor: implement GetAstBuilder on visitor/pass types#10424
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. |
There was a problem hiding this comment.
Pull request overview
This PR is a follow-on refactor to reduce AST-construction boilerplate by implementing GetAstBuilder/GetAllocator on visitor/finalizer/context types that already hold an AstBuilder, enabling call sites to pass self directly into oxc per-type constructors.
Changes:
- Implement
GetAstBuilder+GetAllocatorfor several core visitors/finalizers (PreProcessor,ScopeHoistingFinalizer,HmrAstFinalizer) and plugin visitors. - Mechanically update AST-construction call sites to pass
self(and usetake_in(self)/*_in(..., self)patterns). - Minor trait-impl reordering/annotation tweaks (e.g.,
#[inline]) to match the new usage.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/rolldown/src/utils/tweak_ast_for_scanning.rs | Makes PreProcessor implement GetAstBuilder/GetAllocator and updates AST construction to pass self. |
| crates/rolldown/src/module_finalizers/rename.rs | Switches AST construction to pass self (via ScopeHoistingFinalizer traits). |
| crates/rolldown/src/module_finalizers/mod.rs | Adds GetAstBuilder/GetAllocator impls for ScopeHoistingFinalizer and updates numerous builder/allocator call sites to pass self. |
| crates/rolldown/src/module_finalizers/impl_visit_mut.rs | Updates finalizer visit-mutations to use self for allocation / AST building. |
| crates/rolldown/src/module_finalizers/hmr.rs | Updates HMR-related rewrites to use self for AST construction. |
| crates/rolldown/src/hmr/impl_traverse_for_hmr_ast_finalizer.rs | Updates traversal logic to use take_in(self) and build nodes with self. |
| crates/rolldown/src/hmr/hmr_ast_finalizer.rs | Implements GetAstBuilder/GetAllocator for HmrAstFinalizer and updates call sites to pass self. |
| crates/rolldown/src/ast_scanner/const_eval.rs | Reorders/updates GetAstBuilder/GetAllocator impls for ConstEvalCtx (adds #[inline]). |
| crates/rolldown_plugin_vite_web_worker_post/src/ast_visitor.rs | Implements GetAstBuilder/GetAllocator for the visitor and updates node construction to pass self. |
| crates/rolldown_plugin_vite_build_import_analysis/src/ast_visit.rs | Implements GetAstBuilder/GetAllocator for the visitor and updates construction sites to pass self. |
| crates/rolldown_plugin_vite_build_import_analysis/src/ast_utils.rs | Refactors helper builders to pass self into constructors and allocator-taking helpers. |
| crates/rolldown_plugin_lazy_compilation/src/runtime_injector.rs | Implements GetAstBuilder/GetAllocator and simplifies import-expression rewrite construction with self. |
Merging this PR will not alter performance
Comparing Footnotes
|
c583ea9 to
6466383
Compare
0b48133 to
6802e7c
Compare
Merge activity
|
Follow-on after #10018 and #10353. Pure refactor. Implement `GetAstBuilder` and `GetAllocator` on visitor/pass/context types which hold an `AstBuilder`. AST builder methods take any `&B where B: GetAstBuilder`, so this allows removing a lot of repetitious boilerplate code at call sites. Before: ```rust Expression::new_string_literal( SPAN, Str::from_str_in(id, &self.ast_builder), None, &self.ast_builder, ) ``` After: ```rust Expression::new_string_literal(SPAN, Str::from_str_in(id, self), None, self) ```
6466383 to
55729bb
Compare
6802e7c to
8d87835
Compare
…er` (#10425) Continuation of #10424. `ScopeHoistingFinalizer` has these fields: ```rust pub struct ScopeHoistingFinalizer<'me, 'ast: 'me> { pub alloc: &'ast Allocator, pub ast_builder: AstBuilder<'ast>, // ... other fields ... } ``` `AstBuilder<'ast>` already contains an `&'ast Allocator`, so this was duplicating the same reference twice. #10424 removed most usages of `alloc` field as a by-product of shortening code. This PR removes the remaining usage, and removes the `alloc` field itself.

Follow-on after #10018 and #10353. Pure refactor.
Implement
GetAstBuilderandGetAllocatoron visitor/pass/context types which hold anAstBuilder.AST builder methods take any
&B where B: GetAstBuilder, so this allows removing a lot of repetitious boilerplate code at call sites.Before:
After: