Skip to content

Commit 451cf2e

Browse files
committed
refactor(ast/ast_builder)!: shorter allocator utility method names.
1 parent f211477 commit 451cf2e

34 files changed

+213
-238
lines changed

crates/oxc_ast/src/ast_builder_impl.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,34 @@ impl<'a> AstBuilder<'a> {
2727
}
2828

2929
#[inline]
30-
pub fn new_vec<T>(self) -> Vec<'a, T> {
30+
pub fn vec<T>(self) -> Vec<'a, T> {
3131
Vec::new_in(self.allocator)
3232
}
3333

3434
#[inline]
35-
pub fn new_vec_with_capacity<T>(self, capacity: usize) -> Vec<'a, T> {
35+
pub fn vec_with_capacity<T>(self, capacity: usize) -> Vec<'a, T> {
3636
Vec::with_capacity_in(capacity, self.allocator)
3737
}
3838

3939
#[inline]
40-
pub fn new_vec_single<T>(self, value: T) -> Vec<'a, T> {
41-
let mut vec = self.new_vec_with_capacity(1);
40+
pub fn vec1<T>(self, value: T) -> Vec<'a, T> {
41+
let mut vec = self.vec_with_capacity(1);
4242
vec.push(value);
4343
vec
4444
}
4545

4646
#[inline]
47-
pub fn new_vec_from_iter<T, I: IntoIterator<Item = T>>(self, iter: I) -> Vec<'a, T> {
47+
pub fn vec_from<T, I: IntoIterator<Item = T>>(self, iter: I) -> Vec<'a, T> {
4848
Vec::from_iter_in(iter, self.allocator)
4949
}
5050

5151
#[inline]
52-
pub fn new_str(self, value: &str) -> &'a str {
52+
pub fn str(self, value: &str) -> &'a str {
5353
String::from_str_in(value, self.allocator).into_bump_str()
5454
}
5555

5656
#[inline]
57-
pub fn new_atom(self, value: &str) -> Atom<'a> {
57+
pub fn atom(self, value: &str) -> Atom<'a> {
5858
Atom::from(String::from_str_in(value, self.allocator).into_bump_str())
5959
}
6060

@@ -86,7 +86,7 @@ impl<'a> AstBuilder<'a> {
8686

8787
#[inline]
8888
pub fn move_statement_vec(self, stmts: &mut Vec<'a, Statement<'a>>) -> Vec<'a, Statement<'a>> {
89-
mem::replace(stmts, self.new_vec())
89+
mem::replace(stmts, self.vec())
9090
}
9191

9292
#[inline]
@@ -100,7 +100,7 @@ impl<'a> AstBuilder<'a> {
100100
let empty_decl = self.variable_declaration(
101101
Span::default(),
102102
VariableDeclarationKind::Var,
103-
self.new_vec(),
103+
self.vec(),
104104
false,
105105
);
106106
let empty_decl = Declaration::VariableDeclaration(self.alloc(empty_decl));
@@ -128,7 +128,7 @@ impl<'a> AstBuilder<'a> {
128128
span: Span,
129129
pattern: BindingPattern<'a>,
130130
) -> FormalParameter<'a> {
131-
self.formal_parameter(span, pattern, None, false, false, self.new_vec())
131+
self.formal_parameter(span, pattern, None, false, false, self.vec())
132132
}
133133

134134
#[inline]
@@ -166,7 +166,7 @@ impl<'a> AstBuilder<'a> {
166166
self.alloc(self.export_named_declaration(
167167
span,
168168
Some(declaration),
169-
self.new_vec(),
169+
self.vec(),
170170
None,
171171
ImportOrExportKind::Value,
172172
None,

crates/oxc_isolated_declarations/src/class.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a> IsolatedDeclarations<'a> {
9090
self.ast.class_element_property_definition(
9191
property.r#type,
9292
property.span,
93-
self.ast.new_vec(),
93+
self.ast.vec(),
9494
self.ast.copy(&property.key),
9595
value,
9696
property.computed,
@@ -130,7 +130,7 @@ impl<'a> IsolatedDeclarations<'a> {
130130
self.ast.class_element_method_definition(
131131
definition.r#type,
132132
definition.span,
133-
self.ast.new_vec(),
133+
self.ast.vec(),
134134
self.ast.copy(&definition.key),
135135
value,
136136
definition.kind,
@@ -153,7 +153,7 @@ impl<'a> IsolatedDeclarations<'a> {
153153
self.ast.class_element_property_definition(
154154
r#type,
155155
SPAN,
156-
self.ast.new_vec(),
156+
self.ast.vec(),
157157
key,
158158
None,
159159
false,
@@ -181,7 +181,7 @@ impl<'a> IsolatedDeclarations<'a> {
181181
Some(self.ast.class_element_property_definition(
182182
PropertyDefinitionType::PropertyDefinition,
183183
param.span,
184-
self.ast.new_vec(),
184+
self.ast.vec(),
185185
key,
186186
None,
187187
false,
@@ -219,7 +219,7 @@ impl<'a> IsolatedDeclarations<'a> {
219219
let params = self.ast.alloc_formal_parameters(
220220
SPAN,
221221
FormalParameterKind::Signature,
222-
self.ast.new_vec(),
222+
self.ast.vec(),
223223
Option::<BindingRestElement>::None,
224224
);
225225
self.transform_class_method_definition(method, params, None)
@@ -239,7 +239,7 @@ impl<'a> IsolatedDeclarations<'a> {
239239
function: &Function<'a>,
240240
params: &FormalParameters<'a>,
241241
) -> oxc_allocator::Vec<'a, ClassElement<'a>> {
242-
let mut elements = self.ast.new_vec();
242+
let mut elements = self.ast.vec();
243243
for (index, param) in function.params.items.iter().enumerate() {
244244
if param.accessibility.is_some() || param.readonly {
245245
let type_annotation = if param.accessibility.is_some_and(|a| a.is_private()) {
@@ -278,7 +278,7 @@ impl<'a> IsolatedDeclarations<'a> {
278278
let Some(name) = method.key.static_name() else {
279279
continue;
280280
};
281-
let name = self.ast.new_atom(&name);
281+
let name = self.ast.atom(&name);
282282
if inferred_accessor_types.contains_key(&name) {
283283
// We've inferred that accessor type already
284284
continue;
@@ -337,7 +337,7 @@ impl<'a> IsolatedDeclarations<'a> {
337337
}
338338

339339
let mut has_private_key = false;
340-
let mut elements = self.ast.new_vec();
340+
let mut elements = self.ast.vec();
341341
let mut is_function_overloads = false;
342342
for element in &decl.body.body {
343343
match element {
@@ -373,7 +373,7 @@ impl<'a> IsolatedDeclarations<'a> {
373373
self.transform_set_accessor_params(
374374
&function.params,
375375
inferred_accessor_types
376-
.get(&self.ast.new_atom(&n))
376+
.get(&self.ast.atom(&n))
377377
.map(|t| self.ast.copy(t)),
378378
)
379379
},
@@ -403,7 +403,7 @@ impl<'a> IsolatedDeclarations<'a> {
403403
MethodDefinitionKind::Get => {
404404
let rt = method.key.static_name().and_then(|name| {
405405
inferred_accessor_types
406-
.get(&self.ast.new_atom(&name))
406+
.get(&self.ast.atom(&name))
407407
.map(|t| self.ast.copy(t))
408408
});
409409
if rt.is_none() {
@@ -448,7 +448,7 @@ impl<'a> IsolatedDeclarations<'a> {
448448
None,
449449
property.computed,
450450
property.r#static,
451-
self.ast.new_vec(),
451+
self.ast.vec(),
452452
);
453453
elements.push(new_element);
454454
}
@@ -462,7 +462,7 @@ impl<'a> IsolatedDeclarations<'a> {
462462
// Prevents other classes with the same public members from being used in place of the current class
463463
let ident = self.ast.property_key_private_identifier(SPAN, "private");
464464
let r#type = PropertyDefinitionType::PropertyDefinition;
465-
let decorators = self.ast.new_vec();
465+
let decorators = self.ast.vec();
466466
let element = self.ast.class_element_property_definition(
467467
r#type,
468468
SPAN,
@@ -488,7 +488,7 @@ impl<'a> IsolatedDeclarations<'a> {
488488
Some(self.ast.alloc_class(
489489
decl.r#type,
490490
decl.span,
491-
self.ast.new_vec(),
491+
self.ast.vec(),
492492
self.ast.copy(&decl.id),
493493
self.ast.copy(&decl.super_class),
494494
body,
@@ -525,8 +525,8 @@ impl<'a> IsolatedDeclarations<'a> {
525525
) -> Box<'a, FormalParameters<'a>> {
526526
let pattern = BindingPattern { kind, type_annotation, optional: false };
527527
let parameter =
528-
self.ast.formal_parameter(SPAN, pattern, None, false, false, self.ast.new_vec());
529-
let items = self.ast.new_vec_single(parameter);
528+
self.ast.formal_parameter(SPAN, pattern, None, false, false, self.ast.vec());
529+
let items = self.ast.vec1(parameter);
530530
self.ast.alloc_formal_parameters(
531531
SPAN,
532532
FormalParameterKind::Signature,

crates/oxc_isolated_declarations/src/declaration.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'a> IsolatedDeclarations<'a> {
2323
None
2424
} else {
2525
let declarations =
26-
self.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
26+
self.ast.vec_from(decl.declarations.iter().filter_map(|declarator| {
2727
self.transform_variable_declarator(declarator, check_binding)
2828
}));
2929
Some(self.transform_variable_declaration_with_new_declarations(decl, declarations))
@@ -38,7 +38,7 @@ impl<'a> IsolatedDeclarations<'a> {
3838
self.ast.alloc_variable_declaration(
3939
decl.span,
4040
decl.kind,
41-
self.ast.new_vec_from_iter(declarations),
41+
self.ast.vec_from(declarations),
4242
self.is_declare(),
4343
)
4444
}
@@ -110,10 +110,9 @@ impl<'a> IsolatedDeclarations<'a> {
110110
decl: &UsingDeclaration<'a>,
111111
check_binding: bool,
112112
) -> Box<'a, VariableDeclaration<'a>> {
113-
let declarations =
114-
self.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
115-
self.transform_variable_declarator(declarator, check_binding)
116-
}));
113+
let declarations = self.ast.vec_from(decl.declarations.iter().filter_map(|declarator| {
114+
self.transform_variable_declarator(declarator, check_binding)
115+
}));
117116
self.transform_using_declaration_with_new_declarations(decl, declarations)
118117
}
119118

@@ -138,7 +137,7 @@ impl<'a> IsolatedDeclarations<'a> {
138137
self.scope.enter_scope(ScopeFlags::TsModuleBlock);
139138
let stmts = self.transform_statements_on_demand(&block.body);
140139
self.scope.leave_scope();
141-
self.ast.alloc_ts_module_block(SPAN, self.ast.new_vec(), stmts)
140+
self.ast.alloc_ts_module_block(SPAN, self.ast.vec(), stmts)
142141
}
143142

144143
pub fn transform_ts_module_declaration(

crates/oxc_isolated_declarations/src/enum.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'a> IsolatedDeclarations<'a> {
2020
&mut self,
2121
decl: &TSEnumDeclaration<'a>,
2222
) -> Option<Declaration<'a>> {
23-
let mut members = self.ast.new_vec();
23+
let mut members = self.ast.vec();
2424
let mut prev_initializer_value = Some(ConstantValue::Number(-1.0));
2525
let mut prev_members = FxHashMap::default();
2626
for member in &decl.members {
@@ -63,10 +63,7 @@ impl<'a> IsolatedDeclarations<'a> {
6363

6464
// Infinity
6565
let expr = if v.is_infinite() {
66-
self.ast.expression_identifier_reference(
67-
SPAN,
68-
self.ast.new_atom("Infinity"),
69-
)
66+
self.ast.expression_identifier_reference(SPAN, "Infinity")
7067
} else {
7168
let value = if is_negative { -v } else { v };
7269
self.ast.expression_numeric_literal(

crates/oxc_isolated_declarations/src/function.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'a> IsolatedDeclarations<'a> {
9393
SPAN,
9494
self.ast.ts_type_union_type(
9595
SPAN,
96-
self.ast.new_vec_from_iter([
96+
self.ast.vec_from([
9797
ts_type,
9898
self.ast.ts_type_undefined_keyword(SPAN),
9999
]),
@@ -113,7 +113,7 @@ impl<'a> IsolatedDeclarations<'a> {
113113
);
114114
}
115115

116-
Some(self.ast.formal_parameter(param.span, pattern, None, false, false, self.ast.new_vec()))
116+
Some(self.ast.formal_parameter(param.span, pattern, None, false, false, self.ast.vec()))
117117
}
118118

119119
pub fn transform_formal_parameters(
@@ -124,15 +124,14 @@ impl<'a> IsolatedDeclarations<'a> {
124124
return self.ast.alloc(self.ast.copy(params));
125125
}
126126

127-
let items = self.ast.new_vec_from_iter(params.items.iter().enumerate().filter_map(
128-
|(index, item)| {
127+
let items =
128+
self.ast.vec_from(params.items.iter().enumerate().filter_map(|(index, item)| {
129129
let is_remaining_params_have_required =
130130
params.items.iter().skip(index).any(|item| {
131131
!(item.pattern.optional || item.pattern.kind.is_assignment_pattern())
132132
});
133133
self.transform_formal_parameter(item, is_remaining_params_have_required)
134-
},
135-
));
134+
}));
136135

137136
if let Some(rest) = &params.rest {
138137
if rest.argument.type_annotation.is_none() {

crates/oxc_isolated_declarations/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'a> IsolatedDeclarations<'a> {
5656
/// Returns `Vec<Error>` if any errors were collected during the transformation.
5757
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
5858
let source_type = SourceType::default().with_module(true).with_typescript_definition(true);
59-
let directives = self.ast.new_vec();
59+
let directives = self.ast.vec();
6060
let stmts = self.transform_program(program);
6161
let program = self.ast.program(SPAN, source_type, directives, None, stmts);
6262
IsolatedDeclarationsReturn { program, errors: self.take_errors() }
@@ -98,7 +98,7 @@ impl<'a> IsolatedDeclarations<'a> {
9898
&mut self,
9999
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
100100
) -> oxc_allocator::Vec<'a, Statement<'a>> {
101-
let mut new_ast_stmts = self.ast.new_vec::<Statement<'a>>();
101+
let mut new_ast_stmts = self.ast.vec::<Statement<'a>>();
102102
for stmt in Self::remove_function_overloads_implementation(self.ast.copy(stmts)) {
103103
if let Some(decl) = stmt.as_declaration() {
104104
if let Some(decl) = self.transform_declaration(decl, false) {
@@ -256,7 +256,7 @@ impl<'a> IsolatedDeclarations<'a> {
256256

257257
// 6. Transform variable/using declarations, import statements, remove unused imports
258258
// 7. Return transformed statements
259-
let mut new_ast_stmts = self.ast.new_vec_with_capacity(transformed_indexes.len());
259+
let mut new_ast_stmts = self.ast.vec_with_capacity(transformed_indexes.len());
260260
for (index, stmt) in new_stmts.into_iter().enumerate() {
261261
match stmt {
262262
_ if transformed_indexes.contains(&index) => {
@@ -272,7 +272,7 @@ impl<'a> IsolatedDeclarations<'a> {
272272
let variables_declaration = self
273273
.transform_variable_declaration_with_new_declarations(
274274
&decl,
275-
self.ast.new_vec_from_iter(
275+
self.ast.vec_from(
276276
declarations
277277
.into_iter()
278278
.enumerate()
@@ -294,7 +294,7 @@ impl<'a> IsolatedDeclarations<'a> {
294294
let variable_declaration = self
295295
.transform_using_declaration_with_new_declarations(
296296
&decl,
297-
self.ast.new_vec_from_iter(
297+
self.ast.vec_from(
298298
declarations
299299
.into_iter()
300300
.enumerate()
@@ -323,7 +323,7 @@ impl<'a> IsolatedDeclarations<'a> {
323323
}
324324

325325
if need_empty_export_marker {
326-
let specifiers = self.ast.new_vec();
326+
let specifiers = self.ast.vec();
327327
let kind = ImportOrExportKind::Value;
328328
let empty_export =
329329
self.ast.alloc_export_named_declaration(SPAN, None, specifiers, None, kind, None);

0 commit comments

Comments
 (0)