Unverified Commit ddff41a9 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Support re-exporting unnamed function expression (#46936)

Add support for cases like:

```js
const foo = async function () {}
export default foo

const bar = async function() {}
export { bar }
```

Also fix a bug where nested async function declarations are mistakenly
counted as actions.

Fixes NEXT-800.
parent 80f2293a
...@@ -126,19 +126,11 @@ impl<C: Comments> ServerActions<C> { ...@@ -126,19 +126,11 @@ impl<C: Comments> ServerActions<C> {
fn add_action_annotations( fn add_action_annotations(
&mut self, &mut self,
ident: &mut Ident, ident: &Ident,
function: &mut Box<Function>, function: &mut Box<Function>,
is_exported: bool, is_exported: bool,
is_default_export: bool, is_default_export: bool,
) -> Option<Box<Function>> { ) -> Option<Box<Function>> {
if !function.is_async {
HANDLER.with(|handler| {
handler
.struct_span_err(ident.span, "Server actions must be async functions")
.emit();
});
}
let need_rename_export = self.in_action_file && (self.in_export_decl || is_exported); let need_rename_export = self.in_action_file && (self.in_export_decl || is_exported);
let action_name: JsWord = if need_rename_export { let action_name: JsWord = if need_rename_export {
ident.sym.clone() ident.sym.clone()
...@@ -336,19 +328,35 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -336,19 +328,35 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let old_in_action_fn = self.in_action_fn; let old_in_action_fn = self.in_action_fn;
let old_in_module = self.in_module; let old_in_module = self.in_module;
let old_should_add_name = self.should_add_name; let old_should_add_name = self.should_add_name;
let old_in_export_decl = self.in_export_decl;
let old_in_default_export_decl = self.in_default_export_decl;
self.in_action_fn = is_action_fn; self.in_action_fn = is_action_fn;
self.in_module = false; self.in_module = false;
self.should_add_name = true; self.should_add_name = true;
self.in_export_decl = false;
self.in_default_export_decl = false;
f.visit_mut_children_with(self); f.visit_mut_children_with(self);
self.in_action_fn = old_in_action_fn; self.in_action_fn = old_in_action_fn;
self.in_module = old_in_module; self.in_module = old_in_module;
self.should_add_name = old_should_add_name; self.should_add_name = old_should_add_name;
self.in_export_decl = old_in_export_decl;
self.in_default_export_decl = old_in_default_export_decl;
} }
if !is_action_fn { if !is_action_fn {
return; return;
} }
if !f.function.is_async {
HANDLER.with(|handler| {
handler
.struct_span_err(
f.ident.as_mut().unwrap().span,
"Server actions must be async functions",
)
.emit();
});
} else {
let maybe_new_fn = self.add_action_annotations( let maybe_new_fn = self.add_action_annotations(
f.ident.as_mut().unwrap(), f.ident.as_mut().unwrap(),
&mut f.function, &mut f.function,
...@@ -360,6 +368,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -360,6 +368,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
f.function = new_fn; f.function = new_fn;
} }
} }
}
fn visit_mut_fn_decl(&mut self, f: &mut FnDecl) { fn visit_mut_fn_decl(&mut self, f: &mut FnDecl) {
// Need to collect all async function identifiers if we are in a server // Need to collect all async function identifiers if we are in a server
...@@ -379,21 +388,34 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -379,21 +388,34 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let old_in_action_fn = self.in_action_fn; let old_in_action_fn = self.in_action_fn;
let old_in_module = self.in_module; let old_in_module = self.in_module;
let old_should_add_name = self.should_add_name; let old_should_add_name = self.should_add_name;
let old_in_export_decl = self.in_export_decl;
let old_in_default_export_decl = self.in_default_export_decl;
self.in_action_fn = is_action_fn; self.in_action_fn = is_action_fn;
self.in_module = false; self.in_module = false;
self.should_add_name = true; self.should_add_name = true;
self.in_export_decl = false;
self.in_default_export_decl = false;
f.visit_mut_children_with(self); f.visit_mut_children_with(self);
self.in_action_fn = old_in_action_fn; self.in_action_fn = old_in_action_fn;
self.in_module = old_in_module; self.in_module = old_in_module;
self.should_add_name = old_should_add_name; self.should_add_name = old_should_add_name;
self.in_export_decl = old_in_export_decl;
self.in_default_export_decl = old_in_default_export_decl;
} }
if !is_action_fn { if !is_action_fn {
return; return;
} }
if !f.function.is_async {
HANDLER.with(|handler| {
handler
.struct_span_err(f.ident.span, "Server actions must be async functions")
.emit();
});
} else {
let maybe_new_fn = self.add_action_annotations( let maybe_new_fn = self.add_action_annotations(
&mut f.ident, &f.ident,
&mut f.function, &mut f.function,
is_exported, is_exported,
is_default_export, is_default_export,
...@@ -403,6 +425,53 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -403,6 +425,53 @@ impl<C: Comments> VisitMut for ServerActions<C> {
f.function = new_fn; f.function = new_fn;
} }
} }
}
fn visit_mut_var_decl(&mut self, n: &mut VarDecl) {
if self.in_action_file {
for decl in n.decls.iter_mut() {
if decl.init.is_none() {
continue;
}
let init = decl.init.as_mut().unwrap();
if let Pat::Ident(ident) = &mut decl.name {
if let Some(fn_expr) = init.as_mut_fn_expr() {
// Collect `const foo = async function () {}` declarations. For now we
// ignore other types of assignments.
if fn_expr.function.is_async {
if self.in_prepass {
self.async_fn_idents.push(ident.id.to_id());
} else if let Some(exported_ident) = self
.exported_idents
.iter()
.find(|(id, _)| id == &ident.id.to_id())
{
// It's an action function, we need to add the
// name to the function if missing.
if fn_expr.ident.is_none() {
let action_name: JsWord =
format!("$ACTION_fn_{}", self.action_index).into();
self.action_index += 1;
fn_expr.ident = Some(Ident::new(action_name, DUMMY_SP));
}
self.exported_idents.push((
fn_expr.ident.as_ref().unwrap().to_id(),
exported_ident.1,
));
}
}
}
}
}
if self.in_prepass {
return;
}
}
n.visit_mut_children_with(self);
}
fn visit_mut_module(&mut self, m: &mut Module) { fn visit_mut_module(&mut self, m: &mut Module) {
self.start_pos = m.span.lo; self.start_pos = m.span.lo;
...@@ -541,6 +610,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -541,6 +610,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
if let Some(init) = &decl.init { if let Some(init) = &decl.init {
match &**init { match &**init {
Expr::Fn(_f) => {} Expr::Fn(_f) => {}
Expr::Arrow(_f) => {}
_ => { _ => {
disallowed_export_span = *span; disallowed_export_span = *span;
} }
......
/* __next_internal_action_entry_do_not_use__ foo */ export function foo() {} /* __next_internal_action_entry_do_not_use__ */ export function foo() {}
foo.$$typeof = Symbol.for("react.server.reference"); \ No newline at end of file
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
\ No newline at end of file
/* __next_internal_action_entry_do_not_use__ bar */ 'use strict'; /* __next_internal_action_entry_do_not_use__ */ 'use strict';
export function bar() {} export function bar() {}
\ No newline at end of file
bar.$$typeof = Symbol.for("react.server.reference");
bar.$$id = "ac840dcaf5e8197cb02b7f3a43c119b7a770b272";
bar.$$bound = [];
\ No newline at end of file
'use server'
const foo = async function () {}
export default foo
const bar = async function() {}
export { bar }
/* __next_internal_action_entry_do_not_use__ default,$ACTION_fn_1 */ const foo = async function $ACTION_fn_0() {};
$ACTION_fn_0.$$typeof = Symbol.for("react.server.reference");
$ACTION_fn_0.$$id = "c18c215a6b7cdc64bf709f3a714ffdef1bf9651d";
$ACTION_fn_0.$$bound = [];
export default foo;
const bar = async function $ACTION_fn_1() {};
$ACTION_fn_1.$$typeof = Symbol.for("react.server.reference");
$ACTION_fn_1.$$id = "5bde18329eb1c98fc2d6dea0a22639861a18ce65";
$ACTION_fn_1.$$bound = [];
export { bar };
'use server'
export async function foo() {
async function bar() {}
}
\ No newline at end of file
/* __next_internal_action_entry_do_not_use__ foo */ export async function foo() {
async function bar() {}
}
foo.$$typeof = Symbol.for("react.server.reference");
foo.$$id = "ab21efdafbe611287bc25c0462b1e0510d13e48b";
foo.$$bound = [];
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