Unverified Commit cbd3c298 authored by LongYinan's avatar LongYinan Committed by GitHub

Cleanup remove directive logic to reduce allocate (#47055)




Reduce `unwrap` and `clone` usages
parent 25aa8693
...@@ -97,13 +97,11 @@ impl<C: Comments> ServerActions<C> { ...@@ -97,13 +97,11 @@ impl<C: Comments> ServerActions<C> {
} else { } else {
// Check if the function has `"use server"` // Check if the function has `"use server"`
if let Some(body) = maybe_body { if let Some(body) = maybe_body {
let directive_index = get_server_directive_index_in_fn(&body.stmts); remove_server_directive_index_in_fn(
if directive_index >= 0 { &mut body.stmts,
is_action_fn = true; remove_directive,
if remove_directive { &mut is_action_fn,
body.stmts.remove(directive_index.try_into().unwrap()); );
}
}
} }
} }
...@@ -339,16 +337,17 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -339,16 +337,17 @@ impl<C: Comments> VisitMut for ServerActions<C> {
.emit(); .emit();
}); });
} else if !self.in_action_file { } else if !self.in_action_file {
if f.ident.is_none() { let ident = match f.ident.as_mut() {
let action_name = gen_ident(&mut self.ident_cnt); None => {
f.ident = Some(Ident::new(action_name, DUMMY_SP)); let action_name = gen_ident(&mut self.ident_cnt);
} let ident = Ident::new(action_name, DUMMY_SP);
f.ident.insert(ident)
}
Some(i) => i,
};
let (maybe_new_fn, _) = self.add_action_annotations_and_maybe_hoist( let (maybe_new_fn, _) =
f.ident.as_mut().unwrap(), self.add_action_annotations_and_maybe_hoist(ident, Some(&mut f.function), None);
Some(&mut f.function),
None,
);
if let Some(new_fn) = maybe_new_fn { if let Some(new_fn) = maybe_new_fn {
f.function = new_fn; f.function = new_fn;
...@@ -536,12 +535,11 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -536,12 +535,11 @@ impl<C: Comments> VisitMut for ServerActions<C> {
} }
fn visit_mut_module_items(&mut self, stmts: &mut Vec<ModuleItem>) { fn visit_mut_module_items(&mut self, stmts: &mut Vec<ModuleItem>) {
let directive_index = get_server_directive_index_in_module(stmts); remove_server_directive_index_in_module(
if directive_index >= 0 { stmts,
self.in_action_file = true; &mut self.in_action_file,
self.has_action = true; &mut self.has_action,
stmts.remove(directive_index.try_into().unwrap()); );
}
let old_annotations = self.annotations.take(); let old_annotations = self.annotations.take();
let mut new = Vec::with_capacity(stmts.len()); let mut new = Vec::with_capacity(stmts.len());
...@@ -857,40 +855,47 @@ fn annotate_ident_as_action( ...@@ -857,40 +855,47 @@ fn annotate_ident_as_action(
)); ));
} }
fn get_server_directive_index_in_module(stmts: &[ModuleItem]) -> i32 { fn remove_server_directive_index_in_module(
for (i, stmt) in stmts.iter().enumerate() { stmts: &mut Vec<ModuleItem>,
if let ModuleItem::Stmt(Stmt::Expr(first)) = stmt { in_action_file: &mut bool,
match &*first.expr { has_action: &mut bool,
Expr::Lit(Lit::Str(Str { value, .. })) => { ) {
if value == "use server" { stmts.retain(|stmt| {
return i as i32; if let ModuleItem::Stmt(Stmt::Expr(ExprStmt {
} expr: box Expr::Lit(Lit::Str(Str { value, .. })),
} ..
_ => return -1, })) = stmt
{
if value == "use server" {
*in_action_file = true;
*has_action = true;
return false;
} }
} else {
return -1;
} }
} true
-1 });
} }
fn get_server_directive_index_in_fn(stmts: &[Stmt]) -> i32 { fn remove_server_directive_index_in_fn(
for (i, stmt) in stmts.iter().enumerate() { stmts: &mut Vec<Stmt>,
if let Stmt::Expr(first) = stmt { remove_directive: bool,
match &*first.expr { is_action_fn: &mut bool,
Expr::Lit(Lit::Str(Str { value, .. })) => { ) {
if value == "use server" { stmts.retain(|stmt| {
return i as i32; if let Stmt::Expr(ExprStmt {
} expr: box Expr::Lit(Lit::Str(Str { value, .. })),
..
}) = stmt
{
if value == "use server" {
*is_action_fn = true;
if remove_directive {
return false;
} }
_ => return -1,
} }
} else {
return -1;
} }
} true
-1 });
} }
fn collect_idents_in_array_pat(elems: &[Option<Pat>]) -> Vec<Id> { fn collect_idents_in_array_pat(elems: &[Option<Pat>]) -> Vec<Id> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment