Skip to content

Commit

Permalink
refactor(ast/ast_builder)!: shorter allocator utility method names.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Jul 8, 2024
1 parent f211477 commit 451cf2e
Show file tree
Hide file tree
Showing 34 changed files with 213 additions and 238 deletions.
22 changes: 11 additions & 11 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,34 @@ impl<'a> AstBuilder<'a> {
}

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

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

#[inline]
pub fn new_vec_single<T>(self, value: T) -> Vec<'a, T> {
let mut vec = self.new_vec_with_capacity(1);
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 new_vec_from_iter<T, I: IntoIterator<Item = T>>(self, iter: I) -> Vec<'a, T> {
pub fn vec_from<T, I: IntoIterator<Item = T>>(self, iter: I) -> Vec<'a, T> {
Vec::from_iter_in(iter, self.allocator)
}

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

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

Expand Down Expand Up @@ -86,7 +86,7 @@ impl<'a> AstBuilder<'a> {

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

#[inline]
Expand All @@ -100,7 +100,7 @@ impl<'a> AstBuilder<'a> {
let empty_decl = self.variable_declaration(
Span::default(),
VariableDeclarationKind::Var,
self.new_vec(),
self.vec(),
false,
);
let empty_decl = Declaration::VariableDeclaration(self.alloc(empty_decl));
Expand Down Expand Up @@ -128,7 +128,7 @@ impl<'a> AstBuilder<'a> {
span: Span,
pattern: BindingPattern<'a>,
) -> FormalParameter<'a> {
self.formal_parameter(span, pattern, None, false, false, self.new_vec())
self.formal_parameter(span, pattern, None, false, false, self.vec())
}

#[inline]
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<'a> AstBuilder<'a> {
self.alloc(self.export_named_declaration(
span,
Some(declaration),
self.new_vec(),
self.vec(),
None,
ImportOrExportKind::Value,
None,
Expand Down
30 changes: 15 additions & 15 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.class_element_property_definition(
property.r#type,
property.span,
self.ast.new_vec(),
self.ast.vec(),
self.ast.copy(&property.key),
value,
property.computed,
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.class_element_method_definition(
definition.r#type,
definition.span,
self.ast.new_vec(),
self.ast.vec(),
self.ast.copy(&definition.key),
value,
definition.kind,
Expand All @@ -153,7 +153,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.class_element_property_definition(
r#type,
SPAN,
self.ast.new_vec(),
self.ast.vec(),
key,
None,
false,
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a> IsolatedDeclarations<'a> {
Some(self.ast.class_element_property_definition(
PropertyDefinitionType::PropertyDefinition,
param.span,
self.ast.new_vec(),
self.ast.vec(),
key,
None,
false,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'a> IsolatedDeclarations<'a> {
let params = self.ast.alloc_formal_parameters(
SPAN,
FormalParameterKind::Signature,
self.ast.new_vec(),
self.ast.vec(),
Option::<BindingRestElement>::None,
);
self.transform_class_method_definition(method, params, None)
Expand All @@ -239,7 +239,7 @@ impl<'a> IsolatedDeclarations<'a> {
function: &Function<'a>,
params: &FormalParameters<'a>,
) -> oxc_allocator::Vec<'a, ClassElement<'a>> {
let mut elements = self.ast.new_vec();
let mut elements = self.ast.vec();
for (index, param) in function.params.items.iter().enumerate() {
if param.accessibility.is_some() || param.readonly {
let type_annotation = if param.accessibility.is_some_and(|a| a.is_private()) {
Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'a> IsolatedDeclarations<'a> {
let Some(name) = method.key.static_name() else {
continue;
};
let name = self.ast.new_atom(&name);
let name = self.ast.atom(&name);
if inferred_accessor_types.contains_key(&name) {
// We've inferred that accessor type already
continue;
Expand Down Expand Up @@ -337,7 +337,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

let mut has_private_key = false;
let mut elements = self.ast.new_vec();
let mut elements = self.ast.vec();
let mut is_function_overloads = false;
for element in &decl.body.body {
match element {
Expand Down Expand Up @@ -373,7 +373,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.transform_set_accessor_params(
&function.params,
inferred_accessor_types
.get(&self.ast.new_atom(&n))
.get(&self.ast.atom(&n))
.map(|t| self.ast.copy(t)),
)
},
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<'a> IsolatedDeclarations<'a> {
MethodDefinitionKind::Get => {
let rt = method.key.static_name().and_then(|name| {
inferred_accessor_types
.get(&self.ast.new_atom(&name))
.get(&self.ast.atom(&name))
.map(|t| self.ast.copy(t))
});
if rt.is_none() {
Expand Down Expand Up @@ -448,7 +448,7 @@ impl<'a> IsolatedDeclarations<'a> {
None,
property.computed,
property.r#static,
self.ast.new_vec(),
self.ast.vec(),
);
elements.push(new_element);
}
Expand All @@ -462,7 +462,7 @@ impl<'a> IsolatedDeclarations<'a> {
// Prevents other classes with the same public members from being used in place of the current class
let ident = self.ast.property_key_private_identifier(SPAN, "private");
let r#type = PropertyDefinitionType::PropertyDefinition;
let decorators = self.ast.new_vec();
let decorators = self.ast.vec();
let element = self.ast.class_element_property_definition(
r#type,
SPAN,
Expand All @@ -488,7 +488,7 @@ impl<'a> IsolatedDeclarations<'a> {
Some(self.ast.alloc_class(
decl.r#type,
decl.span,
self.ast.new_vec(),
self.ast.vec(),
self.ast.copy(&decl.id),
self.ast.copy(&decl.super_class),
body,
Expand Down Expand Up @@ -525,8 +525,8 @@ impl<'a> IsolatedDeclarations<'a> {
) -> Box<'a, FormalParameters<'a>> {
let pattern = BindingPattern { kind, type_annotation, optional: false };
let parameter =
self.ast.formal_parameter(SPAN, pattern, None, false, false, self.ast.new_vec());
let items = self.ast.new_vec_single(parameter);
self.ast.formal_parameter(SPAN, pattern, None, false, false, self.ast.vec());
let items = self.ast.vec1(parameter);
self.ast.alloc_formal_parameters(
SPAN,
FormalParameterKind::Signature,
Expand Down
13 changes: 6 additions & 7 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> IsolatedDeclarations<'a> {
None
} else {
let declarations =
self.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
self.ast.vec_from(decl.declarations.iter().filter_map(|declarator| {
self.transform_variable_declarator(declarator, check_binding)
}));
Some(self.transform_variable_declaration_with_new_declarations(decl, declarations))
Expand All @@ -38,7 +38,7 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.alloc_variable_declaration(
decl.span,
decl.kind,
self.ast.new_vec_from_iter(declarations),
self.ast.vec_from(declarations),
self.is_declare(),
)
}
Expand Down Expand Up @@ -110,10 +110,9 @@ impl<'a> IsolatedDeclarations<'a> {
decl: &UsingDeclaration<'a>,
check_binding: bool,
) -> Box<'a, VariableDeclaration<'a>> {
let declarations =
self.ast.new_vec_from_iter(decl.declarations.iter().filter_map(|declarator| {
self.transform_variable_declarator(declarator, check_binding)
}));
let declarations = self.ast.vec_from(decl.declarations.iter().filter_map(|declarator| {
self.transform_variable_declarator(declarator, check_binding)
}));
self.transform_using_declaration_with_new_declarations(decl, declarations)
}

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

pub fn transform_ts_module_declaration(
Expand Down
7 changes: 2 additions & 5 deletions crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> IsolatedDeclarations<'a> {
&mut self,
decl: &TSEnumDeclaration<'a>,
) -> Option<Declaration<'a>> {
let mut members = self.ast.new_vec();
let mut members = self.ast.vec();
let mut prev_initializer_value = Some(ConstantValue::Number(-1.0));
let mut prev_members = FxHashMap::default();
for member in &decl.members {
Expand Down Expand Up @@ -63,10 +63,7 @@ impl<'a> IsolatedDeclarations<'a> {

// Infinity
let expr = if v.is_infinite() {
self.ast.expression_identifier_reference(
SPAN,
self.ast.new_atom("Infinity"),
)
self.ast.expression_identifier_reference(SPAN, "Infinity")
} else {
let value = if is_negative { -v } else { v };
self.ast.expression_numeric_literal(
Expand Down
11 changes: 5 additions & 6 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'a> IsolatedDeclarations<'a> {
SPAN,
self.ast.ts_type_union_type(
SPAN,
self.ast.new_vec_from_iter([
self.ast.vec_from([
ts_type,
self.ast.ts_type_undefined_keyword(SPAN),
]),
Expand All @@ -113,7 +113,7 @@ impl<'a> IsolatedDeclarations<'a> {
);
}

Some(self.ast.formal_parameter(param.span, pattern, None, false, false, self.ast.new_vec()))
Some(self.ast.formal_parameter(param.span, pattern, None, false, false, self.ast.vec()))
}

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

let items = self.ast.new_vec_from_iter(params.items.iter().enumerate().filter_map(
|(index, item)| {
let items =
self.ast.vec_from(params.items.iter().enumerate().filter_map(|(index, item)| {
let is_remaining_params_have_required =
params.items.iter().skip(index).any(|item| {
!(item.pattern.optional || item.pattern.kind.is_assignment_pattern())
});
self.transform_formal_parameter(item, is_remaining_params_have_required)
},
));
}));

if let Some(rest) = &params.rest {
if rest.argument.type_annotation.is_none() {
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> IsolatedDeclarations<'a> {
/// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
let source_type = SourceType::default().with_module(true).with_typescript_definition(true);
let directives = self.ast.new_vec();
let directives = self.ast.vec();
let stmts = self.transform_program(program);
let program = self.ast.program(SPAN, source_type, directives, None, stmts);
IsolatedDeclarationsReturn { program, errors: self.take_errors() }
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'a> IsolatedDeclarations<'a> {
&mut self,
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
let mut new_ast_stmts = self.ast.new_vec::<Statement<'a>>();
let mut new_ast_stmts = self.ast.vec::<Statement<'a>>();
for stmt in Self::remove_function_overloads_implementation(self.ast.copy(stmts)) {
if let Some(decl) = stmt.as_declaration() {
if let Some(decl) = self.transform_declaration(decl, false) {
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<'a> IsolatedDeclarations<'a> {

// 6. Transform variable/using declarations, import statements, remove unused imports
// 7. Return transformed statements
let mut new_ast_stmts = self.ast.new_vec_with_capacity(transformed_indexes.len());
let mut new_ast_stmts = self.ast.vec_with_capacity(transformed_indexes.len());
for (index, stmt) in new_stmts.into_iter().enumerate() {
match stmt {
_ if transformed_indexes.contains(&index) => {
Expand All @@ -272,7 +272,7 @@ impl<'a> IsolatedDeclarations<'a> {
let variables_declaration = self
.transform_variable_declaration_with_new_declarations(
&decl,
self.ast.new_vec_from_iter(
self.ast.vec_from(
declarations
.into_iter()
.enumerate()
Expand All @@ -294,7 +294,7 @@ impl<'a> IsolatedDeclarations<'a> {
let variable_declaration = self
.transform_using_declaration_with_new_declarations(
&decl,
self.ast.new_vec_from_iter(
self.ast.vec_from(
declarations
.into_iter()
.enumerate()
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

if need_empty_export_marker {
let specifiers = self.ast.new_vec();
let specifiers = self.ast.vec();
let kind = ImportOrExportKind::Value;
let empty_export =
self.ast.alloc_export_named_declaration(SPAN, None, specifiers, None, kind, None);
Expand Down
Loading

0 comments on commit 451cf2e

Please sign in to comment.