Unverified Commit d9874838 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Fix `export const foo = function bar(){}` in server reference transform (#100830)

It was doing something like this:
```js
"use server";
export const foo = async function bar() {
    return 'exported named function expression';
};
ensureServerEntryExports([
    bar, // should be foo!
]);
registerServerReference(bar, "003c8ed39a99caf636869d076cc7cae786a4053164", null); // shoudl be foo!
```

leading to
```
⨯ ReferenceError: quux is not defined
    at module evaluation (test/development/app-dir/source-mapping/app/actions1.ts:15:35)
    at module evaluation (test/development/app-dir/source-mapping/app/page.tsx:2:1)
  13 | }
  14 |
> 15 | export const qux = async function quux() {
     |                                   ^
  16 |   return 'exported named function expression'
  17 | }
  18 | {
  page: '/'
}
```
parent f111dc7f
...@@ -167,6 +167,7 @@ pub fn server_actions<C: Comments>( ...@@ -167,6 +167,7 @@ pub fn server_actions<C: Comments>(
file_directive: None, file_directive: None,
current_export_name: None, current_export_name: None,
fn_decl_ident: None, fn_decl_ident: None,
fn_expr_ident: None,
in_callee: false, in_callee: false,
has_action: false, has_action: false,
has_cache: false, has_cache: false,
...@@ -236,6 +237,8 @@ struct ServerActions<C: Comments> { ...@@ -236,6 +237,8 @@ struct ServerActions<C: Comments> {
file_directive: Option<Directive>, file_directive: Option<Directive>,
current_export_name: Option<ModuleExportName>, current_export_name: Option<ModuleExportName>,
fn_decl_ident: Option<Ident>, fn_decl_ident: Option<Ident>,
/// The explicit name of a function expression, not its enclosing binding.
fn_expr_ident: Option<Ident>,
in_callee: bool, in_callee: bool,
has_action: bool, has_action: bool,
has_cache: bool, has_cache: bool,
...@@ -267,6 +270,7 @@ struct ServerActions<C: Comments> { ...@@ -267,6 +270,7 @@ struct ServerActions<C: Comments> {
private_ctxt: SyntaxContext, private_ctxt: SyntaxContext,
unresolved_ctxt: SyntaxContext, unresolved_ctxt: SyntaxContext,
/// The inferred name from an enclosing variable, assignment, or property.
arrow_or_fn_expr_ident: Option<Ident>, arrow_or_fn_expr_ident: Option<Ident>,
export_name_by_local_id: FxIndexMap<Id, ModuleExportName>, export_name_by_local_id: FxIndexMap<Id, ModuleExportName>,
...@@ -1196,13 +1200,10 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1196,13 +1200,10 @@ impl<C: Comments> VisitMut for ServerActions<C> {
fn visit_mut_fn_expr(&mut self, f: &mut FnExpr) { fn visit_mut_fn_expr(&mut self, f: &mut FnExpr) {
let old_this_status = replace(&mut self.this_status, ThisStatus::Allowed); let old_this_status = replace(&mut self.this_status, ThisStatus::Allowed);
let old_arrow_or_fn_expr_ident = self.arrow_or_fn_expr_ident.clone(); let old_fn_expr_ident = replace(&mut self.fn_expr_ident, f.ident.clone());
if let Some(ident) = &f.ident {
self.arrow_or_fn_expr_ident = Some(ident.clone());
}
f.visit_mut_children_with(self); f.visit_mut_children_with(self);
self.this_status = old_this_status; self.this_status = old_this_status;
self.arrow_or_fn_expr_ident = old_arrow_or_fn_expr_ident; self.fn_expr_ident = old_fn_expr_ident;
} }
fn visit_mut_function(&mut self, f: &mut Function) { fn visit_mut_function(&mut self, f: &mut Function) {
...@@ -1223,11 +1224,13 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1223,11 +1224,13 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let old_should_track_names = replace(&mut self.should_track_names, should_track_names); let old_should_track_names = replace(&mut self.should_track_names, should_track_names);
let old_current_export_name = self.current_export_name.take(); let old_current_export_name = self.current_export_name.take();
let old_fn_decl_ident = self.fn_decl_ident.take(); let old_fn_decl_ident = self.fn_decl_ident.take();
let old_fn_expr_ident = self.fn_expr_ident.take();
f.visit_mut_children_with(self); f.visit_mut_children_with(self);
self.in_module_level = old_in_module; self.in_module_level = old_in_module;
self.should_track_names = old_should_track_names; self.should_track_names = old_should_track_names;
self.current_export_name = old_current_export_name; self.current_export_name = old_current_export_name;
self.fn_decl_ident = old_fn_decl_ident; self.fn_decl_ident = old_fn_decl_ident;
self.fn_expr_ident = old_fn_expr_ident;
} }
let mut child_names = take(&mut self.names); let mut child_names = take(&mut self.names);
...@@ -1240,16 +1243,24 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1240,16 +1243,24 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let fn_name = self let fn_name = self
.fn_decl_ident .fn_decl_ident
.as_ref() .as_ref()
.or(self.fn_expr_ident.as_ref())
.or(self.arrow_or_fn_expr_ident.as_ref()) .or(self.arrow_or_fn_expr_ident.as_ref())
.cloned(); .cloned();
let export_ident = self
.fn_decl_ident
.as_ref()
.or(self.arrow_or_fn_expr_ident.as_ref())
.or(self.fn_expr_ident.as_ref())
.cloned();
if !self.validate_async_function(f.is_async, f.span, fn_name.as_ref(), &directive) { if !self.validate_async_function(f.is_async, f.span, fn_name.as_ref(), &directive) {
// If this is an exported function that failed validation, remove it from // If this is an exported function that failed validation, remove it from
// export_name_by_local_id so the post-pass doesn't register it. // export_name_by_local_id so the post-pass doesn't register it.
if self.current_export_name.is_some() if self.current_export_name.is_some()
&& let Some(fn_name) = fn_name && let Some(export_ident) = export_ident
{ {
self.export_name_by_local_id.swap_remove(&fn_name.to_id()); self.export_name_by_local_id
.swap_remove(&export_ident.to_id());
} }
return; return;
...@@ -1272,7 +1283,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1272,7 +1283,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
self.register_server_action_export( self.register_server_action_export(
&export_name, &export_name,
fn_name.as_ref(), export_ident.as_ref(),
Some(&params), Some(&params),
span, span,
&mut || { &mut || {
...@@ -1293,7 +1304,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1293,7 +1304,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
{ {
self.register_cache_export_on_client( self.register_cache_export_on_client(
&export_name, &export_name,
fn_name.as_ref(), export_ident.as_ref(),
Some(&f.params), Some(&f.params),
f.span, f.span,
); );
...@@ -1312,10 +1323,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1312,10 +1323,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let new_expr = self.maybe_hoist_and_create_proxy_for_cache_function( let new_expr = self.maybe_hoist_and_create_proxy_for_cache_function(
child_names.clone(), child_names.clone(),
self.fn_decl_ident fn_name,
.as_ref()
.or(self.arrow_or_fn_expr_ident.as_ref())
.cloned(),
cache_kind, cache_kind,
f, f,
); );
......
'use server'
export const qux = async function wrongId1() {
return 'exported named function expression'
}
const local = async function wrongId2() {
return 'aliased named function expression'
}
export { local as aliased }
const defaultAction = async function wrongId3() {
return 'default exported named function expression'
}
export default defaultAction
/* __next_internal_action_entry_do_not_use__ {"0027fadf3eeb97c777cea9f14a407b5c0b42ac65bb":{"name":"aliased"},"003c8ed39a99caf636869d076cc7cae786a4053164":{"name":"qux"},"00c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":{"name":"default"}} */ import { registerServerReference } from "private-next-rsc-server-reference";
export const qux = async function wrongId1() {
return 'exported named function expression';
};
const local = async function wrongId2() {
return 'aliased named function expression';
};
export { local as aliased };
const defaultAction = async function wrongId3() {
return 'default exported named function expression';
};
export default defaultAction;
import { ensureServerEntryExports } from "private-next-rsc-action-validate";
ensureServerEntryExports([
qux,
local,
defaultAction
]);
registerServerReference(qux, "003c8ed39a99caf636869d076cc7cae786a4053164", null);
registerServerReference(local, "0027fadf3eeb97c777cea9f14a407b5c0b42ac65bb", null);
registerServerReference(defaultAction, "00c18c215a6b7cdc64bf709f3a714ffdef1bf9651d", null);
export const action = async function actionImpl(count) {
'use server'
if (count === 0) return 'done'
return actionImpl(count - 1)
}
export const cached = async function cachedImpl(count) {
'use cache'
if (count === 0) return 'done'
return cachedImpl(count - 1)
}
import { registerServerReference } from "private-next-rsc-server-reference";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
import { cache as $$reactCache__ } from "react";
export const $$RSC_SERVER_ACTION_0 = async function actionImpl(count) {
if (count === 0) return 'done';
return actionImpl(count - 1);
};
registerServerReference($$RSC_SERVER_ACTION_0, "406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null);
/* __next_internal_action_entry_do_not_use__ {"406a88810ecce4a4e8b59d53b8327d7e98bbf251d7":{"name":"$$RSC_SERVER_ACTION_0"},"c0951c375b4a6a6e89d67b743ec5808127cfde405d":{"name":"$$RSC_SERVER_CACHE_1"}} */ export const action = $$RSC_SERVER_ACTION_0;
const $$RSC_SERVER_CACHE_1_INNER = async function cachedImpl(count) {
if (count === 0) return 'done';
return cachedImpl(count - 1);
};
export var $$RSC_SERVER_CACHE_1 = $$reactCache__(function cachedImpl() {
return $$cache__("default", "c0951c375b4a6a6e89d67b743ec5808127cfde405d", 0, $$RSC_SERVER_CACHE_1_INNER, Array.prototype.slice.call(arguments, 0, 1));
});
registerServerReference($$RSC_SERVER_CACHE_1, "c0951c375b4a6a6e89d67b743ec5808127cfde405d", null);
Object["defineProperty"]($$RSC_SERVER_CACHE_1, "name", {
value: "cachedImpl"
});
export const cached = $$RSC_SERVER_CACHE_1;
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