diff --git a/.changeset/tough-peaches-burn.md b/.changeset/tough-peaches-burn.md new file mode 100644 index 0000000000000..5814def9a6b3d --- /dev/null +++ b/.changeset/tough-peaches-burn.md @@ -0,0 +1,5 @@ +--- +"next": patch +--- + +Fix name tracking for closures in server actions transform diff --git a/crates/next-custom-transforms/src/transforms/server_actions.rs b/crates/next-custom-transforms/src/transforms/server_actions.rs index 7a24df04db473..3fd09d097a051 100644 --- a/crates/next-custom-transforms/src/transforms/server_actions.rs +++ b/crates/next-custom-transforms/src/transforms/server_actions.rs @@ -1011,6 +1011,12 @@ impl VisitMut for ServerActions { 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 { @@ -1028,12 +1034,6 @@ impl VisitMut for ServerActions { 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. @@ -1181,6 +1181,12 @@ impl VisitMut for ServerActions { 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 { @@ -1199,12 +1205,6 @@ impl VisitMut for ServerActions { 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( diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/server-graph/61/input.js b/crates/next-custom-transforms/tests/fixture/server-actions/server-graph/61/input.js new file mode 100644 index 0000000000000..14367767c2903 --- /dev/null +++ b/crates/next-custom-transforms/tests/fixture/server-actions/server-graph/61/input.js @@ -0,0 +1,29 @@ +export function ComponentA({ list, y }) { + return ( +
{ + 'use server' + console.log(list.find((x) => x === y)) + }} + > + +
+ ) +} + +export function ComponentB({ list, y }) { + return ( +
{ + 'use server' + console.log( + list.find(function (x) { + return x === y + }) + ) + }} + > + +
+ ) +} diff --git a/crates/next-custom-transforms/tests/fixture/server-actions/server-graph/61/output.js b/crates/next-custom-transforms/tests/fixture/server-actions/server-graph/61/output.js new file mode 100644 index 0000000000000..10417e25b2c85 --- /dev/null +++ b/crates/next-custom-transforms/tests/fixture/server-actions/server-graph/61/output.js @@ -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
+ +
; +} +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
+ +
; +}