|
| 1 | +import { TextDocument } from '@volar/language-server'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { URI } from 'vscode-uri'; |
| 4 | +import { getLanguageServer, testWorkspacePath } from './server.js'; |
| 5 | + |
| 6 | +describe('Definitions', async () => { |
| 7 | + |
| 8 | + it('Inline handler leading', async () => { |
| 9 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 10 | + await assertInlayHints('tsconfigProject/fixture.vue', 'vue', ` |
| 11 | + <script setup lang="ts"> |
| 12 | + let a = 0; |
| 13 | + </script> |
| 14 | +
|
| 15 | + <template> |
| 16 | + <div @click="a = 1"></div> |
| 17 | + </template> |
| 18 | + `); |
| 19 | + }); |
| 20 | + |
| 21 | + it('Missing props', async () => { |
| 22 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 23 | + openDocument('tsconfigProject/foo.vue', 'vue', ` |
| 24 | + <script setup lang="ts"> |
| 25 | + defineProps<{ |
| 26 | + foo: number; |
| 27 | + }>(); |
| 28 | + </script> |
| 29 | + `); |
| 30 | + await assertInlayHints('tsconfigProject/fixture.vue', 'vue', ` |
| 31 | + <script setup lang="ts"> |
| 32 | + import Foo from './foo.vue'; |
| 33 | + </script> |
| 34 | +
|
| 35 | + <template> |
| 36 | + <Foo></Foo> |
| 37 | + </template> |
| 38 | + `); |
| 39 | + }); |
| 40 | + |
| 41 | + it('Options wrapper', async () => { |
| 42 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 43 | + await assertInlayHints('tsconfigProject/fixture.vue', 'vue', ` |
| 44 | + <script> |
| 45 | + export default {}; |
| 46 | + </script> |
| 47 | + `); |
| 48 | + }); |
| 49 | + |
| 50 | + it('Destructured props', async () => { |
| 51 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 52 | + await assertInlayHints('tsconfigProject/fixture.vue', 'vue', ` |
| 53 | + <script setup lang="ts"> |
| 54 | + import { watch } from 'vue'; |
| 55 | +
|
| 56 | + const { foo, bar, ...props } = defineProps<{ |
| 57 | + foo: string; |
| 58 | + bar: string; |
| 59 | + baz: string; |
| 60 | + }>(); |
| 61 | +
|
| 62 | + type foo = foo[string] & typeof foo; |
| 63 | +
|
| 64 | + interface foo extends (typeof foo) { |
| 65 | + foo: string; |
| 66 | + foo(foo: string): void; |
| 67 | + foo: (foo: string) => void; |
| 68 | + } |
| 69 | +
|
| 70 | + const obj = { |
| 71 | + foo: foo, |
| 72 | + [foo]: '', |
| 73 | + foo, |
| 74 | + foo(foo) { }, |
| 75 | + foo: function (foo) { }, |
| 76 | + get bar() { return this.foo; }, |
| 77 | + set bar(val) { this.foo = val; } |
| 78 | + }; |
| 79 | +
|
| 80 | + function func(foo) { } |
| 81 | +
|
| 82 | + class cls { |
| 83 | + foo: string = foo; |
| 84 | + constructor(foo) { } |
| 85 | + } |
| 86 | +
|
| 87 | + for (const char of foo) { } |
| 88 | +
|
| 89 | + try { } catch (foo) { } |
| 90 | +
|
| 91 | + watch(() => foo, (foo) => { |
| 92 | + console.log(foo, bar, props.baz); |
| 93 | + }); |
| 94 | + </script> |
| 95 | + `); |
| 96 | + }); |
| 97 | + |
| 98 | + const openedDocuments: TextDocument[] = []; |
| 99 | + |
| 100 | + afterEach(async () => { |
| 101 | + const server = await getLanguageServer(); |
| 102 | + for (const document of openedDocuments) { |
| 103 | + await server.closeTextDocument(document.uri); |
| 104 | + } |
| 105 | + openedDocuments.length = 0; |
| 106 | + }); |
| 107 | + |
| 108 | + /** |
| 109 | + * @deprecated Remove this when #4717 fixed. |
| 110 | + */ |
| 111 | + async function ensureGlobalTypesHolder(folderName: string) { |
| 112 | + const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', ''); |
| 113 | + const server = await getLanguageServer(); |
| 114 | + await server.sendDocumentDiagnosticRequest(document.uri); |
| 115 | + } |
| 116 | + |
| 117 | + async function assertInlayHints(fileName: string, languageId: string, content: string) { |
| 118 | + const server = await getLanguageServer(); |
| 119 | + let document = await openDocument(fileName, languageId, content); |
| 120 | + |
| 121 | + const inlayHints = await server.sendInlayHintRequest(document.uri, { start: document.positionAt(0), end: document.positionAt(content.length) }); |
| 122 | + expect(inlayHints).toBeDefined(); |
| 123 | + expect(inlayHints!.length).greaterThan(0); |
| 124 | + |
| 125 | + let text = document.getText(); |
| 126 | + for (const hint of inlayHints!.sort((a, b) => document.offsetAt(b.position) - document.offsetAt(a.position))) { |
| 127 | + const offset = document.offsetAt(hint.position); |
| 128 | + text = text.slice(0, offset) + '[' + hint.label + ']' + text.slice(offset); |
| 129 | + } |
| 130 | + |
| 131 | + expect(text).toMatchSnapshot(); |
| 132 | + } |
| 133 | + |
| 134 | + async function openDocument(fileName: string, languageId: string, content: string) { |
| 135 | + const server = await getLanguageServer(); |
| 136 | + const uri = URI.file(`${testWorkspacePath}/${fileName}`); |
| 137 | + const document = await server.openInMemoryDocument(uri.toString(), languageId, content); |
| 138 | + if (openedDocuments.every(d => d.uri !== document.uri)) { |
| 139 | + openedDocuments.push(document); |
| 140 | + } |
| 141 | + return document; |
| 142 | + } |
| 143 | +}); |
0 commit comments