Unverified Commit 4b8240b8 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Support unnamed default export expression (#46888

This PR adds the support for unnamed default export expression support
`export default async function () {}` in a "use server" entry, with
corresponding test.

Also fixed an existing bug that the default exported action's name
should be aligned with the export name, which is `"default"`.

Closes NEXT-769.
parent 14bfe871
...@@ -34,12 +34,14 @@ pub fn server_actions<C: Comments>( ...@@ -34,12 +34,14 @@ pub fn server_actions<C: Comments>(
start_pos: BytePos(0), start_pos: BytePos(0),
in_action_file: false, in_action_file: false,
in_export_decl: false, in_export_decl: false,
in_default_export_decl: false,
in_prepass: false, in_prepass: false,
has_action: false, has_action: false,
top_level: false, top_level: false,
in_module: true, in_module: true,
in_action_fn: false, in_action_fn: false,
action_index: 0,
should_add_name: false, should_add_name: false,
closure_idents: Default::default(), closure_idents: Default::default(),
action_idents: Default::default(), action_idents: Default::default(),
...@@ -61,12 +63,14 @@ struct ServerActions<C: Comments> { ...@@ -61,12 +63,14 @@ struct ServerActions<C: Comments> {
start_pos: BytePos, start_pos: BytePos,
in_action_file: bool, in_action_file: bool,
in_export_decl: bool, in_export_decl: bool,
in_default_export_decl: bool,
in_prepass: bool, in_prepass: bool,
has_action: bool, has_action: bool,
top_level: bool, top_level: bool,
in_module: bool, in_module: bool,
in_action_fn: bool, in_action_fn: bool,
action_index: u32,
should_add_name: bool, should_add_name: bool,
closure_idents: Vec<Id>, closure_idents: Vec<Id>,
action_idents: Vec<Name>, action_idents: Vec<Name>,
...@@ -128,8 +132,14 @@ impl<C: Comments> ServerActions<C> { ...@@ -128,8 +132,14 @@ impl<C: Comments> ServerActions<C> {
}; };
let action_ident = private_ident!(action_name.clone()); let action_ident = private_ident!(action_name.clone());
let export_name: JsWord = if self.in_default_export_decl {
"default".into()
} else {
action_name
};
self.has_action = true; self.has_action = true;
self.export_actions.push(action_name.to_string()); self.export_actions.push(export_name.to_string());
// myAction.$$typeof = Symbol.for('react.server.reference'); // myAction.$$typeof = Symbol.for('react.server.reference');
self.annotations.push(annotate( self.annotations.push(annotate(
...@@ -155,7 +165,7 @@ impl<C: Comments> ServerActions<C> { ...@@ -155,7 +165,7 @@ impl<C: Comments> ServerActions<C> {
// myAction.$$name = '$ACTION_myAction'; // myAction.$$name = '$ACTION_myAction';
self.annotations self.annotations
.push(annotate(ident, "$$name", action_name.into())); .push(annotate(ident, "$$name", export_name.into()));
if self.top_level { if self.top_level {
// myAction.$$bound = []; // myAction.$$bound = [];
...@@ -270,9 +280,12 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -270,9 +280,12 @@ impl<C: Comments> VisitMut for ServerActions<C> {
fn visit_mut_export_default_decl(&mut self, decl: &mut ExportDefaultDecl) { fn visit_mut_export_default_decl(&mut self, decl: &mut ExportDefaultDecl) {
let old = self.in_export_decl; let old = self.in_export_decl;
let old_default = self.in_default_export_decl;
self.in_export_decl = true; self.in_export_decl = true;
self.in_default_export_decl = true;
decl.decl.visit_mut_with(self); decl.decl.visit_mut_with(self);
self.in_export_decl = old; self.in_export_decl = old;
self.in_default_export_decl = old_default;
} }
fn visit_mut_fn_expr(&mut self, f: &mut FnExpr) { fn visit_mut_fn_expr(&mut self, f: &mut FnExpr) {
...@@ -288,9 +301,16 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -288,9 +301,16 @@ impl<C: Comments> VisitMut for ServerActions<C> {
} }
if f.ident.is_none() { if f.ident.is_none() {
// Exported anonymous async functions need to have a name assigned.
if self.in_action_file && self.in_export_decl && f.function.is_async {
let action_name: JsWord = format!("$ACTION_default_{}", self.action_index).into();
self.action_index += 1;
f.ident = Some(Ident::new(action_name, DUMMY_SP));
} else {
f.visit_mut_children_with(self); f.visit_mut_children_with(self);
return; return;
} }
}
let (is_action_fn, is_exported) = let (is_action_fn, is_exported) =
self.get_action_info(f.ident.as_mut().unwrap(), &mut f.function); self.get_action_info(f.ident.as_mut().unwrap(), &mut f.function);
......
/* __next_internal_action_entry_do_not_use__ foo */ export default async function foo() {} /* __next_internal_action_entry_do_not_use__ default */ export default async function foo() {}
foo.$$typeof = Symbol.for("react.server.reference"); foo.$$typeof = Symbol.for("react.server.reference");
foo.$$filepath = "/app/item.js"; foo.$$filepath = "/app/item.js";
foo.$$name = "foo"; foo.$$name = "default";
foo.$$bound = []; foo.$$bound = [];
\ No newline at end of file
'use server'
export default async function () {}
/* __next_internal_action_entry_do_not_use__ default */ export default async function $ACTION_default_0() {}
$ACTION_default_0.$$typeof = Symbol.for("react.server.reference");
$ACTION_default_0.$$filepath = "/app/item.js";
$ACTION_default_0.$$name = "default";
$ACTION_default_0.$$bound = [];
\ No newline at end of file
...@@ -17,8 +17,14 @@ createNextDescribe( ...@@ -17,8 +17,14 @@ createNextDescribe(
await browser.elementByCss('#inc').click() await browser.elementByCss('#inc').click()
await check(() => browser.elementByCss('h1').text(), '1') await check(() => browser.elementByCss('h1').text(), '1')
await browser.elementByCss('#inc').click()
await check(() => browser.elementByCss('h1').text(), '2')
await browser.elementByCss('#double').click()
await check(() => browser.elementByCss('h1').text(), '4')
await browser.elementByCss('#dec').click() await browser.elementByCss('#dec').click()
await check(() => browser.elementByCss('h1').text(), '0') await check(() => browser.elementByCss('h1').text(), '3')
}) })
it('should support headers and cookies', async () => { it('should support headers and cookies', async () => {
......
...@@ -7,3 +7,7 @@ export async function inc(value) { ...@@ -7,3 +7,7 @@ export async function inc(value) {
export async function dec(value) { export async function dec(value) {
return value - 1 return value - 1
} }
export default async function (value) {
return value * 2
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { useState } from 'react' import { useState } from 'react'
export default function Counter({ inc, dec }) { export default function Counter({ inc, dec, double }) {
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
return ( return (
...@@ -26,6 +26,15 @@ export default function Counter({ inc, dec }) { ...@@ -26,6 +26,15 @@ export default function Counter({ inc, dec }) {
> >
-1 -1
</button> </button>
<button
id="double"
onClick={async () => {
const newCount = await double(count)
setCount(newCount)
}}
>
*2
</button>
</div> </div>
) )
} }
import Counter from './counter' import Counter from './counter'
import { inc, dec } from './actions' import double, { inc, dec } from './actions'
export default function Page() { export default function Page() {
return <Counter inc={inc} dec={dec} /> return <Counter inc={inc} dec={dec} double={double} />
} }
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