Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-impalas-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next": patch
---

[TypeScript Plugin] Moved the diagnostics' positions to the prop's type instead of the value for client-boundary warnings.
112 changes: 62 additions & 50 deletions packages/next/src/server/typescript/rules/client-boundary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,69 @@ const clientBoundary = {
const isErrorFile = /[\\/]error\.tsx?$/.test(source.fileName)
const isGlobalErrorFile = /[\\/]global-error\.tsx?$/.test(source.fileName)

const props = node.parameters?.[0]?.name
if (props && ts.isObjectBindingPattern(props)) {
for (const prop of (props as tsModule.ObjectBindingPattern).elements) {
const type = typeChecker.getTypeAtLocation(prop)
const typeDeclarationNode = type.symbol?.getDeclarations()?.[0]
const propName = (prop.propertyName || prop.name).getText()

if (typeDeclarationNode) {
if (ts.isFunctionTypeNode(typeDeclarationNode)) {
// By convention, props named "action" can accept functions since we
// assume these are Server Actions. Structurally, there's no
// difference between a Server Action and a normal function until
// TypeScript exposes directives in the type of a function. This
// will miss accidentally passing normal functions but a false
// negative is better than a false positive given how frequent the
// false-positive would be.
const maybeServerAction =
propName === 'action' || /.+Action$/.test(propName)

// There's a special case for the error file that the `reset` prop
// is allowed to be a function:
// https://github.com/vercel/next.js/issues/46573
const isErrorReset =
(isErrorFile || isGlobalErrorFile) && propName === 'reset'

if (!maybeServerAction && !isErrorReset) {
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_CLIENT_ENTRY_PROP,
messageText:
`Props must be serializable for components in the "use client" entry file. ` +
`"${propName}" is a function that's not a Server Action. ` +
`Rename "${propName}" either to "action" or have its name end with "Action" e.g. "${propName}Action" to indicate it is a Server Action.`,
start: prop.getStart(),
length: prop.getWidth(),
})
const props = node.parameters?.[0]
if (props) {
const propsType = typeChecker.getTypeAtLocation(props)
const typeNode = propsType.symbol?.getDeclarations()?.[0]

if (typeNode && ts.isTypeLiteralNode(typeNode)) {
Copy link
Copy Markdown
Member

@lubieowoce lubieowoce May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm assuming isTypeLiteralNode means that it's an object type written like { a: A, b: B }. might be worth looking into how to check interfaces and stuff like{ x: y } & WhateverProps too. I'm not sure how to do this in the TS API, maybe there's something to iterate over all members of an object-like type?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, quite a case to cover I think, will ITG on that if needed!

for (const member of typeNode.members) {
if (ts.isPropertySignature(member)) {
const propName = member.name.getText()
const propType = member.type

if (propType) {
const propTypeInfo = typeChecker.getTypeAtLocation(propType)
const typeDeclarationNode =
propTypeInfo.symbol?.getDeclarations()?.[0]

if (typeDeclarationNode) {
if (ts.isFunctionTypeNode(typeDeclarationNode)) {
// By convention, props named "action" can accept functions since we
// assume these are Server Actions. Structurally, there's no
// difference between a Server Action and a normal function until
// TypeScript exposes directives in the type of a function. This
// will miss accidentally passing normal functions but a false
// negative is better than a false positive given how frequent the
// false-positive would be.
const maybeServerAction =
propName === 'action' || /.+Action$/.test(propName)

// There's a special case for the error file that the `reset` prop
// is allowed to be a function:
// https://github.com/vercel/next.js/issues/46573
const isErrorReset =
(isErrorFile || isGlobalErrorFile) && propName === 'reset'

if (!maybeServerAction && !isErrorReset) {
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_CLIENT_ENTRY_PROP,
messageText:
`Props must be serializable for components in the "use client" entry file. ` +
`"${propName}" is a function that's not a Server Action. ` +
`Rename "${propName}" either to "action" or have its name end with "Action" e.g. "${propName}Action" to indicate it is a Server Action.`,
start: propType.getStart(),
length: propType.getWidth(),
})
}
} else if (
// Show warning for not serializable props.
ts.isConstructorTypeNode(typeDeclarationNode) ||
ts.isClassDeclaration(typeDeclarationNode)
) {
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_CLIENT_ENTRY_PROP,
messageText: `Props must be serializable for components in the "use client" entry file, "${propName}" is invalid.`,
start: propType.getStart(),
length: propType.getWidth(),
})
}
}
}
} else if (
// Show warning for not serializable props.
ts.isConstructorTypeNode(typeDeclarationNode) ||
ts.isClassDeclaration(typeDeclarationNode)
) {
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_CLIENT_ENTRY_PROP,
messageText: `Props must be serializable for components in the "use client" entry file, "${propName}" is invalid.`,
start: prop.getStart(),
length: prop.getWidth(),
})
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ class Class {}

type ArrowFunctionTypeAlias = () => void

export default function ClientComponent({
_arrowFunctionAction,
_arrowFunctionTypeAliasAction,
// Doesn't make sense, but check for loophole
_classAction,
_constructorAction,
}: {
export default function ClientComponent(props: {
_arrowFunctionAction: () => void
_arrowFunctionTypeAliasAction: ArrowFunctionTypeAlias
// Doesn't make sense, but check for loophole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ class Class {}

type ArrowFunctionTypeAlias = () => void

export default function ClientComponent({
_arrowFunction,
_arrowFunctionTypeAlias,
_class,
_constructor,
}: {
export default function ClientComponent(props: {
_arrowFunction: () => void
_arrowFunctionTypeAlias: ArrowFunctionTypeAlias
_class: Class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
'use client'

export default function ClientComponent({
_string,
_number,
_boolean,
_array,
_object,
_null,
_undefined,
}: {
export default function ClientComponent(props: {
_string: string
_number: number
_boolean: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ describe('typescript-plugin - client-boundary', () => {
"app/non-serializable-action-props.tsx": [
{
"code": 71007,
"length": 12,
"length": 5,
"messageText": "Props must be serializable for components in the "use client" entry file, "_classAction" is invalid.",
"start": 221,
"start": 276,
},
{
"code": 71007,
"length": 18,
"length": 14,
"messageText": "Props must be serializable for components in the "use client" entry file, "_constructorAction" is invalid.",
"start": 237,
"start": 304,
},
],
}
Expand All @@ -91,27 +91,27 @@ describe('typescript-plugin - client-boundary', () => {
"app/non-serializable-props.tsx": [
{
"code": 71007,
"length": 14,
"length": 10,
"messageText": "Props must be serializable for components in the "use client" entry file. "_arrowFunction" is a function that's not a Server Action. Rename "_arrowFunction" either to "action" or have its name end with "Action" e.g. "_arrowFunctionAction" to indicate it is a Server Action.",
"start": 116,
"start": 139,
},
{
"code": 71007,
"length": 23,
"length": 22,
"messageText": "Props must be serializable for components in the "use client" entry file. "_arrowFunctionTypeAlias" is a function that's not a Server Action. Rename "_arrowFunctionTypeAlias" either to "action" or have its name end with "Action" e.g. "_arrowFunctionTypeAliasAction" to indicate it is a Server Action.",
"start": 134,
"start": 177,
},
{
"code": 71007,
"length": 6,
"length": 5,
"messageText": "Props must be serializable for components in the "use client" entry file, "_class" is invalid.",
"start": 161,
"start": 210,
},
{
"code": 71007,
"length": 12,
"length": 14,
"messageText": "Props must be serializable for components in the "use client" entry file, "_constructor" is invalid.",
"start": 171,
"start": 232,
},
],
}
Expand Down
Loading