mirror of
https://github.com/danbulant/oxc
synced 2026-05-25 12:51:57 +00:00
perf(transformer): use AstBuilder::vec_from_array (#7333)
Use `AstBuilder::vec_from_array` introduced in #7331 in the transformer, in place of creating a `Vec` with `Vec::with_capacity` and then pushing values to it.
This commit is contained in:
parent
39afb48025
commit
510b95d6c4
6 changed files with 46 additions and 60 deletions
|
|
@ -297,16 +297,14 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
|||
|
||||
{
|
||||
// Modify the wrapper function to add new body, params, and scope_id.
|
||||
let mut statements = ctx.ast.vec_with_capacity(3);
|
||||
let statement = self.create_async_to_generator_declaration(
|
||||
let async_to_gen_decl = self.create_async_to_generator_declaration(
|
||||
&bound_ident,
|
||||
params,
|
||||
body,
|
||||
generator_scope_id,
|
||||
ctx,
|
||||
);
|
||||
statements.push(statement);
|
||||
if has_function_id {
|
||||
let statements = if has_function_id {
|
||||
let id = caller_function.id.as_ref().unwrap();
|
||||
// If the function has an id, then we need to return the id.
|
||||
// `function foo() { ... }` -> `function foo() {} return foo;`
|
||||
|
|
@ -316,17 +314,17 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
|||
id.symbol_id(),
|
||||
ReferenceFlags::Read,
|
||||
);
|
||||
let statement = Statement::FunctionDeclaration(caller_function);
|
||||
statements.push(statement);
|
||||
statements.push(ctx.ast.statement_return(SPAN, Some(reference)));
|
||||
let func_decl = Statement::FunctionDeclaration(caller_function);
|
||||
let statement_return = ctx.ast.statement_return(SPAN, Some(reference));
|
||||
ctx.ast.vec_from_array([async_to_gen_decl, func_decl, statement_return])
|
||||
} else {
|
||||
// If the function doesn't have an id, then we need to return the function itself.
|
||||
// `function() { ... }` -> `return function() { ... };`
|
||||
let statement_return = ctx
|
||||
.ast
|
||||
.statement_return(SPAN, Some(Expression::FunctionExpression(caller_function)));
|
||||
statements.push(statement_return);
|
||||
}
|
||||
ctx.ast.vec_from_array([async_to_gen_decl, statement_return])
|
||||
};
|
||||
debug_assert!(wrapper_function.body.is_none());
|
||||
wrapper_function.r#async = false;
|
||||
wrapper_function.generator = false;
|
||||
|
|
@ -382,15 +380,16 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
|||
|
||||
// function _name() { _ref.apply(this, arguments); }
|
||||
{
|
||||
let mut statements = ctx.ast.vec_with_capacity(2);
|
||||
statements.push(self.create_async_to_generator_assignment(
|
||||
&bound_ident,
|
||||
params,
|
||||
body,
|
||||
generator_scope_id,
|
||||
ctx,
|
||||
));
|
||||
statements.push(Self::create_apply_call_statement(&bound_ident, ctx));
|
||||
let statements = ctx.ast.vec_from_array([
|
||||
self.create_async_to_generator_assignment(
|
||||
&bound_ident,
|
||||
params,
|
||||
body,
|
||||
generator_scope_id,
|
||||
ctx,
|
||||
),
|
||||
Self::create_apply_call_statement(&bound_ident, ctx),
|
||||
]);
|
||||
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
|
||||
|
||||
let scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
|
||||
|
|
@ -468,9 +467,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
|||
generator_function_id,
|
||||
ctx,
|
||||
);
|
||||
let mut statements = ctx.ast.vec_with_capacity(2);
|
||||
statements.push(statement);
|
||||
statements.push(caller_function);
|
||||
let statements = ctx.ast.vec_from_array([statement, caller_function]);
|
||||
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
|
||||
let params = Self::create_empty_params(ctx);
|
||||
let wrapper_function = Self::create_function(None, params, body, wrapper_scope_id, ctx);
|
||||
|
|
@ -604,9 +601,8 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
|
|||
));
|
||||
|
||||
// (this, arguments)
|
||||
let mut arguments = ctx.ast.vec_with_capacity(2);
|
||||
arguments.push(Argument::from(ctx.ast.expression_this(SPAN)));
|
||||
arguments.push(arguments_ident);
|
||||
let this = Argument::from(ctx.ast.expression_this(SPAN));
|
||||
let arguments = ctx.ast.vec_from_array([this, arguments_ident]);
|
||||
// _ref.apply
|
||||
let callee = Expression::from(ctx.ast.member_expression_static(
|
||||
SPAN,
|
||||
|
|
|
|||
|
|
@ -261,24 +261,22 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
|||
Some(ctx.ast.for_statement_init_variable_declaration(
|
||||
SPAN,
|
||||
VariableDeclarationKind::Var,
|
||||
{
|
||||
let mut items = ctx.ast.vec_with_capacity(2);
|
||||
items.push(ctx.ast.variable_declarator(
|
||||
ctx.ast.vec_from_array([
|
||||
ctx.ast.variable_declarator(
|
||||
SPAN,
|
||||
VariableDeclarationKind::Var,
|
||||
iterator_key.create_binding_pattern(ctx),
|
||||
Some(iterator),
|
||||
false,
|
||||
));
|
||||
items.push(ctx.ast.variable_declarator(
|
||||
),
|
||||
ctx.ast.variable_declarator(
|
||||
SPAN,
|
||||
VariableDeclarationKind::Var,
|
||||
step_key.create_binding_pattern(ctx),
|
||||
None,
|
||||
false,
|
||||
));
|
||||
items
|
||||
},
|
||||
),
|
||||
]),
|
||||
false,
|
||||
)),
|
||||
Some(ctx.ast.expression_assignment(
|
||||
|
|
@ -371,9 +369,8 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
|||
{
|
||||
ctx.ast.block_statement_with_scope_id(
|
||||
SPAN,
|
||||
{
|
||||
let mut items = ctx.ast.vec_with_capacity(2);
|
||||
items.push(ctx.ast.statement_expression(
|
||||
ctx.ast.vec_from_array([
|
||||
ctx.ast.statement_expression(
|
||||
SPAN,
|
||||
ctx.ast.expression_assignment(
|
||||
SPAN,
|
||||
|
|
@ -381,8 +378,8 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
|||
iterator_had_error_key.create_write_target(ctx),
|
||||
ctx.ast.expression_boolean_literal(SPAN, true),
|
||||
),
|
||||
));
|
||||
items.push(ctx.ast.statement_expression(
|
||||
),
|
||||
ctx.ast.statement_expression(
|
||||
SPAN,
|
||||
ctx.ast.expression_assignment(
|
||||
SPAN,
|
||||
|
|
@ -390,9 +387,8 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
|
|||
iterator_error_key.create_write_target(ctx),
|
||||
err_ident.create_read_expression(ctx),
|
||||
),
|
||||
));
|
||||
items
|
||||
},
|
||||
),
|
||||
]),
|
||||
block_scope_id,
|
||||
)
|
||||
},
|
||||
|
|
|
|||
|
|
@ -176,10 +176,7 @@ impl<'a, 'ctx> JsxSource<'a, 'ctx> {
|
|||
.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
|
||||
};
|
||||
|
||||
let mut properties = ctx.ast.vec_with_capacity(3);
|
||||
properties.push(filename);
|
||||
properties.push(line_number);
|
||||
properties.push(column_number);
|
||||
let properties = ctx.ast.vec_from_array([filename, line_number, column_number]);
|
||||
ctx.ast.expression_object(SPAN, properties, None)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -164,11 +164,12 @@ impl<'a, 'ctx> Traverse<'a> for ReactRefresh<'a, 'ctx> {
|
|||
));
|
||||
|
||||
let callee = self.refresh_reg.to_expression(ctx);
|
||||
let mut arguments = ctx.ast.vec_with_capacity(2);
|
||||
arguments.push(Argument::from(binding.create_read_expression(ctx)));
|
||||
arguments.push(Argument::from(
|
||||
ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(&persistent_id)),
|
||||
));
|
||||
let arguments = ctx.ast.vec_from_array([
|
||||
Argument::from(binding.create_read_expression(ctx)),
|
||||
Argument::from(
|
||||
ctx.ast.expression_string_literal(SPAN, ctx.ast.atom(&persistent_id)),
|
||||
),
|
||||
]);
|
||||
new_statements.push(ctx.ast.statement_expression(
|
||||
SPAN,
|
||||
ctx.ast.expression_call(SPAN, callee, NONE, arguments, false),
|
||||
|
|
|
|||
|
|
@ -183,12 +183,10 @@ impl<'a, 'ctx> Traverse<'a> for RegExp<'a, 'ctx> {
|
|||
ctx.create_ident_expr(SPAN, Atom::from("RegExp"), symbol_id, ReferenceFlags::read())
|
||||
};
|
||||
|
||||
let mut arguments = ctx.ast.vec_with_capacity(2);
|
||||
arguments.push(Argument::from(ctx.ast.expression_string_literal(SPAN, pattern_source)));
|
||||
|
||||
let flags_str = flags.to_string();
|
||||
let flags_str = Argument::from(ctx.ast.expression_string_literal(SPAN, flags_str));
|
||||
arguments.push(flags_str);
|
||||
let arguments = ctx.ast.vec_from_array([
|
||||
Argument::from(ctx.ast.expression_string_literal(SPAN, pattern_source)),
|
||||
Argument::from(ctx.ast.expression_string_literal(SPAN, flags.to_string())),
|
||||
]);
|
||||
|
||||
*expr = ctx.ast.expression_new(regexp.span, callee, arguments, NONE);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -491,12 +491,10 @@ impl<'a, 'ctx> TypeScriptNamespace<'a, 'ctx> {
|
|||
assignments.push(Self::create_assignment_statement(name.clone(), id.name.clone(), ctx));
|
||||
});
|
||||
|
||||
let mut stmts = ctx.ast.vec_with_capacity(2);
|
||||
stmts.push(Statement::VariableDeclaration(var_decl));
|
||||
stmts.push(
|
||||
ctx.ast.vec_from_array([
|
||||
Statement::VariableDeclaration(var_decl),
|
||||
ctx.ast.statement_expression(SPAN, ctx.ast.expression_sequence(SPAN, assignments)),
|
||||
);
|
||||
stmts
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue