|
| 1 | +import { join } from 'path' |
1 | 2 | import { GetConfig } from './types'
|
| 3 | +import { findChildContainingExactPosition } from './utils' |
2 | 4 |
|
3 | 5 | export default (proxy: ts.LanguageService, info: ts.server.PluginCreateInfo, c: GetConfig) => {
|
4 | 6 | proxy.getDefinitionAndBoundSpan = (fileName, position) => {
|
5 | 7 | const prior = info.languageService.getDefinitionAndBoundSpan(fileName, position)
|
6 |
| - if (!prior) return |
| 8 | + if (!prior) { |
| 9 | + if (c('enableFileDefinitions')) { |
| 10 | + const sourceFile = info.languageService.getProgram()!.getSourceFile(fileName)! |
| 11 | + const node = findChildContainingExactPosition(sourceFile, position) |
| 12 | + if (node && ts.isStringLiteral(node) && ['./', '../'].some(str => node.text.startsWith(str))) { |
| 13 | + const file = join(fileName, '..', node.text) |
| 14 | + if (info.languageServiceHost.fileExists?.(file)) { |
| 15 | + const start = node.pos + node.getLeadingTriviaWidth() + 1 // + 1 for quote |
| 16 | + const textSpan = { |
| 17 | + start, |
| 18 | + length: node.end - start - 1, |
| 19 | + } |
| 20 | + return { |
| 21 | + textSpan, |
| 22 | + definitions: [ |
| 23 | + { |
| 24 | + containerKind: undefined as any, |
| 25 | + containerName: '', |
| 26 | + name: '', |
| 27 | + fileName: file, |
| 28 | + textSpan: { start: 0, length: 0 }, |
| 29 | + kind: ts.ScriptElementKind.moduleElement, |
| 30 | + contextSpan: { start: 0, length: 0 }, |
| 31 | + }, |
| 32 | + ], |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return |
| 38 | + } |
7 | 39 | if (__WEB__) {
|
8 | 40 | // let extension handle it
|
9 | 41 | // TODO failedAliasResolution
|
@@ -31,7 +63,18 @@ export default (proxy: ts.LanguageService, info: ts.server.PluginCreateInfo, c:
|
31 | 63 | const isFcDef = fileName.endsWith('node_modules/@types/react/index.d.ts') && containerName === 'FunctionComponent'
|
32 | 64 | return !isFcDef
|
33 | 65 | })
|
| 66 | + // 11 |
34 | 67 | }
|
| 68 | + |
| 69 | + if ( |
| 70 | + c('removeModuleFileDefinitions') && |
| 71 | + prior.definitions?.length === 1 && |
| 72 | + firstDef.kind === ts.ScriptElementKind.moduleElement && |
| 73 | + firstDef.name.slice(1, -1).startsWith('*.') |
| 74 | + ) { |
| 75 | + return |
| 76 | + } |
| 77 | + |
35 | 78 | return prior
|
36 | 79 | }
|
37 | 80 | }
|
0 commit comments