Skip to content

Commit 405251e

Browse files
authored
fix(54013): bug: Incorrect Typescript completion with computed property name (#54067)
1 parent 4879087 commit 405251e

File tree

4 files changed

+401
-1
lines changed

4 files changed

+401
-1
lines changed

src/services/completions.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3585,7 +3585,8 @@ function getCompletionData(
35853585
const nameSymbol = leftMostName && typeChecker.getSymbolAtLocation(leftMostName);
35863586
// If this is nested like for `namespace N { export const sym = Symbol(); }`, we'll add the completion for `N`.
35873587
const firstAccessibleSymbol = nameSymbol && getFirstSymbolInChain(nameSymbol, contextToken, typeChecker);
3588-
if (firstAccessibleSymbol && addToSeen(seenPropertySymbols, getSymbolId(firstAccessibleSymbol))) {
3588+
const firstAccessibleSymbolId = firstAccessibleSymbol && getSymbolId(firstAccessibleSymbol);
3589+
if (firstAccessibleSymbolId && addToSeen(seenPropertySymbols, firstAccessibleSymbolId)) {
35893590
const index = symbols.length;
35903591
symbols.push(firstAccessibleSymbol);
35913592
const moduleSymbol = firstAccessibleSymbol.parent;
@@ -3621,6 +3622,9 @@ function getCompletionData(
36213622
}
36223623
}
36233624
else if (preferences.includeCompletionsWithInsertText) {
3625+
if (firstAccessibleSymbolId && seenPropertySymbols.has(firstAccessibleSymbolId)) {
3626+
return;
3627+
}
36243628
addSymbolOriginInfo(symbol);
36253629
addSymbolSortInfo(symbol);
36263630
symbols.push(symbol);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
=== /tests/cases/fourslash/completionsUniqueSymbol2.ts ===
2+
// const a = {
3+
// KEY_1: 'key_1',
4+
// KEY_2: 'key_2',
5+
// KEY_3: 'key_3',
6+
// } as const;
7+
//
8+
// const b = {
9+
// KEY_1: 'key_1',
10+
// KEY_2: 'key_2',
11+
// KEY_3: 'key_3',
12+
// } as const;
13+
//
14+
// interface I {
15+
// [b.KEY_1]: string,
16+
// [a.KEY_2]: string,
17+
// [a.KEY_3]: string
18+
// }
19+
//
20+
// const foo: I = {
21+
// key_1: 'value_1',
22+
// key_2: 'value_2',
23+
// key_3: 'value_3',
24+
// }
25+
//
26+
// foo.
27+
// ^
28+
// | ----------------------------------------------------------------------
29+
// | const a: {
30+
// | readonly KEY_1: "key_1";
31+
// | readonly KEY_2: "key_2";
32+
// | readonly KEY_3: "key_3";
33+
// | }
34+
// | const b: {
35+
// | readonly KEY_1: "key_1";
36+
// | readonly KEY_2: "key_2";
37+
// | readonly KEY_3: "key_3";
38+
// | }
39+
// | ----------------------------------------------------------------------
40+
41+
[
42+
{
43+
"marker": {
44+
"fileName": "/tests/cases/fourslash/completionsUniqueSymbol2.ts",
45+
"position": 345,
46+
"name": ""
47+
},
48+
"item": {
49+
"flags": 0,
50+
"isGlobalCompletion": false,
51+
"isMemberCompletion": true,
52+
"isNewIdentifierLocation": false,
53+
"entries": [
54+
{
55+
"name": "a",
56+
"kind": "const",
57+
"kindModifiers": "",
58+
"sortText": "11",
59+
"insertText": "[a]",
60+
"replacementSpan": {
61+
"start": 344,
62+
"length": 1
63+
},
64+
"displayParts": [
65+
{
66+
"text": "const",
67+
"kind": "keyword"
68+
},
69+
{
70+
"text": " ",
71+
"kind": "space"
72+
},
73+
{
74+
"text": "a",
75+
"kind": "localName"
76+
},
77+
{
78+
"text": ":",
79+
"kind": "punctuation"
80+
},
81+
{
82+
"text": " ",
83+
"kind": "space"
84+
},
85+
{
86+
"text": "{",
87+
"kind": "punctuation"
88+
},
89+
{
90+
"text": "\n",
91+
"kind": "lineBreak"
92+
},
93+
{
94+
"text": " ",
95+
"kind": "space"
96+
},
97+
{
98+
"text": "readonly",
99+
"kind": "keyword"
100+
},
101+
{
102+
"text": " ",
103+
"kind": "space"
104+
},
105+
{
106+
"text": "KEY_1",
107+
"kind": "propertyName"
108+
},
109+
{
110+
"text": ":",
111+
"kind": "punctuation"
112+
},
113+
{
114+
"text": " ",
115+
"kind": "space"
116+
},
117+
{
118+
"text": "\"key_1\"",
119+
"kind": "stringLiteral"
120+
},
121+
{
122+
"text": ";",
123+
"kind": "punctuation"
124+
},
125+
{
126+
"text": "\n",
127+
"kind": "lineBreak"
128+
},
129+
{
130+
"text": " ",
131+
"kind": "space"
132+
},
133+
{
134+
"text": "readonly",
135+
"kind": "keyword"
136+
},
137+
{
138+
"text": " ",
139+
"kind": "space"
140+
},
141+
{
142+
"text": "KEY_2",
143+
"kind": "propertyName"
144+
},
145+
{
146+
"text": ":",
147+
"kind": "punctuation"
148+
},
149+
{
150+
"text": " ",
151+
"kind": "space"
152+
},
153+
{
154+
"text": "\"key_2\"",
155+
"kind": "stringLiteral"
156+
},
157+
{
158+
"text": ";",
159+
"kind": "punctuation"
160+
},
161+
{
162+
"text": "\n",
163+
"kind": "lineBreak"
164+
},
165+
{
166+
"text": " ",
167+
"kind": "space"
168+
},
169+
{
170+
"text": "readonly",
171+
"kind": "keyword"
172+
},
173+
{
174+
"text": " ",
175+
"kind": "space"
176+
},
177+
{
178+
"text": "KEY_3",
179+
"kind": "propertyName"
180+
},
181+
{
182+
"text": ":",
183+
"kind": "punctuation"
184+
},
185+
{
186+
"text": " ",
187+
"kind": "space"
188+
},
189+
{
190+
"text": "\"key_3\"",
191+
"kind": "stringLiteral"
192+
},
193+
{
194+
"text": ";",
195+
"kind": "punctuation"
196+
},
197+
{
198+
"text": "\n",
199+
"kind": "lineBreak"
200+
},
201+
{
202+
"text": "}",
203+
"kind": "punctuation"
204+
}
205+
],
206+
"documentation": []
207+
},
208+
{
209+
"name": "b",
210+
"kind": "const",
211+
"kindModifiers": "",
212+
"sortText": "11",
213+
"insertText": "[b]",
214+
"replacementSpan": {
215+
"start": 344,
216+
"length": 1
217+
},
218+
"displayParts": [
219+
{
220+
"text": "const",
221+
"kind": "keyword"
222+
},
223+
{
224+
"text": " ",
225+
"kind": "space"
226+
},
227+
{
228+
"text": "b",
229+
"kind": "localName"
230+
},
231+
{
232+
"text": ":",
233+
"kind": "punctuation"
234+
},
235+
{
236+
"text": " ",
237+
"kind": "space"
238+
},
239+
{
240+
"text": "{",
241+
"kind": "punctuation"
242+
},
243+
{
244+
"text": "\n",
245+
"kind": "lineBreak"
246+
},
247+
{
248+
"text": " ",
249+
"kind": "space"
250+
},
251+
{
252+
"text": "readonly",
253+
"kind": "keyword"
254+
},
255+
{
256+
"text": " ",
257+
"kind": "space"
258+
},
259+
{
260+
"text": "KEY_1",
261+
"kind": "propertyName"
262+
},
263+
{
264+
"text": ":",
265+
"kind": "punctuation"
266+
},
267+
{
268+
"text": " ",
269+
"kind": "space"
270+
},
271+
{
272+
"text": "\"key_1\"",
273+
"kind": "stringLiteral"
274+
},
275+
{
276+
"text": ";",
277+
"kind": "punctuation"
278+
},
279+
{
280+
"text": "\n",
281+
"kind": "lineBreak"
282+
},
283+
{
284+
"text": " ",
285+
"kind": "space"
286+
},
287+
{
288+
"text": "readonly",
289+
"kind": "keyword"
290+
},
291+
{
292+
"text": " ",
293+
"kind": "space"
294+
},
295+
{
296+
"text": "KEY_2",
297+
"kind": "propertyName"
298+
},
299+
{
300+
"text": ":",
301+
"kind": "punctuation"
302+
},
303+
{
304+
"text": " ",
305+
"kind": "space"
306+
},
307+
{
308+
"text": "\"key_2\"",
309+
"kind": "stringLiteral"
310+
},
311+
{
312+
"text": ";",
313+
"kind": "punctuation"
314+
},
315+
{
316+
"text": "\n",
317+
"kind": "lineBreak"
318+
},
319+
{
320+
"text": " ",
321+
"kind": "space"
322+
},
323+
{
324+
"text": "readonly",
325+
"kind": "keyword"
326+
},
327+
{
328+
"text": " ",
329+
"kind": "space"
330+
},
331+
{
332+
"text": "KEY_3",
333+
"kind": "propertyName"
334+
},
335+
{
336+
"text": ":",
337+
"kind": "punctuation"
338+
},
339+
{
340+
"text": " ",
341+
"kind": "space"
342+
},
343+
{
344+
"text": "\"key_3\"",
345+
"kind": "stringLiteral"
346+
},
347+
{
348+
"text": ";",
349+
"kind": "punctuation"
350+
},
351+
{
352+
"text": "\n",
353+
"kind": "lineBreak"
354+
},
355+
{
356+
"text": "}",
357+
"kind": "punctuation"
358+
}
359+
],
360+
"documentation": []
361+
}
362+
]
363+
}
364+
}
365+
]

0 commit comments

Comments
 (0)