|
| 1 | +import type { TextDocument } from '@volar/language-server'; |
| 2 | +import * as fs from 'node:fs'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import { afterEach, expect, test } from 'vitest'; |
| 5 | +import { URI } from 'vscode-uri'; |
| 6 | +import { getLanguageServer, testWorkspacePath } from './server.js'; |
| 7 | + |
| 8 | +test('#5818 clears missing module error after renaming to an existing filename', async () => { |
| 9 | + const server = await getLanguageServer(); |
| 10 | + const workspaceDir = path.join(testWorkspacePath, 'tsconfigProject'); |
| 11 | + const mainPath = path.join(workspaceDir, 'module-rename-main.vue'); |
| 12 | + const oldComponentPath = path.join(workspaceDir, 'module-rename-comp.vue'); |
| 13 | + const newComponentPath = path.join(workspaceDir, 'module-rename-comp-renamed.vue'); |
| 14 | + |
| 15 | + createFile(oldComponentPath, '<template>Comp</template>'); |
| 16 | + const mainContent = ` |
| 17 | +<script setup lang="ts"> |
| 18 | +import Comp from './module-rename-comp-renamed.vue' |
| 19 | +</script> |
| 20 | + `; |
| 21 | + const document = await openDocument(server, mainPath, mainContent); |
| 22 | + |
| 23 | + const diagnosticsBefore = await getSemanticDiagnostics(server, document.uri); |
| 24 | + expect(diagnosticsBefore.some(diagnostic => diagnostic.code === 2307)).toBe(true); |
| 25 | + |
| 26 | + fs.renameSync(oldComponentPath, newComponentPath); |
| 27 | + createdFiles.push(newComponentPath); |
| 28 | + |
| 29 | + await expect.poll( |
| 30 | + async () => { |
| 31 | + const diagnosticsAfter = await getSemanticDiagnostics(server, document.uri); |
| 32 | + return diagnosticsAfter.some(diagnostic => diagnostic.code === 2307); |
| 33 | + }, |
| 34 | + { |
| 35 | + interval: 100, |
| 36 | + timeout: 5000, |
| 37 | + }, |
| 38 | + ).toBe(false); |
| 39 | +}); |
| 40 | + |
| 41 | +const openedDocuments: TextDocument[] = []; |
| 42 | +const createdFiles: string[] = []; |
| 43 | + |
| 44 | +afterEach(async () => { |
| 45 | + const server = await getLanguageServer(); |
| 46 | + for (const document of openedDocuments) { |
| 47 | + await server.close(document.uri); |
| 48 | + } |
| 49 | + for (const file of createdFiles) { |
| 50 | + if (fs.existsSync(file)) { |
| 51 | + fs.rmSync(file); |
| 52 | + } |
| 53 | + } |
| 54 | + openedDocuments.length = 0; |
| 55 | + createdFiles.length = 0; |
| 56 | +}); |
| 57 | + |
| 58 | +function createFile(fileName: string, content: string) { |
| 59 | + fs.writeFileSync(fileName, content); |
| 60 | + createdFiles.push(fileName); |
| 61 | +} |
| 62 | + |
| 63 | +async function openDocument(server: Awaited<ReturnType<typeof getLanguageServer>>, fileName: string, content: string) { |
| 64 | + createFile(fileName, content); |
| 65 | + const uri = URI.file(fileName); |
| 66 | + const document = await server.open(uri.toString(), 'vue', content); |
| 67 | + if (openedDocuments.every(doc => doc.uri !== document.uri)) { |
| 68 | + openedDocuments.push(document); |
| 69 | + } |
| 70 | + return document; |
| 71 | +} |
| 72 | + |
| 73 | +async function getSemanticDiagnostics(server: Awaited<ReturnType<typeof getLanguageServer>>, uri: string) { |
| 74 | + const res = await server.tsserver.message({ |
| 75 | + seq: server.nextSeq(), |
| 76 | + command: 'semanticDiagnosticsSync', |
| 77 | + arguments: { |
| 78 | + file: URI.parse(uri).fsPath, |
| 79 | + }, |
| 80 | + }); |
| 81 | + expect(res.success).toBe(true); |
| 82 | + return res.body as any[]; |
| 83 | +} |
0 commit comments