Skip to content

feat(ast): add AST creation methods to AST types#23650

Merged
graphite-app[bot] merged 1 commit into
mainfrom
om/06-06-feat_ast_add_ast_creation_methods_to_ast_types
Jun 25, 2026
Merged

feat(ast): add AST creation methods to AST types#23650
graphite-app[bot] merged 1 commit into
mainfrom
om/06-06-feat_ast_add_ast_creation_methods_to_ast_types

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Jun 19, 2026

Copy link
Copy Markdown
Member

Part of #23043.

Codegen AST builder methods on AST types, matching the methods which are generated on current AstBuidler type.

Add a single new AstBuilder type which is the simplest possible - just creates all nodes with dummy NodeIds - same as current builder does. But this PR includes the traits to add other AstBuild implementations later e.g. a builder for transformer which generates unique NodeId for each AST node.

Design is much the same as outlined in #23043 but:

  • Extra trait to reduce trait bounds in generated methods (so reducing code size and compile time).
  • Different naming for enum methods - Expression::new_null_literal not Expression::null_literal. This naming is more verbose, but clearer, and avoids clashing with existing methods e.g. ModuleExportName::identifier_name.

Note: All these methods are inlined (they need to be, to make AST nodes be constructed in arena directly, not built on stack and copied to arena). So the traits should not have any affect on binary size.

overlookmotel commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of 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.

@codspeed-hq

codspeed-hq Bot commented Jun 19, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing om/06-06-feat_ast_add_ast_creation_methods_to_ast_types (f3b599f) with main (2580eda)

Open in CodSpeed

Footnotes

  1. 9 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@overlookmotel overlookmotel self-assigned this Jun 19, 2026
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from 7cab202 to 6a2e997 Compare June 20, 2026 22:21
@overlookmotel overlookmotel force-pushed the om/06-06-refactor_ast_add_ast_builder_cargo_feature branch from 662766a to faeb137 Compare June 20, 2026 22:21
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from 6a2e997 to 6143bb3 Compare June 21, 2026 00:39
@overlookmotel overlookmotel force-pushed the om/06-06-refactor_ast_add_ast_builder_cargo_feature branch 2 times, most recently from 0f7f1c2 to f43a323 Compare June 23, 2026 00:31
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from 6143bb3 to 449b1b2 Compare June 23, 2026 00:31
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from 449b1b2 to c3ccd4d Compare June 23, 2026 00:56
@graphite-app graphite-app Bot force-pushed the om/06-06-refactor_ast_add_ast_builder_cargo_feature branch 2 times, most recently from 553197f to f40235d Compare June 23, 2026 01:08
@graphite-app graphite-app Bot force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from c3ccd4d to c1442ed Compare June 23, 2026 01:08
@overlookmotel overlookmotel changed the base branch from om/06-06-refactor_ast_add_ast_builder_cargo_feature to graphite-base/23650 June 23, 2026 10:46
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from c1442ed to 5399e65 Compare June 23, 2026 10:46
@overlookmotel overlookmotel changed the base branch from graphite-base/23650 to om/06-23-feat_ast_put_astbuilder_behind_builder_cargo_feature June 23, 2026 10:46
@overlookmotel overlookmotel changed the base branch from om/06-23-feat_ast_put_astbuilder_behind_builder_cargo_feature to graphite-base/23650 June 23, 2026 12:07
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from 5399e65 to 32f725e Compare June 23, 2026 12:07
@overlookmotel overlookmotel changed the base branch from graphite-base/23650 to om/06-06-refactor_ast_add_builder_cargo_feature June 23, 2026 12:07
@overlookmotel overlookmotel changed the base branch from om/06-06-refactor_ast_add_builder_cargo_feature to graphite-base/23650 June 24, 2026 16:53
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from 32f725e to cafc6dd Compare June 24, 2026 16:53
@overlookmotel overlookmotel changed the base branch from graphite-base/23650 to main June 24, 2026 16:53
@overlookmotel overlookmotel requested a review from Copilot June 24, 2026 16:55
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from cafc6dd to eae9e61 Compare June 24, 2026 16:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new, feature-gated (“builder_new”) AST construction API where builder/factory methods are generated as inherent methods on AST node types (instead of being methods on the legacy AstBuilder). This is part of the longer-term AstBuilder redesign to support alternative builders (e.g. real NodeId generation) while keeping the current builder available for compatibility.

Changes:

  • Register a new BuilderMethodsGenerator in tasks/ast_tools to generate builder_methods.rs for AST types.
  • Add a new crates/oxc_ast::builder module (behind builder_new) defining AstBuild / GetAstBuilder traits plus a minimal AstBuilder that returns NodeId::DUMMY.
  • Wire the generated builder_methods module into oxc_ast behind the builder_new feature and add the feature flag to Cargo.toml.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tasks/ast_tools/src/main.rs Registers the new generator in the ast_tools pipeline.
tasks/ast_tools/src/generators/mod.rs Adds the new generator module/export.
tasks/ast_tools/src/generators/builder_methods.rs Implements codegen for inherent AST-type builder methods (new, boxed, and new_* for enum variants).
crates/oxc_ast/src/lib.rs Introduces feature-gated builder module + generated builder_methods module integration while keeping legacy AstBuilder re-export.
crates/oxc_ast/src/builder/mod.rs Defines the new builder traits and minimal AstBuilder implementation (feature-gated via lib.rs).
crates/oxc_ast/Cargo.toml Adds the builder_new cargo feature.

Comment thread crates/oxc_ast/src/builder/mod.rs
Comment thread crates/oxc_ast/src/builder/mod.rs Outdated
@overlookmotel overlookmotel force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from eae9e61 to f3b599f Compare June 24, 2026 17:50
@overlookmotel overlookmotel marked this pull request as ready for review June 24, 2026 17:50
@graphite-app graphite-app Bot added the 0-merge Merge with Graphite Merge Queue label Jun 25, 2026
@graphite-app

graphite-app Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Merge activity

Part of #23043.

Codegen AST builder methods on AST types, matching the methods which are generated on current `AstBuidler` type.

Add a single new `AstBuilder` type which is the simplest possible - just creates all nodes with dummy `NodeId`s - same as current builder does. But this PR includes the traits to add other `AstBuild` implementations later e.g. a builder for transformer which generates unique `NodeId` for each AST node.

Design is much the same as outlined in #23043 but:

* Extra trait to reduce trait bounds in generated methods (so reducing code size and compile time).
* Different naming for enum methods - `Expression::new_null_literal` not `Expression::null_literal`. This naming is more verbose, but clearer, and avoids clashing with existing methods e.g. `ModuleExportName::identifier_name`.

Note: All these methods are inlined (they need to be, to make AST nodes be constructed in arena directly, not built on stack and copied to arena). So the traits should not have any affect on binary size.
@graphite-app graphite-app Bot force-pushed the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch from f3b599f to 05d1357 Compare June 25, 2026 01:45
@graphite-app graphite-app Bot merged commit 05d1357 into main Jun 25, 2026
29 checks passed
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jun 25, 2026
@graphite-app graphite-app Bot deleted the om/06-06-feat_ast_add_ast_creation_methods_to_ast_types branch June 25, 2026 01:52
camc314 added a commit that referenced this pull request Jun 29, 2026
### 💥 BREAKING CHANGES

- 94fbacb ast: [**BREAKING**] Only export `AstBuilder` and `NONE` in
`builder` module (#23876) (overlookmotel)
- 8de5122 ecmascript: [**BREAKING**] Switch to new `AstBuilder` (#23834)
(overlookmotel)
- dc0ef38 transformer: [**BREAKING**] Switch to new `AstBuilder`
(#23831) (overlookmotel)
- 88f4455 str: [**BREAKING**] `Str` and `Ident` methods take
`&GetAllocator` (#23781) (overlookmotel)
- 36009dd allocator: [**BREAKING**] `GetAllocator::allocator` take
`&self` (#23676) (overlookmotel)
- bd74f9d allocator: [**BREAKING**] Rename `AllocatorAccessor` trait to
`GetAllocator` (#23675) (overlookmotel)

### 🚀 Features

- 326fe25 transformer_plugins: Support `typeof` `define` keys (#23605)
(Alexander Lichter)
- f2091b3 ast: Unify old and new `AstBuilder`s (#23875) (overlookmotel)
- cd1fd12 codegen: Expose `Codegen::print_string` API (#23785) (camc314)
- 785461b ast: Add custom builder methods to AST types (#23651)
(overlookmotel)
- 05d1357 ast: Add AST creation methods to AST types (#23650)
(overlookmotel)
- 2580eda str: Add `Str::from_str_in` and `Ident::from_str_in` methods
(#23767) (overlookmotel)
- 6883fcf minifier: Fold write-once falsy var to false in boolean
context (#23540) (Dunqing)
- fcbf993 allocator: Add `Vec::from_value_in` method (#23718)
(overlookmotel)
- 989ddb7 allocator: Add `Vec::from_box_in` method (#23717)
(overlookmotel)
- 9d1aa7f allocator: Improve `PartialEq` for `Vec` (#23716)
(overlookmotel)

### 🐛 Bug Fixes

- da0e5bf minifier: Don't reorder a closed-over TDZ read when inlining a
var (#23771) (Dunqing)
- 0b3021f allocator: Remove `Vec::from_box_in` (#23873) (overlookmotel)
- 0ab64ec ast: Silence deprecation warnings within files defining
deprecated `AstBuilder` methods (#23889) (overlookmotel)
- 8c07cad all: Enable `disable_old_builder` Cargo feature for `oxc_ast`
crate in tests (#23888) (overlookmotel)
- 3800f01 ast: Legacy `AstBuilder` methods take `self` not `&self`
(#23891) (overlookmotel)
- 869ac20 semantic/cfg: Connect for update exit to loop test (#23791)
(camc314)
- d3e92d5 semantic/cfg: Connect while branches from condition exit
(#23790) (camc314)
- 025045d ast: `ExportNamedDeclaration` plain builder methods return
boxed nodes (#23783) (overlookmotel)
- 7537c58 ast: Fix name of `AstBuilder` method for
`Expression::V8IntrinsicExpression` (#23766) (overlookmotel)
- 3f574f5 traverse: Fix unsoundness in `Traverse` walk functions
(#23745) (overlookmotel)
- 585760f parser: String in AST reference arena (#23721) (overlookmotel)
- 7231d55 allocator: Fix unsound lifetime extension in `Box::new_in`
(#23685) (overlookmotel)

### ⚡ Performance

- d5c916a semantic: Flatten hoisting_variables to avoid per-scope map
allocation (#23927) (Lawrence Lin)
- e71609d minifier: Bail member-expr folding before the side-effect walk
(#23924) (Lawrence Lin)
- e1f89ab minifier: Reduce string allocations folding addition (#23846)
(overlookmotel)
- 9f6ee3b isolated-declarations: Pool scope maps to avoid per-scope
alloc/rehash (#23761) (Boshen)
- 0b07c4c semantic: Avoid heap alloc for catch-clause binding ids
(#23911) (Lawrence Lin)
- c5eef8b regular_expression: Skip capturing-group pre-parse when
pattern has no `(` (#23908) (Lawrence Lin)
- b4f5b4b isolated_declarations: Remove redundant clone of formal
parameter pattern (#23912) (Lawrence Lin)
- 53d083f isolated_declarations: Use `TakeIn` not `CloneIn` (#23847)
(overlookmotel)
- 3ea9304 react_compiler: Use faster API to arena allocate strings
(#23849) (overlookmotel)
- a6d8e45 parser: Avoid span lookup for arrow expression body (#23788)
(camc314)
- e1886a0 transformer, minifier: Use `static_ident!` macro to create
static `Ident`s (#23727) (overlookmotel)
- 5527bef transformer/object-rest-spread: Reduce iteration (#23720)
(overlookmotel)
- 680ffbc transformer: Allocate AST nodes in arena directly (#23711)
(overlookmotel)
- 1c63c66 parser: Allocate AST nodes in arena directly (#23712)
(overlookmotel)
- 3855f0c minifier: Allocate AST nodes in arena directly (#23710)
(overlookmotel)
- d025887 isolated_declarations: Allocate AST nodes in arena directly
(#23709) (overlookmotel)
- 10b96c6 parser: Remove string search from parsing JSX element name
(#23713) (overlookmotel)

### 📚 Documentation

- 3d61dea all: Correct capitalization in comments (#23887)
(overlookmotel)
- aa1ad74 ast: Add `#[deprecated]` to legacy `AstBuilder` methods
(#23877) (overlookmotel)
- a4676db ast: Correct doc comment for `NONE` (#23765) (overlookmotel)
- 419ec80 syntax: Fix typo in doc comment (#23674) (overlookmotel)

### 🛡️ Security

- 3cdd18f deps: Update npm packages (#23690) (renovate[bot])

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Co-authored-by: Cameron <cameron.clark@hey.com>
camc314 pushed a commit that referenced this pull request Jul 3, 2026
Part of #23043.

Codegen AST builder methods on AST types, matching the methods which are generated on current `AstBuidler` type.

Add a single new `AstBuilder` type which is the simplest possible - just creates all nodes with dummy `NodeId`s - same as current builder does. But this PR includes the traits to add other `AstBuild` implementations later e.g. a builder for transformer which generates unique `NodeId` for each AST node.

Design is much the same as outlined in #23043 but:

* Extra trait to reduce trait bounds in generated methods (so reducing code size and compile time).
* Different naming for enum methods - `Expression::new_null_literal` not `Expression::null_literal`. This naming is more verbose, but clearer, and avoids clashing with existing methods e.g. `ModuleExportName::identifier_name`.

Note: All these methods are inlined (they need to be, to make AST nodes be constructed in arena directly, not built on stack and copied to arena). So the traits should not have any affect on binary size.
camc314 added a commit that referenced this pull request Jul 3, 2026
### 💥 BREAKING CHANGES

- 94fbacb ast: [**BREAKING**] Only export `AstBuilder` and `NONE` in
`builder` module (#23876) (overlookmotel)
- 8de5122 ecmascript: [**BREAKING**] Switch to new `AstBuilder` (#23834)
(overlookmotel)
- dc0ef38 transformer: [**BREAKING**] Switch to new `AstBuilder`
(#23831) (overlookmotel)
- 88f4455 str: [**BREAKING**] `Str` and `Ident` methods take
`&GetAllocator` (#23781) (overlookmotel)
- 36009dd allocator: [**BREAKING**] `GetAllocator::allocator` take
`&self` (#23676) (overlookmotel)
- bd74f9d allocator: [**BREAKING**] Rename `AllocatorAccessor` trait to
`GetAllocator` (#23675) (overlookmotel)

### 🚀 Features

- 326fe25 transformer_plugins: Support `typeof` `define` keys (#23605)
(Alexander Lichter)
- f2091b3 ast: Unify old and new `AstBuilder`s (#23875) (overlookmotel)
- cd1fd12 codegen: Expose `Codegen::print_string` API (#23785) (camc314)
- 785461b ast: Add custom builder methods to AST types (#23651)
(overlookmotel)
- 05d1357 ast: Add AST creation methods to AST types (#23650)
(overlookmotel)
- 2580eda str: Add `Str::from_str_in` and `Ident::from_str_in` methods
(#23767) (overlookmotel)
- 6883fcf minifier: Fold write-once falsy var to false in boolean
context (#23540) (Dunqing)
- fcbf993 allocator: Add `Vec::from_value_in` method (#23718)
(overlookmotel)
- 989ddb7 allocator: Add `Vec::from_box_in` method (#23717)
(overlookmotel)
- 9d1aa7f allocator: Improve `PartialEq` for `Vec` (#23716)
(overlookmotel)

### 🐛 Bug Fixes

- da0e5bf minifier: Don't reorder a closed-over TDZ read when inlining a
var (#23771) (Dunqing)
- 0b3021f allocator: Remove `Vec::from_box_in` (#23873) (overlookmotel)
- 0ab64ec ast: Silence deprecation warnings within files defining
deprecated `AstBuilder` methods (#23889) (overlookmotel)
- 8c07cad all: Enable `disable_old_builder` Cargo feature for `oxc_ast`
crate in tests (#23888) (overlookmotel)
- 3800f01 ast: Legacy `AstBuilder` methods take `self` not `&self`
(#23891) (overlookmotel)
- 869ac20 semantic/cfg: Connect for update exit to loop test (#23791)
(camc314)
- d3e92d5 semantic/cfg: Connect while branches from condition exit
(#23790) (camc314)
- 025045d ast: `ExportNamedDeclaration` plain builder methods return
boxed nodes (#23783) (overlookmotel)
- 7537c58 ast: Fix name of `AstBuilder` method for
`Expression::V8IntrinsicExpression` (#23766) (overlookmotel)
- 3f574f5 traverse: Fix unsoundness in `Traverse` walk functions
(#23745) (overlookmotel)
- 585760f parser: String in AST reference arena (#23721) (overlookmotel)
- 7231d55 allocator: Fix unsound lifetime extension in `Box::new_in`
(#23685) (overlookmotel)

### ⚡ Performance

- d5c916a semantic: Flatten hoisting_variables to avoid per-scope map
allocation (#23927) (Lawrence Lin)
- e71609d minifier: Bail member-expr folding before the side-effect walk
(#23924) (Lawrence Lin)
- e1f89ab minifier: Reduce string allocations folding addition (#23846)
(overlookmotel)
- 9f6ee3b isolated-declarations: Pool scope maps to avoid per-scope
alloc/rehash (#23761) (Boshen)
- 0b07c4c semantic: Avoid heap alloc for catch-clause binding ids
(#23911) (Lawrence Lin)
- c5eef8b regular_expression: Skip capturing-group pre-parse when
pattern has no `(` (#23908) (Lawrence Lin)
- b4f5b4b isolated_declarations: Remove redundant clone of formal
parameter pattern (#23912) (Lawrence Lin)
- 53d083f isolated_declarations: Use `TakeIn` not `CloneIn` (#23847)
(overlookmotel)
- 3ea9304 react_compiler: Use faster API to arena allocate strings
(#23849) (overlookmotel)
- a6d8e45 parser: Avoid span lookup for arrow expression body (#23788)
(camc314)
- e1886a0 transformer, minifier: Use `static_ident!` macro to create
static `Ident`s (#23727) (overlookmotel)
- 5527bef transformer/object-rest-spread: Reduce iteration (#23720)
(overlookmotel)
- 680ffbc transformer: Allocate AST nodes in arena directly (#23711)
(overlookmotel)
- 1c63c66 parser: Allocate AST nodes in arena directly (#23712)
(overlookmotel)
- 3855f0c minifier: Allocate AST nodes in arena directly (#23710)
(overlookmotel)
- d025887 isolated_declarations: Allocate AST nodes in arena directly
(#23709) (overlookmotel)
- 10b96c6 parser: Remove string search from parsing JSX element name
(#23713) (overlookmotel)

### 📚 Documentation

- 3d61dea all: Correct capitalization in comments (#23887)
(overlookmotel)
- aa1ad74 ast: Add `#[deprecated]` to legacy `AstBuilder` methods
(#23877) (overlookmotel)
- a4676db ast: Correct doc comment for `NONE` (#23765) (overlookmotel)
- 419ec80 syntax: Fix typo in doc comment (#23674) (overlookmotel)

### 🛡️ Security

- 3cdd18f deps: Update npm packages (#23690) (renovate[bot])

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Co-authored-by: Cameron <cameron.clark@hey.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST A-ast-tools Area - AST tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants