Skip to content

refactor(parser): switch parser to new AstBuilder#23730

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

refactor(parser): switch parser to new AstBuilder#23730
graphite-app[bot] merged 1 commit into
mainfrom
om/06-23-refactor_parser_switch_parser_to_new_astbuilder_

Conversation

@overlookmotel

@overlookmotel overlookmotel commented Jun 23, 2026

Copy link
Copy Markdown
Member

Part of #23043.

Switch over oxc_parser to use the new AstBuilder and builder methods on AST types themselves.

Vast majority performed by an ast-grep codemod generated by Claude (2nd commit - codemod here).

The last 2 commits are a few small fixes, and some manual changes to shorten code.

overlookmotel commented Jun 23, 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 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ (17a0f9e) with om/06-18-feat_ast_add_custom_builder_methods_to_ast_types (1e4a339)

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 force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from de11062 to e769418 Compare June 23, 2026 00:56
@overlookmotel overlookmotel force-pushed the om/06-22-feat_ast_add_methods_to_create_primitives_to_new_astbuilder_ branch from 27489b3 to e512626 Compare June 23, 2026 00:56
@graphite-app graphite-app Bot force-pushed the om/06-22-feat_ast_add_methods_to_create_primitives_to_new_astbuilder_ branch from e512626 to c5d5217 Compare June 23, 2026 01:09
@graphite-app graphite-app Bot force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from e769418 to 16f04a9 Compare June 23, 2026 01:10
Comment thread crates/oxc_parser/src/js/arrow.rs Outdated
@overlookmotel overlookmotel force-pushed the om/06-22-feat_ast_add_methods_to_create_primitives_to_new_astbuilder_ branch from c5d5217 to 6b2bb22 Compare June 23, 2026 10:46
@overlookmotel overlookmotel force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from 16f04a9 to 53147e2 Compare June 23, 2026 10:46
@overlookmotel overlookmotel force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from 53147e2 to 6afe9e0 Compare June 23, 2026 12:07
@overlookmotel overlookmotel force-pushed the om/06-22-feat_ast_add_methods_to_create_primitives_to_new_astbuilder_ branch from 6b2bb22 to 431ba1e Compare June 23, 2026 12:07
@overlookmotel overlookmotel changed the base branch from om/06-22-feat_ast_add_methods_to_create_primitives_to_new_astbuilder_ to graphite-base/23730 June 24, 2026 16:53
@overlookmotel overlookmotel force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from 6afe9e0 to 36eb2c7 Compare June 24, 2026 16:53
@overlookmotel overlookmotel changed the base branch from graphite-base/23730 to om/06-18-feat_ast_add_custom_builder_methods_to_ast_types June 24, 2026 16:54
@overlookmotel overlookmotel requested a review from Copilot June 24, 2026 16:56
@overlookmotel overlookmotel force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from 36eb2c7 to e8278a2 Compare June 24, 2026 16:56
@overlookmotel overlookmotel force-pushed the om/06-18-feat_ast_add_custom_builder_methods_to_ast_types branch from dd42174 to 5ef81a0 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

This PR migrates oxc_parser to the new AST builder API (builder methods on AST types + GetAstBuilder), aligning the parser with the ongoing AstBuilder redesign in #23043 and reducing reliance on the legacy self.ast.* constructor surface.

Changes:

  • Switch parser AST construction from self.ast.* methods to *_::new(...) / *_::boxed(...) builder methods across JS/TS/JSX parsing paths.
  • Update allocation patterns to use ArenaVec::{new_in, with_capacity_in, from_value_in} and ArenaBox::new_in(...) via GetAllocator.
  • Wire ParserImpl into the new builder system by implementing GetAstBuilder, and enable oxc_ast’s builder_new feature.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/oxc_parser/src/ts/types.rs Convert TS type node construction to TSType::*/TS*::new/boxed builder methods and arena vec helpers.
crates/oxc_parser/src/ts/statement.rs Convert TS statement/declaration parsing to new builder methods and arena vec allocation helpers.
crates/oxc_parser/src/lib.rs Import new builder traits/types, construct Program via Program::new, update alloc, and implement GetAstBuilder for ParserImpl.
crates/oxc_parser/src/jsx/mod.rs Convert JSX node creation to JSX*::new/boxed methods and arena vec helpers.
crates/oxc_parser/src/js/statement.rs Convert statement node creation (and directive/hashbang) to new builder methods and arena vec helpers.
crates/oxc_parser/src/js/object.rs Convert object expression/property construction to new builder methods; update shorthand/init tracking construction sites.
crates/oxc_parser/src/js/module.rs Convert import/export/module AST construction to new builder methods and arena vec helpers.
crates/oxc_parser/src/js/grammar.rs Update cover grammar conversions to new builder methods; replace p.ast.vec() with ArenaVec::new_in(p).
crates/oxc_parser/src/js/function.rs Convert function body/params/function nodes and yield expression construction to new builder methods.
crates/oxc_parser/src/js/expression.rs Convert expression node creation to new builder methods; update arena vec helpers and Str::from_in usage.
crates/oxc_parser/src/js/declaration.rs Convert variable declaration/declarator construction and list allocations to new builder methods.
crates/oxc_parser/src/js/class.rs Convert class/class-body/heritage/method/property element construction to new builder methods and arena vec helpers.
crates/oxc_parser/src/js/binding.rs Convert binding patterns/rest elements/properties/assignment patterns to new builder methods.
crates/oxc_parser/src/js/arrow.rs Convert arrow function construction (params/body/expression) to new builder methods and arena vec helpers.
crates/oxc_parser/src/cursor.rs Replace builder vec creation with ArenaVec::new_in(self) in list parsing helpers.
crates/oxc_parser/Cargo.toml Enable oxc_ast feature builder_new for the parser crate.

@overlookmotel overlookmotel force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from e8278a2 to 858a6ba Compare June 24, 2026 17:49
@overlookmotel overlookmotel force-pushed the om/06-18-feat_ast_add_custom_builder_methods_to_ast_types branch from 5ef81a0 to 1e4a339 Compare June 24, 2026 17:50
@overlookmotel overlookmotel marked this pull request as ready for review June 24, 2026 17:51
@overlookmotel overlookmotel force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from 858a6ba to 17a0f9e Compare June 24, 2026 19:16
@overlookmotel overlookmotel requested a review from Dunqing June 24, 2026 19:17
@overlookmotel overlookmotel self-assigned this Jun 24, 2026
@overlookmotel overlookmotel requested a review from camc314 June 24, 2026 19:17
@overlookmotel

overlookmotel commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

@camc314 @Dunqing I think this is ready to go now. Please could you take a look and see if you think it's OK?

The thing I'm not entirely happy with is that it makes code more verbose (especially self.ast.vec1(node) -> ArenaVec::from_value_in(node, self)), but I can't really see any good way to make it shorter.

I did try a macro, which is shorter syntax, but I felt it was a bit "mysterious":

  • ArenaVec::new_in(self) -> avec![self]
  • ArenaVec::from_value_in(node, self) -> avec![node; self]
  • ArenaVec::from_array_in([node1, node2, node3], self) -> avec![node1, node2, node3; self]

If we all think we should move forwards with this, I think we can merge, and I'll transition all the other crates over in separate PRs.

@camc314 camc314 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.

nice work! looks good to me!

@camc314

camc314 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

I'll transition all the other crates over in separate PRs.

I'm happy to work on this for the linter - would be helpful to get used to the new API. but if me working on that will slow you down, please go ahead!

@overlookmotel

overlookmotel commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

I'm happy to work on this for the linter - would be helpful to get used to the new API. but if me working on that will slow you down, please go ahead!

Thanks! That would be a great help. By all means try the codemod and see how far it gets.

We might need to convert some other Str and Ident methods to take &A where A: GetAllocator<'a> instead of &'a Allocator (like #23767, #23756, and #23755 did) to avoid very verbose code -Str::from_strs_array_in([...], self) not Str::from_strs_array_in([...], &self.ast.allocator).

Then again, the linter doesn't use AstBuilder much, and not with usual pattern of having a context object holding an AstBuilder, so the codemod may not be much help without modification.

I noticed a pattern in linter where it creates an Allocator, creates an AstBuilder, uses that to generate a StringLiteral, and then passes the StringLiteral to Codegen to print it - by looks of things, it's doing all that just to escape a string. Surely there's a simpler way? Maybe oxc_codegen could just expose the function which escapes strings, independent of all the AST stuff?


But all that said, let's wait for Dunqing to have a look and see if he's happy with this before we go any further.

@Dunqing Dunqing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great!

@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Jun 25, 2026

Dunqing commented Jun 25, 2026

Copy link
Copy Markdown
Member

Merge activity

Part of #23043.

Switch over `oxc_parser` to use the new `AstBuilder` and builder methods on AST types themselves.

Vast majority performed by an ast-grep codemod generated by Claude (2nd commit - [codemod here](https://github.com/oxc-project/oxc/tree/overlookmotel/ast-builder-codemod)).

The last 2 commits are a few small fixes, and some manual changes to shorten code.
@graphite-app graphite-app Bot force-pushed the om/06-18-feat_ast_add_custom_builder_methods_to_ast_types branch from 1e4a339 to 785461b Compare June 25, 2026 01:45
@graphite-app graphite-app Bot force-pushed the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch from 17a0f9e to 981a952 Compare June 25, 2026 01:46
@graphite-app graphite-app Bot removed the 0-merge Merge with Graphite Merge Queue label Jun 25, 2026
Base automatically changed from om/06-18-feat_ast_add_custom_builder_methods_to_ast_types to main June 25, 2026 01:53
@graphite-app graphite-app Bot merged commit 981a952 into main Jun 25, 2026
32 checks passed
@graphite-app graphite-app Bot deleted the om/06-23-refactor_parser_switch_parser_to_new_astbuilder_ branch June 25, 2026 01:55
graphite-app Bot pushed a commit that referenced this pull request Jun 25, 2026
I created this ontop of #23781 just to avoid any potential conflicts.

Follows on from #23730

It was relatively pain free! Thanks @ overlookmotel for the migration script!
camc314 pushed a commit that referenced this pull request Jul 3, 2026
Part of #23043.

Switch over `oxc_parser` to use the new `AstBuilder` and builder methods on AST types themselves.

Vast majority performed by an ast-grep codemod generated by Claude (2nd commit - [codemod here](https://github.com/oxc-project/oxc/tree/overlookmotel/ast-builder-codemod)).

The last 2 commits are a few small fixes, and some manual changes to shorten code.
camc314 added a commit that referenced this pull request Jul 3, 2026
I created this ontop of #23781 just to avoid any potential conflicts.

Follows on from #23730

It was relatively pain free! Thanks @ overlookmotel for the migration script!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-parser Area - Parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants