Skip to content

Commit 431e959

Browse files
committed
fix object literal completions to not include duplicates in unions
1 parent 5ff1142 commit 431e959

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

typescript/src/completions/objectLiteralCompletions.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ export default (
2121
const objType = typeChecker.getContextualType(node)
2222
if (!objType) return
2323
const types = objType.isUnion() ? objType.types : [objType]
24-
const properties = types.flatMap(type => {
25-
if (isFunctionType(type, typeChecker)) return []
26-
if (isObjectCompletion(type, typeChecker)) return typeChecker.getPropertiesOfType(type)
27-
return []
28-
})
24+
const properties = types
25+
.flatMap(type => {
26+
if (isFunctionType(type, typeChecker)) return []
27+
if (isObjectCompletion(type, typeChecker)) return typeChecker.getPropertiesOfType(type)
28+
return []
29+
})
30+
.filter((property, i, arr) => {
31+
return !arr.find(({ name }, k) => name === property.name && i !== k)
32+
})
2933
for (const property of properties) {
3034
const entry = entries.find(({ name }) => name === property.name)
3135
if (!entry) continue

typescript/test/completions.spec.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,16 @@ test('Object Literal Completions', () => {
314314
usedOption,
315315
/*1*/
316316
})
317+
318+
const somethingWithUntions: { a: string } | { a: any[], b: string } = {/*2*/}
317319
`)
318-
const { entriesSorted } = getCompletionsAtPosition(numPositions[1]!) ?? {}
320+
const { entriesSorted: pos1 } = getCompletionsAtPosition(numPositions[1]!)!
321+
const { entriesSorted: pos2 } = getCompletionsAtPosition(numPositions[2]!)!
319322
// todo resolve sorting problem + add tests with other keepOriginal (it was tested manually)
320-
for (const entry of entriesSorted ?? []) {
323+
for (const entry of [...pos1, ...pos2]) {
321324
entry.insertText = entry.insertText?.replaceAll('\n', '\\n')
322325
}
323-
expect(entriesSorted).toMatchInlineSnapshot(`
326+
expect(pos1).toMatchInlineSnapshot(/* json */ `
324327
[
325328
{
326329
"insertText": "plugins",
@@ -389,6 +392,34 @@ test('Object Literal Completions', () => {
389392
},
390393
]
391394
`)
395+
expect(pos2).toMatchInlineSnapshot(`
396+
[
397+
{
398+
"insertText": "a",
399+
"isSnippet": true,
400+
"kind": "property",
401+
"kindModifiers": "",
402+
"name": "a",
403+
},
404+
{
405+
"insertText": "b",
406+
"isSnippet": true,
407+
"kind": "property",
408+
"kindModifiers": "",
409+
"name": "b",
410+
},
411+
{
412+
"insertText": "b: \\"$1\\",$0",
413+
"isSnippet": true,
414+
"kind": "property",
415+
"kindModifiers": "",
416+
"labelDetails": {
417+
"detail": ": \\"\\",",
418+
},
419+
"name": "b",
420+
},
421+
]
422+
`)
392423
})
393424

394425
// TODO move/remove this test from here

0 commit comments

Comments
 (0)