Skip to content
Merged
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
122 changes: 60 additions & 62 deletions packages/next/src/server/typescript/rules/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,95 +166,93 @@ const metadata = {
continue
}
const exportName = e.name.getText()
if (exportName !== 'generateMetadata' && exportName !== 'metadata') {
if (exportName !== 'metadata' && exportName !== 'generateMetadata') {
continue
}

// Get the symbol and type for the export
const symbol = typeChecker.getSymbolAtLocation(e.name)
if (!symbol) continue
if (!symbol) {
continue
}

const originalSymbol = typeChecker.getAliasedSymbol(symbol)
const declarations = originalSymbol.getDeclarations()
if (!declarations) {
continue
}

const type = typeChecker.getTypeOfSymbolAtLocation(symbol, e.name)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is unnecessary as is providing typeof value.

if (!type) continue
const declaration = declarations[0]
if (hasType(declaration)) {
continue
}

if (exportName === 'generateMetadata') {
let isAsync = false

// For export declarations, we need to get the actual declaration through the type checker
const originalSymbol = typeChecker.getAliasedSymbol(symbol)
const declaration = originalSymbol?.declarations?.[0]
// async function() {}
if (ts.isFunctionDeclaration(declaration)) {
isAsync =
declaration.modifiers?.some(
(m) => m.kind === ts.SyntaxKind.AsyncKeyword
) ?? false
}

if (declaration) {
if (ts.isFunctionDeclaration(declaration)) {
// foo = async function() {}
// foo = async () => {}
if (
ts.isVariableDeclaration(declaration) &&
declaration.initializer
) {
const initializer = declaration.initializer
const isFunction =
ts.isArrowFunction(initializer) ||
ts.isFunctionExpression(initializer)

if (isFunction) {
isAsync =
declaration.modifiers?.some(
initializer.modifiers?.some(
(m) => m.kind === ts.SyntaxKind.AsyncKeyword
) ?? false
} else if (
ts.isVariableDeclaration(declaration) &&
declaration.initializer
) {
if (
ts.isArrowFunction(declaration.initializer) ||
ts.isFunctionExpression(declaration.initializer)
) {
isAsync =
declaration.initializer.modifiers?.some(
(m) => m.kind === ts.SyntaxKind.AsyncKeyword
) ?? false
}
}
}

if (
declaration &&
ts.isFunctionDeclaration(declaration) &&
!hasType(declaration)
) {
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT,
messageText: `The "generateMetadata" export should have a return type of ${isAsync ? '"Promise<Metadata>"' : '"Metadata"'} from "next".`,
start: e.name.getStart(),
length: e.name.getWidth(),
})
}
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT,
messageText: `The Next.js "generateMetadata" export should have a return type of ${isAsync ? '"Promise<Metadata>"' : '"Metadata"'} from "next".`,
start: e.name.getStart(),
length: e.name.getWidth(),
})
} else {
// must be 'metadata' at this point
const declaration = symbol.declarations?.[0]
if (
declaration &&
ts.isVariableDeclaration(declaration) &&
!hasType(declaration)
) {
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT,
messageText: `The Next.js "metadata" export should be type of "Metadata" from "next".`,
start: e.name.getStart(),
length: e.name.getWidth(),
})
}
diagnostics.push({
file: source,
category: ts.DiagnosticCategory.Warning,
code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT,
messageText: `The Next.js "metadata" export should be type of "Metadata" from "next".`,
start: e.name.getStart(),
length: e.name.getWidth(),
})
}
}
}

return diagnostics
},
},
}

function hasType(
node:
| tsModule.FunctionDeclaration
| tsModule.VariableDeclaration
| tsModule.FunctionExpression
| tsModule.ArrowFunction
): boolean {
function hasType(node: tsModule.Declaration): boolean {
const ts = getTs()

if (
!ts.isVariableDeclaration(node) &&
!ts.isFunctionDeclaration(node) &&
!ts.isArrowFunction(node) &&
!ts.isFunctionExpression(node)
) {
return false
}

// For function declarations, expressions, and arrow functions, check if they have return type
if (
ts.isFunctionDeclaration(node) ||
Expand Down
Loading