-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathast_builder_impl.rs
225 lines (193 loc) · 6.28 KB
/
ast_builder_impl.rs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#![allow(
clippy::fn_params_excessive_bools,
clippy::must_use_candidate, // must_use_candidate is too annoying for this file
clippy::too_many_arguments,
clippy::unused_self,
)]
use std::mem;
use oxc_allocator::{Allocator, Box, FromIn, String, Vec};
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::{number::NumberBase, operator::UnaryOperator};
#[allow(clippy::wildcard_imports)]
use crate::ast::*;
use crate::AstBuilder;
/// Type that can be used in any AST builder method call which requires an `IntoIn<'a, Anything<'a>>`.
/// Pass `NONE` instead of `None::<Anything<'a>>`.
#[allow(clippy::upper_case_acronyms)]
pub struct NONE;
impl<'a, T> FromIn<'a, NONE> for Option<Box<'a, T>> {
fn from_in(_: NONE, _: &'a Allocator) -> Self {
None
}
}
impl<'a> AstBuilder<'a> {
#[inline]
pub fn new(allocator: &'a Allocator) -> Self {
Self { allocator }
}
#[inline]
pub fn alloc<T>(self, value: T) -> Box<'a, T> {
Box::new_in(value, self.allocator)
}
#[inline]
pub fn vec<T>(self) -> Vec<'a, T> {
Vec::new_in(self.allocator)
}
#[inline]
pub fn vec_with_capacity<T>(self, capacity: usize) -> Vec<'a, T> {
Vec::with_capacity_in(capacity, self.allocator)
}
#[inline]
pub fn vec1<T>(self, value: T) -> Vec<'a, T> {
let mut vec = self.vec_with_capacity(1);
vec.push(value);
vec
}
#[inline]
pub fn vec_from_iter<T, I: IntoIterator<Item = T>>(self, iter: I) -> Vec<'a, T> {
Vec::from_iter_in(iter, self.allocator)
}
#[inline]
pub fn str(self, value: &str) -> &'a str {
String::from_str_in(value, self.allocator).into_bump_str()
}
#[inline]
pub fn atom(self, value: &str) -> Atom<'a> {
Atom::from(String::from_str_in(value, self.allocator).into_bump_str())
}
/// # SAFETY
/// This method is completely unsound and should not be used.
/// We need to remove all uses of it. Please don't add any more!
/// <https://github.com/oxc-project/oxc/issues/3483>
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn copy<T>(self, src: &T) -> T {
// SAFETY: Not safe (see above)
unsafe { std::mem::transmute_copy(src) }
}
/// Moves the expression out by replacing it with a null expression.
#[inline]
pub fn move_expression(self, expr: &mut Expression<'a>) -> Expression<'a> {
let null_expr = self.expression_null_literal(expr.span());
mem::replace(expr, null_expr)
}
#[inline]
pub fn move_statement(self, stmt: &mut Statement<'a>) -> Statement<'a> {
let empty_stmt = self.empty_statement(stmt.span());
mem::replace(stmt, Statement::EmptyStatement(self.alloc(empty_stmt)))
}
#[inline]
pub fn move_assignment_target(self, target: &mut AssignmentTarget<'a>) -> AssignmentTarget<'a> {
let dummy =
self.simple_assignment_target_identifier_reference(Span::default(), Atom::from(""));
mem::replace(target, dummy.into())
}
#[inline]
pub fn move_declaration(self, decl: &mut Declaration<'a>) -> Declaration<'a> {
let empty_decl = self.variable_declaration(
Span::default(),
VariableDeclarationKind::Var,
self.vec(),
false,
);
let empty_decl = Declaration::VariableDeclaration(self.alloc(empty_decl));
mem::replace(decl, empty_decl)
}
#[inline]
pub fn move_vec<T>(self, vec: &mut Vec<'a, T>) -> Vec<'a, T> {
mem::replace(vec, self.vec())
}
/* ---------- Constructors ---------- */
/// `0`
#[inline]
pub fn number_0(self) -> Expression<'a> {
self.expression_numeric_literal(Span::default(), 0.0, "0", NumberBase::Decimal)
}
/// `void 0`
#[inline]
pub fn void_0(self) -> Expression<'a> {
let num = self.number_0();
Expression::UnaryExpression(self.alloc(self.unary_expression(
Span::default(),
UnaryOperator::Void,
num,
)))
}
/* ---------- Functions ---------- */
#[inline]
pub fn plain_formal_parameter(
self,
span: Span,
pattern: BindingPattern<'a>,
) -> FormalParameter<'a> {
self.formal_parameter(span, self.vec(), pattern, None, false, false)
}
#[inline]
pub fn plain_function(
self,
r#type: FunctionType,
span: Span,
id: Option<BindingIdentifier<'a>>,
params: FormalParameters<'a>,
body: Option<FunctionBody<'a>>,
) -> Box<'a, Function<'a>> {
self.alloc(
self.function(r#type, span, id, false, false, false, NONE, NONE, params, NONE, body),
)
}
/* ---------- Modules ---------- */
#[inline]
pub fn plain_export_named_declaration_declaration(
self,
span: Span,
declaration: Declaration<'a>,
) -> Box<'a, ExportNamedDeclaration<'a>> {
self.alloc(self.export_named_declaration(
span,
Some(declaration),
self.vec(),
None,
ImportOrExportKind::Value,
NONE,
))
}
#[inline]
pub fn plain_export_named_declaration(
self,
span: Span,
specifiers: Vec<'a, ExportSpecifier<'a>>,
source: Option<StringLiteral<'a>>,
) -> Box<'a, ExportNamedDeclaration<'a>> {
self.alloc(self.export_named_declaration(
span,
None,
specifiers,
source,
ImportOrExportKind::Value,
NONE,
))
}
/* ---------- TypeScript ---------- */
#[inline]
pub fn ts_interface_heritages(
self,
extends: Vec<'a, (Expression<'a>, Option<Box<'a, TSTypeParameterInstantiation<'a>>>, Span)>,
) -> Vec<'a, TSInterfaceHeritage<'a>> {
Vec::from_iter_in(
extends.into_iter().map(|(expression, type_parameters, span)| TSInterfaceHeritage {
span,
expression,
type_parameters,
}),
self.allocator,
)
}
#[inline]
pub fn jsx_opening_fragment(self, span: Span) -> JSXOpeningFragment {
JSXOpeningFragment { span }
}
#[inline]
pub fn jsx_closing_fragment(self, span: Span) -> JSXClosingFragment {
JSXClosingFragment { span }
}
}