Skip to content

Commit 9949a3a

Browse files
authored
(fix) fix event completion with createEventDispatcher or with multiple declarations of the same event (#1083)
1 parent 735cb1e commit 9949a3a

File tree

6 files changed

+101
-14
lines changed

6 files changed

+101
-14
lines changed

packages/language-server/src/plugins/typescript/ComponentInfoProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
5757
return type
5858
.getProperties()
5959
.map((prop) => {
60-
if (!prop.valueDeclaration) {
60+
// type would still be correct when there're multiple declarations
61+
const declaration = prop.valueDeclaration ?? prop.declarations?.[0];
62+
if (!declaration) {
6163
return;
6264
}
6365

6466
return {
6567
name: prop.name,
6668
type: this.typeChecker.typeToString(
67-
this.typeChecker.getTypeOfSymbolAtLocation(prop, prop.valueDeclaration)
69+
this.typeChecker.getTypeOfSymbolAtLocation(prop, declaration)
6870
),
6971
doc: ts.displayPartsToString(prop.getDocumentationComment(this.typeChecker))
7072
};

packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('CompletionProviderImpl', () => {
111111

112112
const completions = await completionProvider.getCompletions(
113113
document,
114-
Position.create(4, 5),
114+
Position.create(5, 5),
115115
{
116116
triggerKind: CompletionTriggerKind.Invoked
117117
}
@@ -158,7 +158,7 @@ describe('CompletionProviderImpl', () => {
158158

159159
const completions = await completionProvider.getCompletions(
160160
document,
161-
Position.create(4, 10),
161+
Position.create(5, 10),
162162
{
163163
triggerKind: CompletionTriggerKind.Invoked
164164
}
@@ -183,11 +183,11 @@ describe('CompletionProviderImpl', () => {
183183
newText: 'on:a',
184184
range: {
185185
start: {
186-
line: 4,
186+
line: 5,
187187
character: 7
188188
},
189189
end: {
190-
line: 4,
190+
line: 5,
191191
character: 10
192192
}
193193
}
@@ -205,11 +205,11 @@ describe('CompletionProviderImpl', () => {
205205
newText: 'on:b',
206206
range: {
207207
start: {
208-
line: 4,
208+
line: 5,
209209
character: 7
210210
},
211211
end: {
212-
line: 4,
212+
line: 5,
213213
character: 10
214214
}
215215
}
@@ -224,11 +224,11 @@ describe('CompletionProviderImpl', () => {
224224
newText: 'on:c',
225225
range: {
226226
start: {
227-
line: 4,
227+
line: 5,
228228
character: 7
229229
},
230230
end: {
231-
line: 4,
231+
line: 5,
232232
character: 10
233233
}
234234
}
@@ -237,6 +237,33 @@ describe('CompletionProviderImpl', () => {
237237
]);
238238
});
239239

240+
it('provides event completions from createEventDispatcher', async () => {
241+
const { completionProvider, document } = setup('component-events-completion.svelte');
242+
243+
const completions = await completionProvider.getCompletions(
244+
document,
245+
Position.create(6, 5),
246+
{
247+
triggerKind: CompletionTriggerKind.Invoked
248+
}
249+
);
250+
251+
const eventCompletions = completions!.items.filter((item) => item.label.startsWith('on:'));
252+
253+
assert.deepStrictEqual(eventCompletions, <CompletionItem[]>[
254+
{
255+
detail: 'c: CustomEvent<boolean>',
256+
documentation: {
257+
kind: 'markdown',
258+
value: 'abc'
259+
},
260+
label: 'on:c',
261+
sortText: '-1',
262+
textEdit: undefined
263+
}
264+
]);
265+
});
266+
240267
it('provides event completion for components with type definition', async () => {
241268
const { completionProvider, document } = setup('component-events-completion-ts-def.svelte');
242269

@@ -295,6 +322,42 @@ describe('CompletionProviderImpl', () => {
295322
]);
296323
});
297324

325+
it('provides event completion for components with type definition having multiple declarations of the same event', async () => {
326+
const { completionProvider, document } = setup('component-events-completion-ts-def.svelte');
327+
328+
const completions = await completionProvider.getCompletions(
329+
document,
330+
Position.create(6, 17),
331+
{
332+
triggerKind: CompletionTriggerKind.Invoked
333+
}
334+
);
335+
336+
const eventCompletions = completions!.items.filter((item) => item.label.startsWith('on:'));
337+
338+
assert.deepStrictEqual(eventCompletions, <CompletionItem[]>[
339+
{
340+
detail: 'event1: CustomEvent<string> | CustomEvent<number>',
341+
label: 'on:event1',
342+
sortText: '-1',
343+
documentation: '',
344+
textEdit: {
345+
newText: 'on:event1',
346+
range: {
347+
end: {
348+
character: 18,
349+
line: 6
350+
},
351+
start: {
352+
character: 15,
353+
line: 6
354+
}
355+
}
356+
}
357+
}
358+
]);
359+
});
360+
298361
it('does not provide completions inside style tag', async () => {
299362
const { completionProvider, document } = setup('completionsstyle.svelte');
300363

packages/language-server/test/plugins/typescript/testfiles/completions/ComponentDef.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ export class ComponentDef extends SvelteComponentTyped<
1717
* documentation for let2
1818
*/
1919
let2: string;
20-
}
20+
};
2121
}
2222
> {}
23+
24+
export class ComponentDef2 extends SvelteComponentTyped<
25+
{},
26+
| {
27+
event1: CustomEvent<number>;
28+
}
29+
| {
30+
event1: CustomEvent<string>;
31+
},
32+
{}
33+
> {}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
2-
import { ComponentDef } from './ComponentDef';
2+
import { ComponentDef, ComponentDef2 } from './ComponentDef';
33
</script>
44

55
<ComponentDef on:></ComponentDef>
6-
<ComponentDef let:></ComponentDef>
6+
<ComponentDef let:></ComponentDef>
7+
<ComponentDef2 on:></ComponentDef2>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script lang="ts">
22
import CEI from './component-events-interface.svelte';
3+
import CED from './component-events-event-dispatcher.svelte';
34
</script>
45

5-
<CEI on: />
6+
<CEI on: />
7+
<CED on: />
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
import { createEventDispatcher } from 'svelte';
3+
4+
const dispatch = createEventDispatcher<{
5+
/**abc*/
6+
c: boolean
7+
}>()
8+
</script>

0 commit comments

Comments
 (0)