Unverified Commit 08209b9b authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Improve error message for sync server functions (#81705)

When a server action or `'use cache'` function is defined as a
synchronous function, the error message now only marks the function name
for better clarity. Previously, the entire function body was marked,
which looks fine in small test fixtures, but is pretty messy for larger
functions, both in the terminal as well as in the dev error overlay.

For anonymous default exports we can't optimize this and are still
marking the entire function body, as we don't have a name to refer to.

closes NAR-165
parent 152dc29c
......@@ -998,9 +998,14 @@ impl<C: Comments> VisitMut for ServerActions<C> {
}
if let Some(directive) = directive {
let fn_name = self
.fn_decl_ident
.clone()
.or(self.arrow_or_fn_expr_ident.clone());
if !f.is_async {
emit_error(ServerActionsErrorKind::InlineSyncFunction {
span: f.span,
span: fn_name.as_ref().map_or(f.span, |ident| ident.span),
directive,
});
......@@ -1066,9 +1071,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let new_expr = self.maybe_hoist_and_create_proxy_for_server_action_function(
child_names,
f,
self.fn_decl_ident
.clone()
.or(self.arrow_or_fn_expr_ident.clone()),
fn_name,
);
if self.in_default_export_decl {
......@@ -1170,7 +1173,10 @@ impl<C: Comments> VisitMut for ServerActions<C> {
if let Some(directive) = directive {
if !a.is_async {
emit_error(ServerActionsErrorKind::InlineSyncFunction {
span: a.span,
span: self
.arrow_or_fn_expr_ident
.as_ref()
.map_or(a.span, |ident| ident.span),
directive,
});
......
......@@ -3,5 +3,5 @@
,-[input.js:3:1]
2 |
3 | export function foo() {}
: ^^^^^^^^^^^^^^^^^
: ^^^
`----
......@@ -11,7 +11,7 @@
,-[input.js:4:1]
3 | export default function () {}
4 | export function foo() {}
: ^^^^^^^^^^^^^^^^^
: ^^^
5 | export const bar = () => {}
`----
x "use cache" functions must be async functions.
......@@ -19,7 +19,7 @@
,-[input.js:5:1]
4 | export function foo() {}
5 | export const bar = () => {}
: ^^^^^^^^
: ^^^
6 | export const baz = 42
`----
x Only async functions are allowed to be exported in a "use cache" file.
......
export default function () {
export default function Default() {
'use cache'
}
......
export default function() {}
export default function Default() {}
export function foo() {}
export const bar = ()=>{};
x "use cache" functions must be async functions.
|
,-[input.js:1:1]
1 | ,-> export default function () {
2 | | 'use cache'
3 | `-> }
1 | export default function Default() {
: ^^^^^^^
2 | 'use cache'
`----
x "use cache" functions must be async functions.
|
,-[input.js:5:1]
4 |
5 | ,-> export function foo() {
6 | | 'use cache'
7 | `-> }
4 |
5 | export function foo() {
: ^^^
6 | 'use cache'
`----
x "use cache" functions must be async functions.
|
,-[input.js:9:1]
8 |
9 | ,-> export const bar = () => {
10 | | 'use cache'
11 | `-> }
8 |
9 | export const bar = () => {
: ^^^
10 | 'use cache'
`----
......@@ -3,5 +3,5 @@
,-[input.js:7:1]
6 |
7 | export function bar() {}
: ^^^^^^^^^^^^^^^^^
: ^^^
`----
x Server Actions must be async functions.
|
,-[input.js:1:1]
1 | ,-> const foo = () => {
2 | | 'use server'
3 | `-> }
1 | const foo = () => {
: ^^^
2 | '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