Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
45d57323
Unverified
Commit
45d57323
authored
Feb 05, 2023
by
Donny/강동윤
Committed by
GitHub
Feb 05, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve swc transforms (#45594)
parent
dc0aea54
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
148 additions
and
26 deletions
+148
-26
server_actions.rs
packages/next-swc/crates/core/src/server_actions.rs
+118
-26
input.js
...t-swc/crates/core/tests/fixture/server-actions/7/input.js
+9
-0
output.js
...-swc/crates/core/tests/fixture/server-actions/7/output.js
+21
-0
No files found.
packages/next-swc/crates/core/src/server_actions.rs
View file @
45d57323
use
std
::
convert
::{
TryFrom
,
TryInto
};
use
next_binding
::
swc
::
core
::{
use
next_binding
::
swc
::
core
::{
common
::{
common
::{
comments
::{
Comment
,
CommentKind
,
Comments
},
comments
::{
Comment
,
CommentKind
,
Comments
},
...
@@ -9,9 +11,9 @@ use next_binding::swc::core::{
...
@@ -9,9 +11,9 @@ use next_binding::swc::core::{
ast
::{
ast
::{
op
,
ArrayLit
,
AssignExpr
,
AssignPatProp
,
BlockStmt
,
CallExpr
,
ComputedPropName
,
Decl
,
op
,
ArrayLit
,
AssignExpr
,
AssignPatProp
,
BlockStmt
,
CallExpr
,
ComputedPropName
,
Decl
,
ExportDecl
,
Expr
,
ExprStmt
,
FnDecl
,
Function
,
Id
,
Ident
,
KeyValuePatProp
,
KeyValueProp
,
ExportDecl
,
Expr
,
ExprStmt
,
FnDecl
,
Function
,
Id
,
Ident
,
KeyValuePatProp
,
KeyValueProp
,
Lit
,
MemberExpr
,
MemberProp
,
Module
,
ModuleDecl
,
ModuleItem
,
ObjectPatProp
,
Param
,
Pat
,
Lit
,
MemberExpr
,
MemberProp
,
Module
,
ModuleDecl
,
ModuleItem
,
ObjectPatProp
,
PatOrExpr
,
Prop
,
PropName
,
RestPat
,
ReturnStmt
,
Stmt
,
Str
,
VarDecl
,
VarDeclKind
,
OptChainBase
,
OptChainExpr
,
Param
,
Pat
,
PatOrExpr
,
Prop
,
PropName
,
RestPat
,
ReturnStmt
,
VarDeclarator
,
Stmt
,
Str
,
VarDecl
,
VarDeclKind
,
VarDeclarator
,
},
},
atoms
::
JsWord
,
atoms
::
JsWord
,
utils
::{
private_ident
,
quote_ident
,
ExprFactory
},
utils
::{
private_ident
,
quote_ident
,
ExprFactory
},
...
@@ -43,6 +45,7 @@ pub fn server_actions<C: Comments>(
...
@@ -43,6 +45,7 @@ pub fn server_actions<C: Comments>(
in_module
:
true
,
in_module
:
true
,
in_action_fn
:
false
,
in_action_fn
:
false
,
should_add_name
:
false
,
closure_idents
:
Default
::
default
(),
closure_idents
:
Default
::
default
(),
action_idents
:
Default
::
default
(),
action_idents
:
Default
::
default
(),
...
@@ -66,8 +69,9 @@ struct ServerActions<C: Comments> {
...
@@ -66,8 +69,9 @@ struct ServerActions<C: Comments> {
in_module
:
bool
,
in_module
:
bool
,
in_action_fn
:
bool
,
in_action_fn
:
bool
,
should_add_name
:
bool
,
closure_idents
:
Vec
<
Id
>
,
closure_idents
:
Vec
<
Id
>
,
action_idents
:
Vec
<
Id
>
,
action_idents
:
Vec
<
Name
>
,
annotations
:
Vec
<
Stmt
>
,
annotations
:
Vec
<
Stmt
>
,
extra_items
:
Vec
<
ModuleItem
>
,
extra_items
:
Vec
<
ModuleItem
>
,
...
@@ -107,11 +111,14 @@ impl<C: Comments> VisitMut for ServerActions<C> {
...
@@ -107,11 +111,14 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// Visit children
// Visit children
let
old_in_action_fn
=
self
.in_action_fn
;
let
old_in_action_fn
=
self
.in_action_fn
;
let
old_in_module
=
self
.in_module
;
let
old_in_module
=
self
.in_module
;
let
old_should_add_name
=
self
.should_add_name
;
self
.in_action_fn
=
in_action_fn
;
self
.in_action_fn
=
in_action_fn
;
self
.in_module
=
false
;
self
.in_module
=
false
;
self
.should_add_name
=
true
;
f
.visit_mut_children_with
(
self
);
f
.visit_mut_children_with
(
self
);
self
.in_action_fn
=
old_in_action_fn
;
self
.in_action_fn
=
old_in_action_fn
;
self
.in_module
=
old_in_module
;
self
.in_module
=
old_in_module
;
self
.should_add_name
=
old_should_add_name
;
}
}
if
!
in_action_fn
{
if
!
in_action_fn
{
...
@@ -185,7 +192,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
...
@@ -185,7 +192,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
// Hoist the function to the top level.
// Hoist the function to the top level.
let
mut
ids_from_closure
=
self
.action_idents
.clone
();
let
mut
ids_from_closure
=
self
.action_idents
.clone
();
ids_from_closure
.retain
(|
id
|
self
.closure_idents
.contains
(
id
));
ids_from_closure
.retain
(|
id
|
self
.closure_idents
.contains
(
&
id
.
0
));
let
closure_arg
=
private_ident!
(
"closure"
);
let
closure_arg
=
private_ident!
(
"closure"
);
...
@@ -299,12 +306,18 @@ impl<C: Comments> VisitMut for ServerActions<C> {
...
@@ -299,12 +306,18 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}
}
}
}
fn
visit_mut_ident
(
&
mut
self
,
n
:
&
mut
Ident
)
{
fn
visit_mut_expr
(
&
mut
self
,
n
:
&
mut
Expr
)
{
n
.visit_mut_children_with
(
self
);
if
self
.in_action_fn
&&
self
.should_add_name
{
if
let
Ok
(
name
)
=
Name
::
try_from
(
&*
n
)
{
if
self
.in_action_fn
{
self
.should_add_name
=
false
;
self
.action_idents
.push
(
n
.to_id
())
self
.action_idents
.push
(
name
);
n
.visit_mut_children_with
(
self
);
self
.should_add_name
=
true
;
return
;
}
}
}
n
.visit_mut_children_with
(
self
);
}
}
fn
visit_mut_module_items
(
&
mut
self
,
stmts
:
&
mut
Vec
<
ModuleItem
>
)
{
fn
visit_mut_module_items
(
&
mut
self
,
stmts
:
&
mut
Vec
<
ModuleItem
>
)
{
...
@@ -487,14 +500,18 @@ fn collect_idents_in_stmt(stmt: &Stmt) -> Vec<Id> {
...
@@ -487,14 +500,18 @@ fn collect_idents_in_stmt(stmt: &Stmt) -> Vec<Id> {
pub
(
crate
)
struct
ClosureReplacer
<
'a
>
{
pub
(
crate
)
struct
ClosureReplacer
<
'a
>
{
closure_arg
:
&
'a
Ident
,
closure_arg
:
&
'a
Ident
,
used_ids
:
&
'a
[
Id
],
used_ids
:
&
'a
[
Name
],
}
}
impl
ClosureReplacer
<
'_
>
{
impl
ClosureReplacer
<
'_
>
{
fn
index
(
&
self
,
i
:
&
Ident
)
->
Option
<
usize
>
{
fn
index_of_id
(
&
self
,
i
:
&
Ident
)
->
Option
<
usize
>
{
self
.used_ids
let
name
=
Name
(
i
.to_id
(),
vec!
[]);
.iter
()
self
.used_ids
.iter
()
.position
(|
used_id
|
*
used_id
==
name
)
.position
(|
used_id
|
i
.sym
==
used_id
.
0
&&
i
.span.ctxt
==
used_id
.
1
)
}
fn
index
(
&
self
,
e
:
&
Expr
)
->
Option
<
usize
>
{
let
name
=
Name
::
try_from
(
e
)
.ok
()
?
;
self
.used_ids
.iter
()
.position
(|
used_id
|
*
used_id
==
name
)
}
}
}
}
...
@@ -502,17 +519,15 @@ impl VisitMut for ClosureReplacer<'_> {
...
@@ -502,17 +519,15 @@ impl VisitMut for ClosureReplacer<'_> {
fn
visit_mut_expr
(
&
mut
self
,
e
:
&
mut
Expr
)
{
fn
visit_mut_expr
(
&
mut
self
,
e
:
&
mut
Expr
)
{
e
.visit_mut_children_with
(
self
);
e
.visit_mut_children_with
(
self
);
if
let
Expr
::
Ident
(
i
)
=
e
{
if
let
Some
(
index
)
=
self
.index
(
e
)
{
if
let
Some
(
index
)
=
self
.index
(
i
)
{
*
e
=
Expr
::
Member
(
MemberExpr
{
*
e
=
Expr
::
Member
(
MemberExpr
{
span
:
DUMMY_SP
,
obj
:
self
.closure_arg
.clone
()
.into
(),
prop
:
MemberProp
::
Computed
(
ComputedPropName
{
span
:
DUMMY_SP
,
span
:
DUMMY_SP
,
obj
:
self
.closure_arg
.clone
()
.into
(),
expr
:
index
.into
(),
prop
:
MemberProp
::
Computed
(
ComputedPropName
{
}),
span
:
DUMMY_SP
,
});
expr
:
index
.into
(),
}),
});
}
}
}
}
}
...
@@ -520,7 +535,7 @@ impl VisitMut for ClosureReplacer<'_> {
...
@@ -520,7 +535,7 @@ impl VisitMut for ClosureReplacer<'_> {
p
.visit_mut_children_with
(
self
);
p
.visit_mut_children_with
(
self
);
if
let
Prop
::
Shorthand
(
i
)
=
p
{
if
let
Prop
::
Shorthand
(
i
)
=
p
{
if
let
Some
(
index
)
=
self
.index
(
i
)
{
if
let
Some
(
index
)
=
self
.index
_of_id
(
i
)
{
*
p
=
Prop
::
KeyValue
(
KeyValueProp
{
*
p
=
Prop
::
KeyValue
(
KeyValueProp
{
key
:
PropName
::
Ident
(
i
.clone
()),
key
:
PropName
::
Ident
(
i
.clone
()),
value
:
MemberExpr
{
value
:
MemberExpr
{
...
@@ -539,3 +554,80 @@ impl VisitMut for ClosureReplacer<'_> {
...
@@ -539,3 +554,80 @@ impl VisitMut for ClosureReplacer<'_> {
noop_visit_mut_type!
();
noop_visit_mut_type!
();
}
}
#[derive(Debug,
Clone,
PartialEq,
Eq)]
struct
Name
(
Id
,
Vec
<
(
JsWord
,
bool
)
>
);
impl
TryFrom
<&
'_
Expr
>
for
Name
{
type
Error
=
();
fn
try_from
(
value
:
&
Expr
)
->
Result
<
Self
,
Self
::
Error
>
{
match
value
{
Expr
::
Ident
(
i
)
=>
Ok
(
Name
(
i
.to_id
(),
vec!
[])),
Expr
::
Member
(
e
)
=>
e
.try_into
(),
Expr
::
OptChain
(
e
)
=>
e
.try_into
(),
_
=>
Err
(()),
}
}
}
impl
TryFrom
<&
'_
MemberExpr
>
for
Name
{
type
Error
=
();
fn
try_from
(
value
:
&
MemberExpr
)
->
Result
<
Self
,
Self
::
Error
>
{
match
&
value
.prop
{
MemberProp
::
Ident
(
prop
)
=>
{
let
mut
obj
:
Name
=
value
.obj
.as_ref
()
.try_into
()
?
;
obj
.
1
.push
((
prop
.sym
.clone
(),
true
));
Ok
(
obj
)
}
_
=>
Err
(()),
}
}
}
impl
TryFrom
<&
'_
OptChainExpr
>
for
Name
{
type
Error
=
();
fn
try_from
(
value
:
&
OptChainExpr
)
->
Result
<
Self
,
Self
::
Error
>
{
match
&
value
.base
{
OptChainBase
::
Member
(
value
)
=>
match
&
value
.prop
{
MemberProp
::
Ident
(
prop
)
=>
{
let
mut
obj
:
Name
=
value
.obj
.as_ref
()
.try_into
()
?
;
obj
.
1
.push
((
prop
.sym
.clone
(),
false
));
Ok
(
obj
)
}
_
=>
Err
(()),
},
OptChainBase
::
Call
(
_
)
=>
Err
(()),
}
}
}
impl
From
<
Name
>
for
Expr
{
fn
from
(
value
:
Name
)
->
Self
{
let
mut
expr
=
Expr
::
Ident
(
value
.
0
.into
());
for
(
prop
,
is_member
)
in
value
.
1
.into_iter
()
{
if
is_member
{
expr
=
Expr
::
Member
(
MemberExpr
{
span
:
DUMMY_SP
,
obj
:
expr
.into
(),
prop
:
MemberProp
::
Ident
(
Ident
::
new
(
prop
,
DUMMY_SP
)),
});
}
else
{
expr
=
Expr
::
OptChain
(
OptChainExpr
{
span
:
DUMMY_SP
,
question_dot_token
:
DUMMY_SP
,
base
:
OptChainBase
::
Member
(
MemberExpr
{
span
:
DUMMY_SP
,
obj
:
expr
.into
(),
prop
:
MemberProp
::
Ident
(
Ident
::
new
(
prop
,
DUMMY_SP
)),
}),
});
}
}
expr
}
}
packages/next-swc/crates/core/tests/fixture/server-actions/7/input.js
0 → 100644
View file @
45d57323
import
deleteFromDb
from
'db'
export
function
Item
(
product
,
foo
,
bar
)
{
async
function
deleteItem
()
{
"use server"
;
await
deleteFromDb
(
product
.
id
,
product
?.
foo
,
product
.
bar
.
baz
,
product
[
foo
,
bar
]);
}
return
<
Button
action
=
{
deleteItem
}
>
Delete
<
/Button>
;
}
\ No newline at end of file
packages/next-swc/crates/core/tests/fixture/server-actions/7/output.js
0 → 100644
View file @
45d57323
/* __next_internal_action_entry_do_not_use__ $ACTION_deleteItem */
import
deleteFromDb
from
'db'
;
export
function
Item
(
product
,
foo
,
bar
)
{
async
function
deleteItem
()
{
return
$ACTION_deleteItem
(
deleteItem
.
$$closure
);
}
deleteItem
.
$$typeof
=
Symbol
.
for
(
"react.server.reference"
);
deleteItem
.
$$filepath
=
"/app/item.js"
;
deleteItem
.
$$name
=
"$ACTION_deleteItem"
;
deleteItem
.
$$closure
=
[
product
.
id
,
product
?.
foo
,
product
.
bar
.
baz
,
product
,
foo
,
bar
];
return
<
Button
action
=
{
deleteItem
}
>
Delete
<
/Button>
;
}
export
async
function
$ACTION_deleteItem
(
closure
)
{
await
deleteFromDb
(
closure
[
3
].
id
,
closure
[
3
]?.
foo
,
closure
[
3
].
bar
.
baz
,
closure
[
3
][
closure
[
4
],
closure
[
5
]]);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment