1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! AST builder.
//!
//! This is undergoing change at present.
//!
//! Explanation of the motivation for this change here: <https://github.com/oxc-project/oxc/issues/23043>.
//!
//! ## Old builder
//!
//! [`AstBuilder`] used to be a [`Copy`] type, which had own methods for:
//!
//! * Creating AST nodes e.g. `builder.statement_expression(span, expr)`
//! * Creating primitives e.g. `builder.vec()`, `builder.ident(str)`
//!
//! These methods are defined in `../generated/ast_builder.rs` and `methods.rs`.
//!
//! ## New builder
//!
//! We have now added methods to AST types themselves which perform the same role,
//! and are passed an `&B where B: GetAstBuilder` or `&A where A: GetAllocator`:
//!
//! * `Statement::new_expression_statement(span, expr, &builder)`
//! * `Vec::new_in(&builder)`, `Ident::from_str_in(str, &builder)`
//!
//! `AstBuilder` is no longer `Copy` or `Clone`, and is passed by reference.
//! Its `allocator` field is no longer public. Use `allocator` method provided by `GetAllocator` trait.
//!
//! Implementing `GetAstBuilder` on types which hold an `AstBuilder` allows for a shorter syntax:
//!
//! * Long: `Vec::new_in(&self.ast)`
//! * Short: `Vec::new_in(self)`
//!
//! e.g.:
//!
//! ```
//! use oxc_allocator::{Allocator, ArenaVec, GetAllocator};
//! use oxc_ast::{ast::*, builder::{AstBuilder, GetAstBuilder}};
//! use oxc_span::SPAN;
//!
//! struct MyAstProcessor<'a> {
//! builder: AstBuilder<'a>,
//! }
//!
//! impl<'a> GetAstBuilder<'a> for MyAstProcessor<'a> {
//! type Builder = AstBuilder<'a>;
//!
//! fn builder(&self) -> &AstBuilder<'a> {
//! &self.builder
//! }
//! }
//!
//! impl<'a> GetAllocator<'a> for MyAstProcessor<'a> {
//! fn allocator(&self) -> &'a Allocator {
//! self.builder.allocator()
//! }
//! }
//!
//! impl<'a> MyAstProcessor<'a> {
//! pub fn new(allocator: &'a Allocator) -> Self {
//! let builder = AstBuilder::new(allocator);
//! Self { builder }
//! }
//!
//! /// Create a `Vec` of 3 x `null` expressions.
//! pub fn null_literals_array(&self) -> ArenaVec<'a, Expression<'a>> {
//! // Can just pass `self` to all these methods, because `GetAstBuilder`
//! // and `GetAllocator` are implemented on `MyAstProcessor`
//! ArenaVec::from_array_in(
//! [
//! Expression::new_null_literal(SPAN, self),
//! Expression::new_null_literal(SPAN, self),
//! Expression::new_null_literal(SPAN, self),
//! ],
//! self
//! )
//! }
//! }
//! ```
//!
//! These AST type builder methods are defined in `../generated/builder_methods.rs` and `custom.rs`.
//!
//! ## Migration
//!
//! To minimize immediate breaking changes for downstream consumers, and allow them to migrate incrementally,
//! at present `AstBuilder` still has its own methods, but can also be passed to the AST type builder methods.
//!
//! Once a project has migrated, they should enable the `disable_old_builder` Cargo feature,
//! which will remove the old-style own methods on `AstBuilder`.
//!
//! After a few weeks, we will remove `AstBuilder`'s own methods entirely.
//!
//! ## Oxc
//!
//! All Oxc crates have been migrated to the new usage.
//!
//! `disable_old_builder` Cargo feature is enabled in all Oxc crates which utilize `AstBuilder`.
//! Where those crates expose the `AstBuilder` they use to user code, the feature is only enabled in tests.
use ;
use NodeId;
/// Trait for types which can create AST nodes.
///
/// Implemented by [`AstBuilder`], and provides the memory arena and [`NodeId`]s used by the AST node
/// builder methods defined on AST types (e.g. `Statement::new_expression_statement`).
///
/// AST node builder methods are generic over `A: GetAstBuilder<'a>`, so they can be called with a
/// builder directly, or with a type which exposes one by implementing [`GetAstBuilder`]
/// (e.g. parser or traverse context).
///
/// Further [`AstBuild`] implementations will be added later:
///
/// * Version for parser which counts AST nodes (to accurately pre-allocate `Vec`s in `SemanticBuilder`).
/// * Version for transformer/minifier which assigns unique [`NodeId`]s to all AST nodes.
/// Trait for types which provide access to an [`AstBuild`]er.
///
/// Implemented by the [`AstBuild`]ers themselves (returning `self`), and by types which hold one
/// (e.g. parser or traverse context). AST node builder methods are generic over `A: GetAstBuilder<'a>`,
/// so they can be called with a builder directly, or with a type which holds one.
/// AST builder which assigns dummy [`NodeId`]s to AST nodes.
///
/// For use where no `NodeId`s are required on AST nodes as they are built
/// e.g. parser, because `NodeId`s are assigned later when building `Semantic`.
/// [`AstBuilder`] implements [`GetAstBuilder`] so it can be passed directly to AST build methods.
/// Type that can be used in any AST builder method call which requires an `IntoIn<'a, Option<Anything<'a>>>`.
/// Pass `NONE` instead of `None::<Anything<'a>>`.
;