Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-peaches-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next": patch
---

Fix name tracking for closures in server actions transform
24 changes: 12 additions & 12 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,12 @@ impl<C: Comments> VisitMut for ServerActions<C> {
self.fn_decl_ident = old_fn_decl_ident;
}

let mut child_names = take(&mut self.names);

if self.should_track_names {
self.names = [old_names, child_names.clone()].concat();
}

if let Some(directive) = directive {
if !f.is_async {
emit_error(ServerActionsErrorKind::InlineSyncFunction {
Expand All @@ -1028,12 +1034,6 @@ impl<C: Comments> VisitMut for ServerActions<C> {
return;
}

let mut child_names = take(&mut self.names);

if self.should_track_names {
self.names = [old_names, child_names.clone()].concat();
}

if let Directive::UseCache { cache_kind } = directive {
// Collect all the identifiers defined inside the closure and used
// in the cache function. With deduplication.
Expand Down Expand Up @@ -1181,6 +1181,12 @@ impl<C: Comments> VisitMut for ServerActions<C> {
self.in_default_export_decl = old_in_default_export_decl;
}

let mut child_names = take(&mut self.names);

if self.should_track_names {
self.names = [old_names, child_names.clone()].concat();
}

if let Some(directive) = directive {
if !a.is_async {
emit_error(ServerActionsErrorKind::InlineSyncFunction {
Expand All @@ -1199,12 +1205,6 @@ impl<C: Comments> VisitMut for ServerActions<C> {
return;
}

let mut child_names = take(&mut self.names);

if self.should_track_names {
self.names = [old_names, child_names.clone()].concat();
}

// Collect all the identifiers defined inside the closure and used
// in the action function. With deduplication.
retain_names_from_declared_idents(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export function ComponentA({ list, y }) {
return (
<form
action={async () => {
'use server'
console.log(list.find((x) => x === y))
}}
>
<button>submit</button>
</form>
)
}

export function ComponentB({ list, y }) {
return (
<form
action={async () => {
'use server'
console.log(
list.find(function (x) {
return x === y
})
)
}}
>
<button>submit</button>
</form>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* __next_internal_action_entry_do_not_use__ {"406a88810ecce4a4e8b59d53b8327d7e98bbf251d7":"$$RSC_SERVER_ACTION_0","4090b5db271335765a4b0eab01f044b381b5ebd5cd":"$$RSC_SERVER_ACTION_1"} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
export const $$RSC_SERVER_ACTION_0 = async function action($$ACTION_CLOSURE_BOUND) {
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", $$ACTION_CLOSURE_BOUND);
console.log($$ACTION_ARG_0.find((x)=>x === $$ACTION_ARG_1));
};
export function ComponentA({ list, y }) {
return <form action={registerServerReference($$RSC_SERVER_ACTION_0, "406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", null).bind(null, encryptActionBoundArgs("406a88810ecce4a4e8b59d53b8327d7e98bbf251d7", list, y))}>
<button>submit</button>
</form>;
}
export const $$RSC_SERVER_ACTION_1 = async function action($$ACTION_CLOSURE_BOUND) {
var [$$ACTION_ARG_0, $$ACTION_ARG_1] = await decryptActionBoundArgs("4090b5db271335765a4b0eab01f044b381b5ebd5cd", $$ACTION_CLOSURE_BOUND);
console.log($$ACTION_ARG_0.find(function(x) {
return x === $$ACTION_ARG_1;
}));
};
export function ComponentB({ list, y }) {
return <form action={registerServerReference($$RSC_SERVER_ACTION_1, "4090b5db271335765a4b0eab01f044b381b5ebd5cd", null).bind(null, encryptActionBoundArgs("4090b5db271335765a4b0eab01f044b381b5ebd5cd", list, y))}>
<button>submit</button>
</form>;
}
Loading