Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
94ffcba2
Unverified
Commit
94ffcba2
authored
Feb 09, 2023
by
Shu Ding
Committed by
GitHub
Feb 09, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add IntelliSense for `generateMetadata` (#45723)
parent
628bc4c0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
63 deletions
+120
-63
index.ts
packages/next/src/server/typescript/index.ts
+13
-0
metadata.ts
packages/next/src/server/typescript/rules/metadata.ts
+107
-63
No files found.
packages/next/src/server/typescript/index.ts
View file @
94ffcba2
...
@@ -257,6 +257,19 @@ export function createTSPlugin(modules: {
...
@@ -257,6 +257,19 @@ export function createTSPlugin(modules: {
ts
.
isFunctionDeclaration
(
node
)
&&
ts
.
isFunctionDeclaration
(
node
)
&&
node
.
modifiers
?.
some
((
m
)
=>
m
.
kind
===
ts
.
SyntaxKind
.
ExportKeyword
)
node
.
modifiers
?.
some
((
m
)
=>
m
.
kind
===
ts
.
SyntaxKind
.
ExportKeyword
)
)
{
)
{
if
(
isAppEntry
)
{
const
metadataDiagnostics
=
isClientEntry
?
metadata
.
getSemanticDiagnosticsForExportVariableStatementInClientEntry
(
fileName
,
node
)
:
metadata
.
getSemanticDiagnosticsForExportVariableStatement
(
fileName
,
node
)
prior
.
push
(...
metadataDiagnostics
)
}
// export function ...
// export function ...
if
(
isClientEntry
)
{
if
(
isClientEntry
)
{
prior
.
push
(
prior
.
push
(
...
...
packages/next/src/server/typescript/rules/metadata.ts
View file @
94ffcba2
...
@@ -2,6 +2,7 @@ import { NEXT_TS_ERRORS } from '../constant'
...
@@ -2,6 +2,7 @@ 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'
const
TYPE_ANOTATION_ASYNC
=
': Promise<Metadata> | Metadata'
const
TYPE_IMPORT
=
`import type { Metadata } from 'next'`
const
TYPE_IMPORT
=
`import type { Metadata } from 'next'`
// Find the `export const metadata = ...` node.
// Find the `export const metadata = ...` node.
...
@@ -136,26 +137,42 @@ function getProxiedLanguageService() {
...
@@ -136,26 +137,42 @@ function getProxiedLanguageService() {
function
updateVirtualFileWithType
(
function
updateVirtualFileWithType
(
fileName
:
string
,
fileName
:
string
,
node
:
ts
.
VariableDeclaration
node
:
ts
.
VariableDeclaration
|
ts
.
FunctionDeclaration
,
isGenerateMetadata
?:
boolean
)
{
)
{
const
source
=
getSource
(
fileName
)
const
source
=
getSource
(
fileName
)
if
(
!
source
)
return
if
(
!
source
)
return
// We annotate with the type in a vritual language service
// We annotate with the type in a vritual language service
const
sourceText
=
source
.
getFullText
()
const
sourceText
=
source
.
getFullText
()
const
nodeEnd
=
node
.
name
.
getFullStart
()
+
node
.
name
.
getFullWidth
()
let
nodeEnd
:
number
let
annotation
:
string
const
ts
=
getTs
()
if
(
ts
.
isFunctionDeclaration
(
node
))
{
if
(
isGenerateMetadata
)
{
nodeEnd
=
node
.
body
!
.
getFullStart
()
annotation
=
TYPE_ANOTATION_ASYNC
}
else
{
return
}
}
else
{
nodeEnd
=
node
.
name
.
getFullStart
()
+
node
.
name
.
getFullWidth
()
annotation
=
TYPE_ANOTATION
}
const
newSource
=
const
newSource
=
sourceText
.
slice
(
0
,
nodeEnd
)
+
sourceText
.
slice
(
0
,
nodeEnd
)
+
TYPE_ANOTATION
+
annotation
+
sourceText
.
slice
(
nodeEnd
)
+
sourceText
.
slice
(
nodeEnd
)
+
TYPE_IMPORT
TYPE_IMPORT
const
{
languageServiceHost
}
=
getProxiedLanguageService
()
const
{
languageServiceHost
}
=
getProxiedLanguageService
()
languageServiceHost
.
addFile
(
fileName
,
newSource
)
languageServiceHost
.
addFile
(
fileName
,
newSource
)
return
nodeEnd
return
[
nodeEnd
,
annotation
.
length
]
}
}
function
isTyped
(
node
:
ts
.
VariableDeclaration
)
{
function
isTyped
(
node
:
ts
.
VariableDeclaration
|
ts
.
FunctionDeclaration
)
{
return
node
.
type
!==
undefined
return
node
.
type
!==
undefined
}
}
...
@@ -173,13 +190,12 @@ const metadata = {
...
@@ -173,13 +190,12 @@ const metadata = {
const
ts
=
getTs
()
const
ts
=
getTs
()
// We annotate with the type in a vritual language service
// We annotate with the type in a vritual language service
const
nodeEnd
=
updateVirtualFileWithType
(
fileName
,
node
)
const
pos
=
updateVirtualFileWithType
(
fileName
,
node
)
if
(
nodeEnd
===
undefined
)
return
prior
if
(
pos
===
undefined
)
return
prior
// Get completions
// Get completions
const
{
languageService
}
=
getProxiedLanguageService
()
const
{
languageService
}
=
getProxiedLanguageService
()
const
newPos
=
const
newPos
=
position
<=
pos
[
0
]
?
position
:
position
+
pos
[
1
]
position
<=
nodeEnd
?
position
:
position
+
TYPE_ANOTATION
.
length
const
completions
=
languageService
.
getCompletionsAtPosition
(
const
completions
=
languageService
.
getCompletionsAtPosition
(
fileName
,
fileName
,
newPos
,
newPos
,
...
@@ -225,75 +241,105 @@ const metadata = {
...
@@ -225,75 +241,105 @@ const metadata = {
getSemanticDiagnosticsForExportVariableStatementInClientEntry
(
getSemanticDiagnosticsForExportVariableStatementInClientEntry
(
fileName
:
string
,
fileName
:
string
,
node
:
ts
.
VariableStatement
node
:
ts
.
VariableStatement
|
ts
.
FunctionDeclaration
)
{
)
{
const
source
=
getSource
(
fileName
)
const
source
=
getSource
(
fileName
)
const
ts
=
getTs
()
const
ts
=
getTs
()
for
(
const
declaration
of
node
.
declarationList
.
declarations
)
{
// It is not allowed to export `metadata` or `generateMetadata` in client entry
const
name
=
declaration
.
name
.
getText
()
if
(
ts
.
isFunctionDeclaration
(
node
))
{
if
([
'metadata'
,
'generateMetadata'
].
includes
(
name
))
{
if
(
node
.
name
?.
getText
()
===
'generateMetadata'
)
{
// It is not allowed to use `metadata` or `generateMetadata` in client entry
return
[
return
[
{
{
file
:
source
,
file
:
source
,
category
:
ts
.
DiagnosticCategory
.
Error
,
category
:
ts
.
DiagnosticCategory
.
Error
,
code
:
NEXT_TS_ERRORS
.
INVALID_METADATA_EXPORT
,
code
:
NEXT_TS_ERRORS
.
INVALID_METADATA_EXPORT
,
messageText
:
`The Next.js '
${
name
}
' API is not allowed in a client component.`
,
messageText
:
`The Next.js '
generateMetadata
' API is not allowed in a client component.`
,
start
:
declaration
.
name
.
getStart
(),
start
:
node
.
name
.
getStart
(),
length
:
declaration
.
name
.
getWidth
(),
length
:
node
.
name
.
getWidth
(),
},
},
]
]
}
}
}
else
{
for
(
const
declaration
of
node
.
declarationList
.
declarations
)
{
const
name
=
declaration
.
name
.
getText
()
if
(
name
===
'metadata'
)
{
return
[
{
file
:
source
,
category
:
ts
.
DiagnosticCategory
.
Error
,
code
:
NEXT_TS_ERRORS
.
INVALID_METADATA_EXPORT
,
messageText
:
`The Next.js 'metadata' API is not allowed in a client component.`
,
start
:
declaration
.
name
.
getStart
(),
length
:
declaration
.
name
.
getWidth
(),
},
]
}
}
}
}
return
[]
return
[]
},
},
getSemanticDiagnosticsForExportVariableStatement
(
getSemanticDiagnosticsForExportVariableStatement
(
fileName
:
string
,
fileName
:
string
,
node
:
ts
.
VariableStatement
node
:
ts
.
VariableStatement
|
ts
.
FunctionDeclaration
)
{
)
{
const
source
=
getSource
(
fileName
)
const
source
=
getSource
(
fileName
)
const
ts
=
getTs
()
for
(
const
declaration
of
node
.
declarationList
.
declarations
)
{
function
proxyDiagnostics
(
if
(
declaration
.
name
.
getText
()
===
'metadata'
)
{
pos
:
number
[],
if
(
isTyped
(
declaration
))
break
n
:
ts
.
VariableDeclaration
|
ts
.
FunctionDeclaration
)
{
// Get diagnostics
const
{
languageService
}
=
getProxiedLanguageService
()
const
diagnostics
=
languageService
.
getSemanticDiagnostics
(
fileName
)
// Filter and map the results
return
diagnostics
.
filter
((
d
)
=>
{
if
(
d
.
start
===
undefined
||
d
.
length
===
undefined
)
return
false
if
(
d
.
start
<
n
.
getFullStart
())
return
false
if
(
d
.
start
+
d
.
length
>=
n
.
getFullStart
()
+
n
.
getFullWidth
()
+
pos
[
1
]
)
return
false
return
true
})
.
map
((
d
)
=>
{
return
{
file
:
source
,
category
:
d
.
category
,
code
:
d
.
code
,
messageText
:
d
.
messageText
,
start
:
d
.
start
!
<
pos
[
0
]
?
d
.
start
:
d
.
start
!
-
pos
[
1
],
length
:
d
.
length
,
}
})
}
if
(
ts
.
isFunctionDeclaration
(
node
))
{
if
(
node
.
name
?.
getText
()
===
'generateMetadata'
)
{
if
(
isTyped
(
node
))
return
[]
// We annotate with the type in a vritual language service
// We annotate with the type in a vritual language service
const
nodeEnd
=
updateVirtualFileWithType
(
fileName
,
declaration
)
const
pos
=
updateVirtualFileWithType
(
fileName
,
node
,
true
)
if
(
!
nodeEnd
)
break
if
(
!
pos
)
return
[]
// Get diagnostics
return
proxyDiagnostics
(
pos
,
node
)
const
{
languageService
}
=
getProxiedLanguageService
()
}
const
diagnostics
=
languageService
.
getSemanticDiagnostics
(
fileName
)
}
else
{
for
(
const
declaration
of
node
.
declarationList
.
declarations
)
{
// Filter and map the results
if
(
declaration
.
name
.
getText
()
===
'metadata'
)
{
return
diagnostics
if
(
isTyped
(
declaration
))
break
.
filter
((
d
)
=>
{
if
(
d
.
start
===
undefined
||
d
.
length
===
undefined
)
return
false
// We annotate with the type in a vritual language service
if
(
d
.
start
<
declaration
.
getFullStart
())
return
false
const
pos
=
updateVirtualFileWithType
(
fileName
,
declaration
)
if
(
if
(
!
pos
)
break
d
.
start
+
d
.
length
>
declaration
.
getFullStart
()
+
return
proxyDiagnostics
(
pos
,
declaration
)
declaration
.
getFullWidth
()
+
}
TYPE_ANOTATION
.
length
)
return
false
return
true
})
.
map
((
d
)
=>
{
return
{
file
:
source
,
category
:
d
.
category
,
code
:
d
.
code
,
messageText
:
d
.
messageText
,
start
:
d
.
start
!
<=
nodeEnd
?
d
.
start
:
d
.
start
!
-
TYPE_ANOTATION
.
length
,
length
:
d
.
length
,
}
})
}
}
}
}
return
[]
return
[]
...
@@ -313,12 +359,11 @@ const metadata = {
...
@@ -313,12 +359,11 @@ const metadata = {
if
(
isTyped
(
node
))
return
if
(
isTyped
(
node
))
return
// We annotate with the type in a vritual language service
// We annotate with the type in a vritual language service
const
nodeEnd
=
updateVirtualFileWithType
(
fileName
,
node
)
const
pos
=
updateVirtualFileWithType
(
fileName
,
node
)
if
(
nodeEnd
===
undefined
)
return
if
(
pos
===
undefined
)
return
const
{
languageService
}
=
getProxiedLanguageService
()
const
{
languageService
}
=
getProxiedLanguageService
()
const
newPos
=
const
newPos
=
position
<=
pos
[
0
]
?
position
:
position
+
pos
[
1
]
position
<=
nodeEnd
?
position
:
position
+
TYPE_ANOTATION
.
length
const
details
=
languageService
.
getCompletionEntryDetails
(
const
details
=
languageService
.
getCompletionEntryDetails
(
fileName
,
fileName
,
...
@@ -338,12 +383,11 @@ const metadata = {
...
@@ -338,12 +383,11 @@ const metadata = {
if
(
isTyped
(
node
))
return
if
(
isTyped
(
node
))
return
// We annotate with the type in a vritual language service
// We annotate with the type in a vritual language service
const
nodeEnd
=
updateVirtualFileWithType
(
fileName
,
node
)
const
pos
=
updateVirtualFileWithType
(
fileName
,
node
)
if
(
nodeEnd
===
undefined
)
return
if
(
pos
===
undefined
)
return
const
{
languageService
}
=
getProxiedLanguageService
()
const
{
languageService
}
=
getProxiedLanguageService
()
const
newPos
=
const
newPos
=
position
<=
pos
[
0
]
?
position
:
position
+
pos
[
1
]
position
<=
nodeEnd
?
position
:
position
+
TYPE_ANOTATION
.
length
const
insight
=
languageService
.
getQuickInfoAtPosition
(
fileName
,
newPos
)
const
insight
=
languageService
.
getQuickInfoAtPosition
(
fileName
,
newPos
)
return
insight
return
insight
},
},
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment