|
| 1 | +import { Location, TextDocument } from '@volar/language-server'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 4 | +import { URI } from 'vscode-uri'; |
| 5 | +import { getLanguageServer, testWorkspacePath } from './server.js'; |
| 6 | + |
| 7 | +describe('Definitions', async () => { |
| 8 | + |
| 9 | + it('TS to vue', async () => { |
| 10 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 11 | + await assertDefinition('tsconfigProject/fixture1.ts', 'typescript', `import C|omponent from './empty.vue';`); |
| 12 | + await assertDefinition('tsconfigProject/fixture2.ts', 'typescript', `import Component from '|./empty.vue';`); |
| 13 | + }); |
| 14 | + |
| 15 | + it('Alias path', async () => { |
| 16 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 17 | + await openDocument('tsconfigProject/foo.ts', 'typescript', `export const foo = 'foo';`); |
| 18 | + await assertDefinition('tsconfigProject/fixture.vue', 'vue', ` |
| 19 | + <script setup lang="ts"> |
| 20 | + import { foo| } from '@/foo'; |
| 21 | + </script> |
| 22 | + `); |
| 23 | + }); |
| 24 | + |
| 25 | + it('#2600', async () => { |
| 26 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 27 | + await openDocument('tsconfigProject/foo.vue', 'vue', ` |
| 28 | + <template> |
| 29 | + <h1>{{ msg }}</h1> |
| 30 | + </template> |
| 31 | +
|
| 32 | + <script lang="ts"> |
| 33 | + export default defineProps<{ msg: string }>() |
| 34 | + </script> |
| 35 | + `); |
| 36 | + await assertDefinition('tsconfigProject/fixture.vue', 'vue', ` |
| 37 | + <script setup lang="ts"> |
| 38 | + import Foo from '|@/foo.vue'; |
| 39 | + </script> |
| 40 | + `); |
| 41 | + }); |
| 42 | + |
| 43 | + const openedDocuments: TextDocument[] = []; |
| 44 | + |
| 45 | + afterEach(async () => { |
| 46 | + const server = await getLanguageServer(); |
| 47 | + for (const document of openedDocuments) { |
| 48 | + await server.closeTextDocument(document.uri); |
| 49 | + } |
| 50 | + openedDocuments.length = 0; |
| 51 | + }); |
| 52 | + |
| 53 | + /** |
| 54 | + * @deprecated Remove this when #4717 fixed. |
| 55 | + */ |
| 56 | + async function ensureGlobalTypesHolder(folderName: string) { |
| 57 | + const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', ''); |
| 58 | + const server = await getLanguageServer(); |
| 59 | + await server.sendDocumentDiagnosticRequest(document.uri); |
| 60 | + } |
| 61 | + |
| 62 | + async function assertDefinition(fileName: string, languageId: string, content: string) { |
| 63 | + const offset = content.indexOf('|'); |
| 64 | + content = content.slice(0, offset) + content.slice(offset + 1); |
| 65 | + |
| 66 | + const server = await getLanguageServer(); |
| 67 | + let document = await openDocument(fileName, languageId, content); |
| 68 | + |
| 69 | + const position = document.positionAt(offset); |
| 70 | + const definition = await server.sendDefinitionRequest(document.uri, position) as Location[] | null; |
| 71 | + expect(definition).toBeDefined(); |
| 72 | + expect(definition!.length).greaterThan(0); |
| 73 | + |
| 74 | + for (const loc of definition!) { |
| 75 | + expect(path.relative(testWorkspacePath, URI.parse(loc.uri).fsPath)).toMatchSnapshot(); |
| 76 | + expect(loc.range).toMatchSnapshot(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + async function openDocument(fileName: string, languageId: string, content: string) { |
| 81 | + const server = await getLanguageServer(); |
| 82 | + const uri = URI.file(`${testWorkspacePath}/${fileName}`); |
| 83 | + const document = await server.openInMemoryDocument(uri.toString(), languageId, content); |
| 84 | + if (openedDocuments.every(d => d.uri !== document.uri)) { |
| 85 | + openedDocuments.push(document); |
| 86 | + } |
| 87 | + return document; |
| 88 | + } |
| 89 | +}); |
0 commit comments