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

Forward type definition with the TS plugin (#45764)

Improve the experience that when you have type errors, you can use
CMD+Click to directly jump to the metadata definition:


https://user-images.githubusercontent.com/3676859/217918427-590d1feb-1d05-4b15-8146-0b7aa35fab0b.mp4

## 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 e8e85eff
...@@ -285,6 +285,19 @@ export function createTSPlugin(modules: { ...@@ -285,6 +285,19 @@ export function createTSPlugin(modules: {
return prior return prior
} }
// Get definition and link for specific node
proxy.getDefinitionAndBoundSpan = (fileName: string, position: number) => {
if (isAppEntryFile(fileName) && !getIsClientEntry(fileName)) {
const metadataDefinition = metadata.getDefinitionAndBoundSpan(
fileName,
position
)
if (metadataDefinition) return metadataDefinition
}
return info.languageService.getDefinitionAndBoundSpan(fileName, position)
}
return proxy return proxy
} }
......
...@@ -391,6 +391,29 @@ const metadata = { ...@@ -391,6 +391,29 @@ const metadata = {
const insight = languageService.getQuickInfoAtPosition(fileName, newPos) const insight = languageService.getQuickInfoAtPosition(fileName, newPos)
return insight return insight
}, },
getDefinitionAndBoundSpan(fileName: string, position: number) {
const node = getMetadataExport(fileName, position)
if (!node) return
if (isTyped(node)) return
if (!isPositionInsideNode(position, node)) return
// We annotate with the type in a vritual language service
const pos = updateVirtualFileWithType(fileName, node)
if (pos === undefined) return
const { languageService } = getProxiedLanguageService()
const newPos = position <= pos[0] ? position : position + pos[1]
const definitionInfoAndBoundSpan =
languageService.getDefinitionAndBoundSpan(fileName, newPos)
if (definitionInfoAndBoundSpan) {
// Adjust the start position of the text span
if (definitionInfoAndBoundSpan.textSpan.start > pos[0]) {
definitionInfoAndBoundSpan.textSpan.start -= pos[1]
}
}
return definitionInfoAndBoundSpan
},
} }
export default metadata export default metadata
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