Unverified Commit e40574b0 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix: Fix syntax context usage and add PURE comments in server actions (#70014)

### What?

Fix usages of `SyntaxContext` in server actions transform.

### Why?

This is part of [the tree-shaking PR](https://github.com/vercel/next.js/pull/69344).

### How?
parent 21855f9b
...@@ -537,6 +537,7 @@ impl ReactServerComponentValidator { ...@@ -537,6 +537,7 @@ impl ReactServerComponentValidator {
"useTransition", "useTransition",
"useOptimistic", "useOptimistic",
"useActionState", "useActionState",
"experimental_useOptimistic",
], ],
), ),
( (
......
...@@ -12,7 +12,7 @@ use swc_core::{ ...@@ -12,7 +12,7 @@ use swc_core::{
comments::{Comment, CommentKind, Comments}, comments::{Comment, CommentKind, Comments},
errors::HANDLER, errors::HANDLER,
util::take::Take, util::take::Take,
BytePos, FileName, Span, SyntaxContext, DUMMY_SP, BytePos, FileName, Mark, Span, SyntaxContext, DUMMY_SP,
}, },
ecma::{ ecma::{
ast::*, ast::*,
...@@ -68,6 +68,8 @@ pub fn server_actions<C: Comments>( ...@@ -68,6 +68,8 @@ pub fn server_actions<C: Comments>(
annotations: Default::default(), annotations: Default::default(),
extra_items: Default::default(), extra_items: Default::default(),
export_actions: Default::default(), export_actions: Default::default(),
private_ctxt: SyntaxContext::empty().apply_mark(Mark::new()),
}) })
} }
...@@ -111,6 +113,8 @@ struct ServerActions<C: Comments> { ...@@ -111,6 +113,8 @@ struct ServerActions<C: Comments> {
annotations: Vec<Stmt>, annotations: Vec<Stmt>,
extra_items: Vec<ModuleItem>, extra_items: Vec<ModuleItem>,
export_actions: Vec<String>, export_actions: Vec<String>,
private_ctxt: SyntaxContext,
} }
impl<C: Comments> ServerActions<C> { impl<C: Comments> ServerActions<C> {
...@@ -160,7 +164,7 @@ impl<C: Comments> ServerActions<C> { ...@@ -160,7 +164,7 @@ impl<C: Comments> ServerActions<C> {
arrow: Option<&mut ArrowExpr>, arrow: Option<&mut ArrowExpr>,
) -> Option<Box<Expr>> { ) -> Option<Box<Expr>> {
let action_name: JsWord = gen_ident(&mut self.reference_index); let action_name: JsWord = gen_ident(&mut self.reference_index);
let action_ident = private_ident!(action_name.clone()); let action_ident = Ident::new(action_name.clone(), DUMMY_SP, self.private_ctxt);
let export_name: JsWord = action_name; let export_name: JsWord = action_name;
self.has_action = true; self.has_action = true;
...@@ -184,6 +188,7 @@ impl<C: Comments> ServerActions<C> { ...@@ -184,6 +188,7 @@ impl<C: Comments> ServerActions<C> {
if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body { if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body {
block.visit_mut_with(&mut ClosureReplacer { block.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure, used_ids: &ids_from_closure,
private_ctxt: self.private_ctxt,
}); });
} }
...@@ -207,7 +212,12 @@ impl<C: Comments> ServerActions<C> { ...@@ -207,7 +212,12 @@ impl<C: Comments> ServerActions<C> {
let mut pats = vec![]; let mut pats = vec![];
for i in 0..ids_from_closure.len() { for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident( pats.push(Some(Pat::Ident(
IdentName::new(format!("$$ACTION_ARG_{i}").into(), DUMMY_SP).into(), Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
))); )));
} }
let decryption_decl = VarDecl { let decryption_decl = VarDecl {
...@@ -316,6 +326,7 @@ impl<C: Comments> ServerActions<C> { ...@@ -316,6 +326,7 @@ impl<C: Comments> ServerActions<C> {
f.body.visit_mut_with(&mut ClosureReplacer { f.body.visit_mut_with(&mut ClosureReplacer {
used_ids: &ids_from_closure, used_ids: &ids_from_closure,
private_ctxt: self.private_ctxt,
}); });
// export async function $ACTION_myAction () {} // export async function $ACTION_myAction () {}
...@@ -339,7 +350,12 @@ impl<C: Comments> ServerActions<C> { ...@@ -339,7 +350,12 @@ impl<C: Comments> ServerActions<C> {
let mut pats = vec![]; let mut pats = vec![];
for i in 0..ids_from_closure.len() { for i in 0..ids_from_closure.len() {
pats.push(Some(Pat::Ident( pats.push(Some(Pat::Ident(
IdentName::new(format!("$$ACTION_ARG_{i}").into(), DUMMY_SP).into(), Ident::new(
format!("$$ACTION_ARG_{i}").into(),
DUMMY_SP,
self.private_ctxt,
)
.into(),
))); )));
} }
let decryption_decl = VarDecl { let decryption_decl = VarDecl {
...@@ -490,7 +506,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -490,7 +506,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
match f.ident.as_mut() { match f.ident.as_mut() {
None => { None => {
let action_name = gen_ident(&mut self.reference_index); let action_name = gen_ident(&mut self.reference_index);
let ident = Ident::new(action_name, DUMMY_SP, Default::default()); let ident = Ident::new(action_name, DUMMY_SP, self.private_ctxt);
f.ident.insert(ident) f.ident.insert(ident)
} }
Some(i) => i, Some(i) => i,
...@@ -860,7 +876,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -860,7 +876,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let new_ident = Ident::new( let new_ident = Ident::new(
gen_ident(&mut self.reference_index), gen_ident(&mut self.reference_index),
DUMMY_SP, DUMMY_SP,
Default::default(), self.private_ctxt,
); );
f.ident = Some(new_ident.clone()); f.ident = Some(new_ident.clone());
self.exported_idents self.exported_idents
...@@ -882,7 +898,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -882,7 +898,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let new_ident = Ident::new( let new_ident = Ident::new(
gen_ident(&mut self.reference_index), gen_ident(&mut self.reference_index),
DUMMY_SP, DUMMY_SP,
Default::default(), self.private_ctxt,
); );
self.exported_idents self.exported_idents
...@@ -904,7 +920,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -904,7 +920,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let new_ident = Ident::new( let new_ident = Ident::new(
gen_ident(&mut self.reference_index), gen_ident(&mut self.reference_index),
DUMMY_SP, DUMMY_SP,
Default::default(), self.private_ctxt,
); );
self.exported_idents self.exported_idents
...@@ -996,12 +1012,14 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -996,12 +1012,14 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let action_id = let action_id =
generate_action_id(&self.config.hash_salt, &self.file_name, export_name); generate_action_id(&self.config.hash_salt, &self.file_name, export_name);
let span = Span::dummy_with_cmt();
self.comments.add_pure_comment(span.lo);
if export_name == "default" { if export_name == "default" {
let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr( let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(
ExportDefaultExpr { ExportDefaultExpr {
span: DUMMY_SP, span: DUMMY_SP,
expr: Box::new(Expr::Call(CallExpr { expr: Box::new(Expr::Call(CallExpr {
span: DUMMY_SP, span,
callee: Callee::Expr(Box::new(Expr::Ident( callee: Callee::Expr(Box::new(Expr::Ident(
create_ref_ident.clone(), create_ref_ident.clone(),
))), ))),
...@@ -1025,7 +1043,7 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1025,7 +1043,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
.into(), .into(),
), ),
init: Some(Box::new(Expr::Call(CallExpr { init: Some(Box::new(Expr::Call(CallExpr {
span: DUMMY_SP, span,
callee: Callee::Expr(Box::new(Expr::Ident( callee: Callee::Expr(Box::new(Expr::Ident(
create_ref_ident.clone(), create_ref_ident.clone(),
))), ))),
...@@ -1704,6 +1722,7 @@ fn collect_decl_idents_in_stmt(stmt: &Stmt, ids: &mut Vec<Id>) { ...@@ -1704,6 +1722,7 @@ fn collect_decl_idents_in_stmt(stmt: &Stmt, ids: &mut Vec<Id>) {
pub(crate) struct ClosureReplacer<'a> { pub(crate) struct ClosureReplacer<'a> {
used_ids: &'a [Name], used_ids: &'a [Name],
private_ctxt: SyntaxContext,
} }
impl ClosureReplacer<'_> { impl ClosureReplacer<'_> {
...@@ -1722,7 +1741,7 @@ impl VisitMut for ClosureReplacer<'_> { ...@@ -1722,7 +1741,7 @@ impl VisitMut for ClosureReplacer<'_> {
// $$ACTION_ARG_0 // $$ACTION_ARG_0
format!("$$ACTION_ARG_{index}").into(), format!("$$ACTION_ARG_{index}").into(),
DUMMY_SP, DUMMY_SP,
Default::default(), self.private_ctxt,
)); ));
} }
} }
...@@ -1739,7 +1758,7 @@ impl VisitMut for ClosureReplacer<'_> { ...@@ -1739,7 +1758,7 @@ impl VisitMut for ClosureReplacer<'_> {
// $$ACTION_ARG_0 // $$ACTION_ARG_0
format!("$$ACTION_ARG_{index}").into(), format!("$$ACTION_ARG_{index}").into(),
DUMMY_SP, DUMMY_SP,
Default::default(), self.private_ctxt,
))), ))),
}))); })));
} }
......
...@@ -135,3 +135,13 @@ ...@@ -135,3 +135,13 @@
: ^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^
17 | } from 'react' 17 | } from 'react'
`---- `----
x You're importing a component that needs `experimental_useOptimistic`. This React hook only works in a client component. To fix, mark the file (or its parent) with the `"use client"` directive.
|
| Learn more: https://nextjs.org/docs/app/building-your-application/rendering/client-components
|
|
,-[input.js:19:1]
18 |
19 | import { experimental_useOptimistic as useOptimistic } from 'react'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^
`----
// app/send.ts // app/send.ts
/* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","e10665baac148856374b2789aceb970f66fec33e":"myAction"} */ export default createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d"); /* __next_internal_action_entry_do_not_use__ {"c18c215a6b7cdc64bf709f3a714ffdef1bf9651d":"default","e10665baac148856374b2789aceb970f66fec33e":"myAction"} */ export default /*#__PURE__*/ createServerReference("c18c215a6b7cdc64bf709f3a714ffdef1bf9651d");
import { createServerReference } from "private-next-rsc-action-client-wrapper"; import { createServerReference } from "private-next-rsc-action-client-wrapper";
export var myAction = createServerReference("e10665baac148856374b2789aceb970f66fec33e"); export var myAction = /*#__PURE__*/ createServerReference("e10665baac148856374b2789aceb970f66fec33e");
// app/send.ts // app/send.ts
/* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ export var foo = createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b"); /* __next_internal_action_entry_do_not_use__ {"ab21efdafbe611287bc25c0462b1e0510d13e48b":"foo"} */ export var foo = /*#__PURE__*/ createServerReference("ab21efdafbe611287bc25c0462b1e0510d13e48b");
import { createServerReference } from "private-next-rsc-action-client-wrapper"; import { createServerReference } from "private-next-rsc-action-client-wrapper";
/* __next_internal_action_entry_do_not_use__ {"a0c73dd6f5af839e3335c6b19262ecb86cca6af4":"sampleFunction2","bd336abe00c3c59da66acb696fc8e151d8e54ea4":"sampleFunction","d4f2e95bc745b6500b439c0847003511748c8ece":"sampleFunction3","f03b256ee88a51700367acee3082894e25e6e7d9":"sampleFunction4"} */ export var sampleFunction4 = createServerReference("f03b256ee88a51700367acee3082894e25e6e7d9"); /* __next_internal_action_entry_do_not_use__ {"a0c73dd6f5af839e3335c6b19262ecb86cca6af4":"sampleFunction2","bd336abe00c3c59da66acb696fc8e151d8e54ea4":"sampleFunction","d4f2e95bc745b6500b439c0847003511748c8ece":"sampleFunction3","f03b256ee88a51700367acee3082894e25e6e7d9":"sampleFunction4"} */ export var sampleFunction4 = /*#__PURE__*/ createServerReference("f03b256ee88a51700367acee3082894e25e6e7d9");
import { createServerReference } from "private-next-rsc-action-client-wrapper"; import { createServerReference } from "private-next-rsc-action-client-wrapper";
export var sampleFunction = createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4"); export var sampleFunction = /*#__PURE__*/ createServerReference("bd336abe00c3c59da66acb696fc8e151d8e54ea4");
export var sampleFunction2 = createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4"); export var sampleFunction2 = /*#__PURE__*/ createServerReference("a0c73dd6f5af839e3335c6b19262ecb86cca6af4");
export var sampleFunction3 = createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece"); export var sampleFunction3 = /*#__PURE__*/ createServerReference("d4f2e95bc745b6500b439c0847003511748c8ece");
/* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ export var bar = createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272"); /* __next_internal_action_entry_do_not_use__ {"ac840dcaf5e8197cb02b7f3a43c119b7a770b272":"bar"} */ export var bar = /*#__PURE__*/ createServerReference("ac840dcaf5e8197cb02b7f3a43c119b7a770b272");
import { createServerReference } from "private-next-rsc-action-client-wrapper"; import { createServerReference } from "private-next-rsc-action-client-wrapper";
/* __next_internal_action_entry_do_not_use__ {"0090eaf4e1f08a2d94f6be401e54a2ded399b87c":"action0","188d5d945750dc32e2c842b93c75a65763d4a922":"$$ACTION_1","6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0","9878bfa39811ca7650992850a8751f9591b6a557":"$$ACTION_2"} */ export var action0 = createServerReference("0090eaf4e1f08a2d94f6be401e54a2ded399b87c"); /* __next_internal_action_entry_do_not_use__ {"0090eaf4e1f08a2d94f6be401e54a2ded399b87c":"action0","188d5d945750dc32e2c842b93c75a65763d4a922":"$$ACTION_1","6d53ce510b2e36499b8f56038817b9bad86cabb4":"$$ACTION_0","9878bfa39811ca7650992850a8751f9591b6a557":"$$ACTION_2"} */ export var action0 = /*#__PURE__*/ createServerReference("0090eaf4e1f08a2d94f6be401e54a2ded399b87c");
import { createServerReference } from "private-next-rsc-action-client-wrapper"; import { createServerReference } from "private-next-rsc-action-client-wrapper";
...@@ -3,7 +3,7 @@ use std::{cell::RefCell, collections::HashMap, mem::take}; ...@@ -3,7 +3,7 @@ use std::{cell::RefCell, collections::HashMap, mem::take};
use swc_core::{ use swc_core::{
base::SwcComments, base::SwcComments,
common::{ common::{
comments::{Comment, Comments}, comments::{Comment, CommentKind, Comments},
BytePos, BytePos,
}, },
}; };
...@@ -130,6 +130,31 @@ impl Comments for ImmutableComments { ...@@ -130,6 +130,31 @@ impl Comments for ImmutableComments {
panic!("Comments are immutable after parsing") panic!("Comments are immutable after parsing")
} }
fn has_flag(&self, pos: BytePos, flag: &str) -> bool {
self.with_leading(pos, |cmts| {
for c in cmts {
if c.kind == CommentKind::Block {
for line in c.text.lines() {
// jsdoc
let line = line.trim_start_matches(['*', ' ']);
let line = line.trim();
//
if line.len() == (flag.len() + 5)
&& (line.starts_with("#__") || line.starts_with("@__"))
&& line.ends_with("__")
&& flag == &line[3..line.len() - 2]
{
return true;
}
}
}
}
false
})
}
fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret
where where
Self: Sized, Self: Sized,
......
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