Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect common invalid cases for the server directive #47069

Merged
merged 11 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
: ^^^^^^^^^^^^
`----