Skip to content

Commit

Permalink
Merge branch 'canary' into shuding/next-208-implement-route-announcer…
Browse files Browse the repository at this point in the history
…-for-app
  • Loading branch information
shuding committed Mar 13, 2023
2 parents 060bec4 + b21c85b commit 64f46e6
Show file tree
Hide file tree
Showing 13 changed files with 190 additions and 13 deletions.
136 changes: 123 additions & 13 deletions packages/next-swc/crates/core/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,21 +855,98 @@ fn annotate_ident_as_action(
));
}

const DIRECTIVE_TYPOS: &[&str] = &[
"use servers",
"use-server",
"use sevrer",
"use srever",
"use servre",
"user server",
];

fn remove_server_directive_index_in_module(
stmts: &mut Vec<ModuleItem>,
in_action_file: &mut bool,
has_action: &mut bool,
) {
let mut is_directive = true;

stmts.retain(|stmt| {
if let ModuleItem::Stmt(Stmt::Expr(ExprStmt {
expr: box Expr::Lit(Lit::Str(Str { value, .. })),
..
})) = stmt
{
if value == "use server" {
*in_action_file = true;
*has_action = true;
return false;
match stmt {
ModuleItem::Stmt(Stmt::Expr(ExprStmt {
expr: box Expr::Lit(Lit::Str(Str { value, span, .. })),
..
})) => {
if value == "use server" {
if is_directive {
*in_action_file = true;
*has_action = true;
return false;
} else {
HANDLER.with(|handler| {
handler
.struct_span_err(
*span,
"The \"use server\" directive must be at the top of the file.",
)
.emit();
});
}
} else {
// Detect typo of "use server"
if DIRECTIVE_TYPOS.iter().any(|&s| s == value) {
HANDLER.with(|handler| {
handler
.struct_span_err(
*span,
format!(
"Did you mean \"use server\"? \"{}\" is not a supported \
directive name.",
value
)
.as_str(),
)
.emit();
});
}
}
}
ModuleItem::Stmt(Stmt::Expr(ExprStmt {
expr:
box Expr::Paren(ParenExpr {
expr: box Expr::Lit(Lit::Str(Str { value, .. })),
..
}),
span,
..
})) => {
// Match `("use server")`.
if value == "use server" || DIRECTIVE_TYPOS.iter().any(|&s| s == value) {
if is_directive {
HANDLER.with(|handler| {
handler
.struct_span_err(
*span,
"The \"use server\" directive cannot be wrapped in \
parentheses.",
)
.emit();
})
} else {
HANDLER.with(|handler| {
handler
.struct_span_err(
*span,
"The \"use server\" directive must be at the top of the file, \
and cannot be wrapped in parentheses.",
)
.emit();
})
}
}
}
_ => {
is_directive = false;
}
}
true
Expand All @@ -881,18 +958,51 @@ fn remove_server_directive_index_in_fn(
remove_directive: bool,
is_action_fn: &mut bool,
) {
let mut is_directive = true;

stmts.retain(|stmt| {
if let Stmt::Expr(ExprStmt {
expr: box Expr::Lit(Lit::Str(Str { value, .. })),
expr: box Expr::Lit(Lit::Str(Str { value, span, .. })),
..
}) = stmt
{
if value == "use server" {
*is_action_fn = true;
if remove_directive {
return false;
if is_directive {
*is_action_fn = true;
if remove_directive {
return false;
}
} else {
HANDLER.with(|handler| {
handler
.struct_span_err(
*span,
"The \"use server\" directive must be at the top of the function \
body.",
)
.emit();
});
}
} else {
// Detect typo of "use server"
if DIRECTIVE_TYPOS.iter().any(|&s| s == value) {
HANDLER.with(|handler| {
handler
.struct_span_err(
*span,
format!(
"Did you mean \"use server\"? \"{}\" is not a supported \
directive name.",
value
)
.as_str(),
)
.emit();
});
}
}
} else {
is_directive = false;
}
true
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use sevrer'

export async function foo () {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use sevrer';
export async function foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

x Did you mean "use server"? "use sevrer" is not a supported directive name.
,-[input.js:1:1]
1 | 'use sevrer'
: ^^^^^^^^^^^^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'react'

('use sevrer')

// ^Tools like Prettier sometimes do this.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'react';
'use sevrer' // ^Tools like Prettier sometimes do this.
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x The "use server" directive must be at the top of the file, and cannot be wrapped in parentheses.
,-[input.js:2:1]
2 |
3 | ('use sevrer')
: ^^^^^^^^^^^^^^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const foo = async () => {
'use strict'
'use server'
}

const bar = async () => {
const x = 1
'use server'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* __next_internal_action_entry_do_not_use__ $$ACTION_1 */ const foo = $$ACTION_0 = async ()=>$$ACTION_1($$ACTION_0.$$bound);
$$ACTION_0.$$typeof = Symbol.for("react.server.reference");
$$ACTION_0.$$id = "188d5d945750dc32e2c842b93c75a65763d4a922";
$$ACTION_0.$$bound = [];
export const $$ACTION_1 = async (closure)=>{
'use strict';
};
var $$ACTION_0;
const bar = async ()=>{
const x = 1;
'use server';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

x The "use server" directive must be at the top of the function body.
,-[input.js:7:1]
7 | const x = 1
8 | 'use server'
: ^^^^^^^^^^^^
9 | }
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'my-module'

'use server'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'my-module';
'use server';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

x The "use server" directive must be at the top of the file.
,-[input.js:2:1]
2 |
3 | 'use server'
: ^^^^^^^^^^^^
`----

0 comments on commit 64f46e6

Please sign in to comment.