Skip to content

Commit

Permalink
Fix server actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Sep 5, 2024
1 parent 52fb420 commit ff9d4f6
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use swc_core::{
comments::{Comment, CommentKind, Comments},
errors::HANDLER,
util::take::Take,
BytePos, FileName, Span, SyntaxContext, DUMMY_SP,
BytePos, FileName, Mark, Span, SyntaxContext, DUMMY_SP,
},
ecma::{
ast::*,
Expand Down Expand Up @@ -68,6 +68,8 @@ pub fn server_actions<C: Comments>(
annotations: Default::default(),
extra_items: Default::default(),
export_actions: Default::default(),

args_ctxt: SyntaxContext::empty().apply_mark(Mark::new()),
})
}

Expand Down Expand Up @@ -111,6 +113,8 @@ struct ServerActions<C: Comments> {
annotations: Vec<Stmt>,
extra_items: Vec<ModuleItem>,
export_actions: Vec<String>,

args_ctxt: SyntaxContext,
}

impl<C: Comments> ServerActions<C> {
Expand Down Expand Up @@ -184,6 +188,7 @@ impl<C: Comments> ServerActions<C> {
if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body {
block.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
args_ctxt: self.args_ctxt,
});
}

Expand All @@ -207,7 +212,8 @@ impl<C: Comments> ServerActions<C> {
let mut pats = vec![];
for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident(
IdentName::new(format!("$$ACTION_ARG_{i}").into(), DUMMY_SP).into(),
Ident::new(format!("$$ACTION_ARG_{i}").into(), DUMMY_SP, self.args_ctxt)
.into(),
)));
}
let decryption_decl = VarDecl {
Expand Down Expand Up @@ -316,6 +322,7 @@ impl<C: Comments> ServerActions<C> {

f.body.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure,
args_ctxt: self.args_ctxt,
});

// export async function $ACTION_myAction () {}
Expand Down Expand Up @@ -490,7 +497,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
match f.ident.as_mut() {
None => {
let action_name = gen_ident(&mut self.reference_index);
let ident = Ident::new(action_name, DUMMY_SP, Default::default());
let ident = Ident::new_private(action_name, DUMMY_SP);
f.ident.insert(ident)
}
Some(i) => i,
Expand Down Expand Up @@ -857,10 +864,9 @@ impl<C: Comments> VisitMut for ServerActions<C> {
self.exported_idents.push((ident.to_id(), "default".into()));
} else {
// export default function() {}
let new_ident = Ident::new(
let new_ident = Ident::new_private(
gen_ident(&mut self.reference_index),
DUMMY_SP,
Default::default(),
);
f.ident = Some(new_ident.clone());
self.exported_idents
Expand All @@ -879,10 +885,9 @@ impl<C: Comments> VisitMut for ServerActions<C> {
disallowed_export_span = default_expr.span;
} else {
// export default async () => {}
let new_ident = Ident::new(
let new_ident = Ident::new_private(
gen_ident(&mut self.reference_index),
DUMMY_SP,
Default::default(),
);

self.exported_idents
Expand All @@ -901,10 +906,9 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}
Expr::Call(call) => {
// export default fn()
let new_ident = Ident::new(
let new_ident = Ident::new_private(
gen_ident(&mut self.reference_index),
DUMMY_SP,
Default::default(),
);

self.exported_idents
Expand Down Expand Up @@ -1706,6 +1710,7 @@ fn collect_decl_idents_in_stmt(stmt: &Stmt, ids: &mut Vec<Id>) {

pub(crate) struct ClosureReplacer<'a> {
used_ids: &'a [Name],
args_ctxt: SyntaxContext,
}

impl ClosureReplacer<'_> {
Expand All @@ -1724,7 +1729,7 @@ impl VisitMut for ClosureReplacer<'_> {
// $$ACTION_ARG_0
format!("$$ACTION_ARG_{index}").into(),
DUMMY_SP,
Default::default(),
self.args_ctxt,
));
}
}
Expand All @@ -1741,7 +1746,7 @@ impl VisitMut for ClosureReplacer<'_> {
// $$ACTION_ARG_0
format!("$$ACTION_ARG_{index}").into(),
DUMMY_SP,
Default::default(),
self.args_ctxt,
))),
})));
}
Expand Down

0 comments on commit ff9d4f6

Please sign in to comment.