forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve TypeScript plugin for server boundary (vercel#63667)
For problems like vercel#62821, vercel#62860 and other, the only way to improve the DX would be relying on the type checker to ensure that Server Actions are async functions. Inlined definitions will always be checked by SWC (as they're always syntactically defined as functions already), but export values are sometimes determined at the runtime. Also added `react-dom` related methods to the disallow list for the server layer. Examples: https://github.com/vercel/next.js/assets/3676859/ac0b12fa-829b-42a4-a4c6-e1c321b68a8e https://github.com/vercel/next.js/assets/3676859/2e2e3ab8-6743-4281-9783-30bd2a82fb5c https://github.com/vercel/next.js/assets/3676859/b61a4c0a-1ad4-4ad6-9d50-311ef3450e13 Closes NEXT-2913
- Loading branch information
Showing
5 changed files
with
270 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
packages/next/src/server/typescript/rules/server-boundary.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// This module provides intellisense for all exports from `"use server"` directive. | ||
|
||
import { NEXT_TS_ERRORS } from '../constant' | ||
import { getTs, getTypeChecker } from '../utils' | ||
import type tsModule from 'typescript/lib/tsserverlibrary' | ||
|
||
// Check if the type is `Promise<T>`. | ||
function isPromiseType(type: tsModule.Type, typeChecker: tsModule.TypeChecker) { | ||
const typeReferenceType = type as tsModule.TypeReference | ||
if (!typeReferenceType.target) return false | ||
|
||
// target should be Promise or Promise<...> | ||
if ( | ||
!/^Promise(<.+>)?$/.test(typeChecker.typeToString(typeReferenceType.target)) | ||
) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
function isFunctionReturningPromise( | ||
node: tsModule.Node, | ||
typeChecker: tsModule.TypeChecker, | ||
ts: typeof tsModule | ||
) { | ||
const type = typeChecker.getTypeAtLocation(node) | ||
const signatures = typeChecker.getSignaturesOfType( | ||
type, | ||
ts.SignatureKind.Call | ||
) | ||
|
||
let isPromise = true | ||
if (signatures.length) { | ||
for (const signature of signatures) { | ||
const returnType = signature.getReturnType() | ||
if (returnType.isUnion()) { | ||
for (const t of returnType.types) { | ||
if (!isPromiseType(t, typeChecker)) { | ||
isPromise = false | ||
break | ||
} | ||
} | ||
} else { | ||
isPromise = isPromiseType(returnType, typeChecker) | ||
} | ||
} | ||
} else { | ||
isPromise = false | ||
} | ||
|
||
return isPromise | ||
} | ||
|
||
const serverBoundary = { | ||
getSemanticDiagnosticsForExportDeclaration( | ||
source: tsModule.SourceFile, | ||
node: tsModule.ExportDeclaration | ||
) { | ||
const ts = getTs() | ||
const typeChecker = getTypeChecker() | ||
if (!typeChecker) return [] | ||
|
||
const diagnostics: tsModule.Diagnostic[] = [] | ||
|
||
const exportClause = node.exportClause | ||
if (exportClause && ts.isNamedExports(exportClause)) { | ||
for (const e of exportClause.elements) { | ||
if (!isFunctionReturningPromise(e, typeChecker, ts)) { | ||
diagnostics.push({ | ||
file: source, | ||
category: ts.DiagnosticCategory.Error, | ||
code: NEXT_TS_ERRORS.INVALID_SERVER_ENTRY_RETURN, | ||
messageText: `The "use server" file can only export async functions.`, | ||
start: e.getStart(), | ||
length: e.getWidth(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return diagnostics | ||
}, | ||
|
||
getSemanticDiagnosticsForExportVariableStatement( | ||
source: tsModule.SourceFile, | ||
node: tsModule.VariableStatement | ||
) { | ||
const ts = getTs() | ||
|
||
const diagnostics: tsModule.Diagnostic[] = [] | ||
|
||
if (ts.isVariableDeclarationList(node.declarationList)) { | ||
for (const declaration of node.declarationList.declarations) { | ||
const initializer = declaration.initializer | ||
if ( | ||
initializer && | ||
(ts.isArrowFunction(initializer) || | ||
ts.isFunctionDeclaration(initializer) || | ||
ts.isFunctionExpression(initializer)) | ||
) { | ||
diagnostics.push( | ||
...serverBoundary.getSemanticDiagnosticsForFunctionExport( | ||
source, | ||
initializer | ||
) | ||
) | ||
} else { | ||
diagnostics.push({ | ||
file: source, | ||
category: ts.DiagnosticCategory.Error, | ||
code: NEXT_TS_ERRORS.INVALID_SERVER_ENTRY_RETURN, | ||
messageText: `The "use server" file can only export async functions.`, | ||
start: declaration.getStart(), | ||
length: declaration.getWidth(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return diagnostics | ||
}, | ||
|
||
getSemanticDiagnosticsForFunctionExport( | ||
source: tsModule.SourceFile, | ||
node: | ||
| tsModule.FunctionDeclaration | ||
| tsModule.ArrowFunction | ||
| tsModule.FunctionExpression | ||
) { | ||
const ts = getTs() | ||
const typeChecker = getTypeChecker() | ||
if (!typeChecker) return [] | ||
|
||
const diagnostics: tsModule.Diagnostic[] = [] | ||
|
||
if (!isFunctionReturningPromise(node, typeChecker, ts)) { | ||
diagnostics.push({ | ||
file: source, | ||
category: ts.DiagnosticCategory.Error, | ||
code: NEXT_TS_ERRORS.INVALID_SERVER_ENTRY_RETURN, | ||
messageText: `The "use server" file can only export async functions. Add "async" to the function declaration or return a Promise.`, | ||
start: node.getStart(), | ||
length: node.getWidth(), | ||
}) | ||
} | ||
|
||
return diagnostics | ||
}, | ||
} | ||
|
||
export default serverBoundary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.