Unverified Commit 54770906 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Emit plugin errors for metadata API in client entries (#45613)

NEXT-461

<img width="720" alt="CleanShot-2023-02-06-snyeDfbt@2x" src="https://user-images.githubusercontent.com/3676859/216978751-9dbb1901-a38d-4ade-bc72-7c4f02e64b35.png">

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 30a6c512
...@@ -6,6 +6,7 @@ export const NEXT_TS_ERRORS = { ...@@ -6,6 +6,7 @@ export const NEXT_TS_ERRORS = {
INVALID_PAGE_PROP: 71005, INVALID_PAGE_PROP: 71005,
INVALID_CONFIG_OPTION: 71006, INVALID_CONFIG_OPTION: 71006,
INVALID_CLIENT_ENTRY_PROP: 71007, INVALID_CLIENT_ENTRY_PROP: 71007,
INVALID_METADATA_EXPORT: 71008,
} }
export const ALLOWED_EXPORTS = [ export const ALLOWED_EXPORTS = [
......
...@@ -214,11 +214,15 @@ export function createTSPlugin(modules: { ...@@ -214,11 +214,15 @@ export function createTSPlugin(modules: {
source, source,
node node
) )
const metadataDiagnostics = const metadataDiagnostics = isClientEntry
metadata.getSemanticDiagnosticsForExportVariableStatement( ? metadata.getSemanticDiagnosticsForExportVariableStatementInClientEntry(
fileName, fileName,
node node
) )
: metadata.getSemanticDiagnosticsForExportVariableStatement(
fileName,
node
)
prior.push(...diagnostics, ...metadataDiagnostics) prior.push(...diagnostics, ...metadataDiagnostics)
} }
......
import { NEXT_TS_ERRORS } from '../constant'
import { getInfo, getSource, getTs, isPositionInsideNode } from '../utils' import { getInfo, getSource, getTs, isPositionInsideNode } from '../utils'
const TYPE_ANOTATION = ': Metadata' const TYPE_ANOTATION = ': Metadata'
...@@ -222,6 +223,32 @@ const metadata = { ...@@ -222,6 +223,32 @@ const metadata = {
return prior return prior
}, },
getSemanticDiagnosticsForExportVariableStatementInClientEntry(
fileName: string,
node: ts.VariableStatement
) {
const source = getSource(fileName)
const ts = getTs()
for (const declaration of node.declarationList.declarations) {
const name = declaration.name.getText()
if (['metadata', 'generateMetadata'].includes(name)) {
// It is not allowed to use `metadata` or `generateMetadata` in client entry
return [
{
file: source,
category: ts.DiagnosticCategory.Error,
code: NEXT_TS_ERRORS.INVALID_METADATA_EXPORT,
messageText: `The Next.js '${name}' API is not allowed in a client component.`,
start: declaration.name.getStart(),
length: declaration.name.getWidth(),
},
]
}
}
return []
},
getSemanticDiagnosticsForExportVariableStatement( getSemanticDiagnosticsForExportVariableStatement(
fileName: string, fileName: string,
node: ts.VariableStatement node: ts.VariableStatement
......
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