From 17eb1d83ca57ccbb921ef105007c9b28eef8e12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 20 Feb 2024 10:17:03 +0900 Subject: [PATCH 1/2] Highlight only directive line --- .../src/transforms/server_actions.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/next-swc/crates/next-custom-transforms/src/transforms/server_actions.rs b/packages/next-swc/crates/next-custom-transforms/src/transforms/server_actions.rs index 79badb09d7216..3651b56b7a87b 100644 --- a/packages/next-swc/crates/next-custom-transforms/src/transforms/server_actions.rs +++ b/packages/next-swc/crates/next-custom-transforms/src/transforms/server_actions.rs @@ -6,6 +6,7 @@ use std::{ use hex::encode as hex_encode; use serde::Deserialize; use sha1::{Digest, Sha1}; +use swc_core::common::Span; use turbopack_binding::swc::core::{ common::{ comments::{Comment, CommentKind, Comments}, @@ -125,10 +126,12 @@ impl ServerActions { } else { // Check if the function has `"use server"` if let Some(body) = maybe_body { + let mut action_span = None; remove_server_directive_index_in_fn( &mut body.stmts, remove_directive, &mut is_action_fn, + &mut action_span, self.config.enabled, ); @@ -136,7 +139,7 @@ impl ServerActions { HANDLER.with(|handler| { handler .struct_span_err( - body.span, + action_span.unwrap_or(body.span), "It is not allowed to define inline \"use server\" annotated Server Actions in Client Components.\nTo use Server Actions in a Client Component, you can either export them from a separate file with \"use server\" at the top, or pass them down through props from a Server Component.\n\nRead more: https://nextjs.org/docs/app/api-reference/functions/server-actions#with-client-components\n", ) .emit() @@ -1396,6 +1399,7 @@ fn remove_server_directive_index_in_fn( stmts: &mut Vec, remove_directive: bool, is_action_fn: &mut bool, + action_span: &mut Option, enabled: bool, ) { let mut is_directive = true; @@ -1407,6 +1411,8 @@ fn remove_server_directive_index_in_fn( }) = stmt { if value == "use server" { + *action_span = Some(*span); + if is_directive { *is_action_fn = true; if !enabled { From 5f60a7c3fa147b10694aab96c1b9ab1c1e9b0df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Tue, 20 Feb 2024 11:04:52 +0900 Subject: [PATCH 2/2] Update test refs --- .../server-actions/client-graph/1/output.stderr | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/next-swc/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.stderr b/packages/next-swc/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.stderr index 9f20dd6734675..25657d291a76d 100644 --- a/packages/next-swc/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.stderr +++ b/packages/next-swc/crates/next-custom-transforms/tests/errors/server-actions/client-graph/1/output.stderr @@ -4,10 +4,9 @@ | | Read more: https://nextjs.org/docs/app/api-reference/functions/server-actions#with-client-components | - ,-[input.js:3:1] - 3 | export default function App() { - 4 | ,-> async function fn() { - 5 | | 'use server' - 6 | `-> } - 7 | return
App
+ ,-[input.js:4:1] + 4 | async function fn() { + 5 | 'use server' + : ^^^^^^^^^^^^ + 6 | } `----