Unverified Commit b21c85bc authored by Shu Ding's avatar Shu Ding Committed by GitHub

Detect common invalid cases for the server directive (#47069)

It will be crucial to detect these common problems and abort compilation, because it's intended to be using a server boundary but these problems might cause it ending up in the client.

Fixes NEXT-762.
parent 0b3e3578
...@@ -855,21 +855,98 @@ fn annotate_ident_as_action( ...@@ -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( fn remove_server_directive_index_in_module(
stmts: &mut Vec<ModuleItem>, stmts: &mut Vec<ModuleItem>,
in_action_file: &mut bool, in_action_file: &mut bool,
has_action: &mut bool, has_action: &mut bool,
) { ) {
let mut is_directive = true;
stmts.retain(|stmt| { stmts.retain(|stmt| {
if let ModuleItem::Stmt(Stmt::Expr(ExprStmt { match stmt {
expr: box Expr::Lit(Lit::Str(Str { value, .. })), ModuleItem::Stmt(Stmt::Expr(ExprStmt {
.. expr: box Expr::Lit(Lit::Str(Str { value, span, .. })),
})) = stmt ..
{ })) => {
if value == "use server" { if value == "use server" {
*in_action_file = true; if is_directive {
*has_action = true; *in_action_file = true;
return false; *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 true
...@@ -881,18 +958,51 @@ fn remove_server_directive_index_in_fn( ...@@ -881,18 +958,51 @@ fn remove_server_directive_index_in_fn(
remove_directive: bool, remove_directive: bool,
is_action_fn: &mut bool, is_action_fn: &mut bool,
) { ) {
let mut is_directive = true;
stmts.retain(|stmt| { stmts.retain(|stmt| {
if let Stmt::Expr(ExprStmt { if let Stmt::Expr(ExprStmt {
expr: box Expr::Lit(Lit::Str(Str { value, .. })), expr: box Expr::Lit(Lit::Str(Str { value, span, .. })),
.. ..
}) = stmt }) = stmt
{ {
if value == "use server" { if value == "use server" {
*is_action_fn = true; if is_directive {
if remove_directive { *is_action_fn = true;
return false; 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 true
}); });
......
x Did you mean "use server"? "use sevrer" is not a supported directive name.
,-[input.js:1:1]
1 | 'use sevrer'
: ^^^^^^^^^^^^
`----
import 'react'
('use sevrer')
// ^Tools like Prettier sometimes do this.
import 'react';
'use sevrer' // ^Tools like Prettier sometimes do this.
;
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')
: ^^^^^^^^^^^^^^
`----
const foo = async () => {
'use strict'
'use server'
}
const bar = async () => {
const x = 1
'use server'
}
/* __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';
};
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 | }
`----
x The "use server" directive must be at the top of the file.
,-[input.js:2:1]
2 |
3 | 'use server'
: ^^^^^^^^^^^^
`----
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