|
| 1 | +import { TextDocument } from '@volar/language-server'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { URI } from 'vscode-uri'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { getLanguageServer, testWorkspacePath } from './server.js'; |
| 6 | + |
| 7 | +describe('Renaming', async () => { |
| 8 | + |
| 9 | + it('#2410', async () => { |
| 10 | + await assertRenaming('fixture.vue', 'vue', `<template><|h1></h1></template>`, 'h2'); |
| 11 | + await assertRenaming('fixture.vue', 'vue', `<template><h1|></h1></template>`, 'h2'); |
| 12 | + }); |
| 13 | + |
| 14 | + it('CSS', async () => { |
| 15 | + await assertRenaming('fixture.vue', 'vue', ` |
| 16 | + <template> |
| 17 | + <div :class="$style.foo|"></div> |
| 18 | + </template> |
| 19 | +
|
| 20 | + <style module> |
| 21 | + /* .foo { } */ |
| 22 | + .foo { } |
| 23 | + </style> |
| 24 | +
|
| 25 | + <style module lang="scss"> |
| 26 | + // .foo { } |
| 27 | + </style> |
| 28 | + `, 'bar'); |
| 29 | + await assertRenaming('fixture.vue', 'vue', ` |
| 30 | + <template> |
| 31 | + <div class="foo|"></div> |
| 32 | + </template> |
| 33 | +
|
| 34 | + <style scoped> |
| 35 | + .foo { } |
| 36 | + </style> |
| 37 | + `, 'bar'); |
| 38 | + await assertRenaming('fixture.vue', 'vue', ` |
| 39 | + <script lang="ts" setup> |
| 40 | + const foo = 1; |
| 41 | + </script> |
| 42 | +
|
| 43 | + <style> |
| 44 | + /* .bar { color: v-bind(foo); } */ |
| 45 | + .bar { color: v-bind(foo|); } |
| 46 | + .bar { color: v-bind('foo'); } |
| 47 | + .bar { color: v-bind("foo"); } |
| 48 | + .bar { color: v-bind(foo + foo); } |
| 49 | + .bar { color: v-bind('foo + foo'); } |
| 50 | + .bar { color: v-bind("foo + foo"); } |
| 51 | + .bar { color: v-bind(); } |
| 52 | + </style> |
| 53 | +
|
| 54 | + <style lang="scss"> |
| 55 | + // .bar { color: v-bind(foo); } |
| 56 | + </style> |
| 57 | + `, 'bar'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('Component props', async () => { |
| 61 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 62 | + await openDocument('tsconfigProject/foo.vue', 'vue', ` |
| 63 | + <template> |
| 64 | + <Comp :aaa-bbb="'foo'"></Comp> |
| 65 | + <Comp :aaaBbb="'foo'"></Comp> |
| 66 | + </template> |
| 67 | +
|
| 68 | + <script lang="ts" setup> |
| 69 | + import Comp from './fixture.vue'; |
| 70 | + </script> |
| 71 | + `); |
| 72 | + await assertRenaming('tsconfigProject/fixture.vue', 'vue', ` |
| 73 | + <template> |
| 74 | + {{ aaaBbb }} |
| 75 | + </template> |
| 76 | +
|
| 77 | + <script lang="ts" setup> |
| 78 | + defineProps({ aaaBbb|: String }); |
| 79 | + </script> |
| 80 | + `, 'cccDdd'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('Component type props', async () => { |
| 84 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 85 | + await openDocument('tsconfigProject/foo.vue', 'vue', ` |
| 86 | + <template> |
| 87 | + <Comp :aaa-bbb="'foo'"></Comp> |
| 88 | + <Comp :aaaBbb="'foo'"></Comp> |
| 89 | + </template> |
| 90 | +
|
| 91 | + <script lang="ts" setup> |
| 92 | + import Comp from './fixture.vue'; |
| 93 | + </script> |
| 94 | + `); |
| 95 | + await assertRenaming('tsconfigProject/fixture.vue', 'vue', ` |
| 96 | + <template> |
| 97 | + {{ aaaBbb }} |
| 98 | + </template> |
| 99 | +
|
| 100 | + <script lang="ts" setup> |
| 101 | + defineProps<{ aaaBbb|: String }>(); |
| 102 | + </script> |
| 103 | + `, 'cccDdd'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('Component dynamic props', async () => { |
| 107 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 108 | + await assertRenaming('tsconfigProject/fixture.vue', 'vue', ` |
| 109 | + <template> |
| 110 | + <div :[foo|]="123"></div> |
| 111 | + </template> |
| 112 | +
|
| 113 | + <script lang="ts" setup> |
| 114 | + const foo = 'foo'; |
| 115 | + </script> |
| 116 | + `, 'bar'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('Component returns', async () => { |
| 120 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 121 | + await assertRenaming('tsconfigProject/fixture.vue', 'vue', ` |
| 122 | + <template> |
| 123 | + {{ foo| }} |
| 124 | + </template> |
| 125 | +
|
| 126 | + <script lang="ts"> |
| 127 | + import { defineComponent } from 'vue'; |
| 128 | +
|
| 129 | + export default defineComponent({ |
| 130 | + setup() { |
| 131 | + return { |
| 132 | + foo: 1, |
| 133 | + }; |
| 134 | + }, |
| 135 | + }); |
| 136 | + </script> |
| 137 | + `, 'bar'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('<script setup>', async () => { |
| 141 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 142 | + await assertRenaming('tsconfigProject/fixture.vue', 'vue', ` |
| 143 | + <template> |
| 144 | + {{ foo| }} |
| 145 | + </template> |
| 146 | +
|
| 147 | + <script lang="ts" setup> |
| 148 | + const foo = 1; |
| 149 | + </script> |
| 150 | + `, 'bar'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('Component tags', async () => { |
| 154 | + await ensureGlobalTypesHolder('tsconfigProject'); |
| 155 | + await assertRenaming('tsconfigProject/fixture.vue', 'vue', ` |
| 156 | + <template> |
| 157 | + <AaBb></AaBb> |
| 158 | + <aa-bb></aa-bb> |
| 159 | + </template> |
| 160 | +
|
| 161 | + <script lang="ts" setup> |
| 162 | + import AaBb| from './empty.vue'; |
| 163 | + </script> |
| 164 | + `, 'CcDd'); |
| 165 | + }); |
| 166 | + |
| 167 | + const openedDocuments: TextDocument[] = []; |
| 168 | + |
| 169 | + afterEach(async () => { |
| 170 | + const server = await getLanguageServer(); |
| 171 | + for (const document of openedDocuments) { |
| 172 | + await server.closeTextDocument(document.uri); |
| 173 | + } |
| 174 | + openedDocuments.length = 0; |
| 175 | + }); |
| 176 | + |
| 177 | + /** |
| 178 | + * @deprecated Remove this when #4717 fixed. |
| 179 | + */ |
| 180 | + async function ensureGlobalTypesHolder(folderName: string) { |
| 181 | + const document = await openDocument(`${folderName}/globalTypesHolder.vue`, 'vue', ''); |
| 182 | + const server = await getLanguageServer(); |
| 183 | + await server.sendDocumentDiagnosticRequest(document.uri); |
| 184 | + } |
| 185 | + |
| 186 | + async function assertRenaming(fileName: string, languageId: string, _content: string, newName: string) { |
| 187 | + const offset = _content.indexOf('|'); |
| 188 | + expect(offset).toBeGreaterThanOrEqual(0); |
| 189 | + const content = _content.slice(0, offset) + _content.slice(offset + 1); |
| 190 | + |
| 191 | + const server = await getLanguageServer(); |
| 192 | + let document = await openDocument(fileName, languageId, content); |
| 193 | + |
| 194 | + const position = document.positionAt(offset); |
| 195 | + const edit = await server.sendRenameRequest(document.uri, position, newName); |
| 196 | + expect(edit).toBeDefined(); |
| 197 | + expect(edit?.changes).toBeDefined(); |
| 198 | + |
| 199 | + for (const [uri, edits] of Object.entries(edit!.changes!)) { |
| 200 | + expect(path.relative(testWorkspacePath, URI.parse(uri).fsPath)).toMatchSnapshot(); |
| 201 | + expect(edits).toMatchSnapshot(); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + async function openDocument(fileName: string, languageId: string, content: string) { |
| 206 | + const server = await getLanguageServer(); |
| 207 | + const uri = URI.file(`${testWorkspacePath}/${fileName}`); |
| 208 | + const document = await server.openInMemoryDocument(uri.toString(), languageId, content); |
| 209 | + if (openedDocuments.every(d => d.uri !== document.uri)) { |
| 210 | + openedDocuments.push(document); |
| 211 | + } |
| 212 | + return document; |
| 213 | + } |
| 214 | +}); |
0 commit comments