Skip to content

Commit

Permalink
Method/function props of exported objects are not server functions
Browse files Browse the repository at this point in the history
This is a follow-up fix for #72969. Method or function properties of
exported objects in `"use cache"` or `"use server"` files must not be
compiled into server functions.
  • Loading branch information
unstubbable committed Nov 22, 2024
1 parent fd5dc5f commit 1f4c2f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1190,13 +1190,15 @@ impl<C: Comments> VisitMut for ServerActions<C> {

fn visit_mut_prop_or_spread(&mut self, n: &mut PropOrSpread) {
let old_arrow_or_fn_expr_ident = self.arrow_or_fn_expr_ident.clone();
let old_in_exported_expr = self.in_exported_expr;

match n {
PropOrSpread::Prop(box Prop::KeyValue(KeyValueProp {
key: PropName::Ident(ident_name),
value: box Expr::Arrow(_) | box Expr::Fn(_),
..
})) => {
self.in_exported_expr = false;
self.arrow_or_fn_expr_ident = Some(ident_name.clone().into());
}
PropOrSpread::Prop(box Prop::Method(MethodProp { key, .. })) => {
Expand All @@ -1205,7 +1207,9 @@ impl<C: Comments> VisitMut for ServerActions<C> {
self.arrow_or_fn_expr_ident = Some(ident_name.clone().into());
}
self.rewrite_expr_to_proxy_expr = None;
self.in_exported_expr = false;
n.visit_mut_children_with(self);
self.in_exported_expr = old_in_exported_expr;
if let Some(expr) = &self.rewrite_expr_to_proxy_expr {
*n = PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key,
Expand All @@ -1230,6 +1234,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {

n.visit_mut_children_with(self);
self.arrow_or_fn_expr_ident = old_arrow_or_fn_expr_ident;
self.in_exported_expr = old_in_exported_expr;
}

fn visit_mut_callee(&mut self, n: &mut Callee) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use cache'

// No method nor function property should be considered a cache function.
export const obj = {
foo() {
return 1
},
async bar() {
return 2
},
baz: () => 2,
qux: async () => 3,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* __next_internal_action_entry_do_not_use__ {} */ import { registerServerReference } from "private-next-rsc-server-reference";
import { encryptActionBoundArgs, decryptActionBoundArgs } from "private-next-rsc-action-encryption";
import { cache as $$cache__ } from "private-next-rsc-cache-wrapper";
// No method nor function property should be considered a cache function.
export const obj = {
foo () {
return 1;
},
async bar () {
return 2;
},
baz: ()=>2,
qux: async ()=>3
};

0 comments on commit 1f4c2f5

Please sign in to comment.