Skip to content

Commit 53152d1

Browse files
committed
fix: objectLiteralCompletions was completing after property access
1 parent e6074bb commit 53152d1

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

typescript/src/completions/objectLiteralCompletions.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,63 @@ export default (
1414
// plans to make it hihgly configurable! e.g. if user wants to make some subtype leading (e.g. from [] | {})
1515
if (ts.isIdentifier(node)) node = node.parent
1616
if (ts.isShorthandPropertyAssignment(node)) node = node.parent
17+
if (!ts.isObjectLiteralExpression(node)) return
18+
1719
entries = [...entries]
18-
if (ts.isObjectLiteralExpression(node)) {
19-
const typeChecker = languageService.getProgram()!.getTypeChecker()!
20-
const objType = typeChecker.getContextualType(node)
21-
if (!objType) return
22-
// its doesn't return all actual properties in some cases e.g. it would be more correct to use symbols from entries, but there is a block from TS
23-
const properties = objType.getProperties()
24-
for (const property of properties) {
25-
const entry = entries.find(({ name }) => name === property.name)
26-
if (!entry) continue
27-
const type = typeChecker.getTypeOfSymbolAtLocation(property, node)
28-
if (!type) continue
29-
if (isMethodCompletionCall(type, typeChecker)) {
30-
if (['above', 'remove'].includes(keepOriginal) && preferences.includeCompletionsWithObjectLiteralMethodSnippets) {
31-
const methodEntryIndex = entries.findIndex(e => e.name === entry.name && isObjectLiteralMethodSnippet(e))
32-
const methodEntry = entries[methodEntryIndex]
33-
if (methodEntry) {
34-
entries.splice(methodEntryIndex, 1)
35-
entries.splice(entries.indexOf(entry) + (keepOriginal === 'below' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, {
36-
...methodEntry,
37-
// let correctSorting.enable sort it
38-
sortText: entry.sortText,
39-
})
40-
}
20+
const typeChecker = languageService.getProgram()!.getTypeChecker()!
21+
const objType = typeChecker.getContextualType(node)
22+
if (!objType) return
23+
// its doesn't return all actual properties in some cases e.g. it would be more correct to use symbols from entries, but there is a block from TS
24+
const properties = objType.getProperties()
25+
for (const property of properties) {
26+
const entry = entries.find(({ name }) => name === property.name)
27+
if (!entry) continue
28+
const type = typeChecker.getTypeOfSymbolAtLocation(property, node)
29+
if (!type) continue
30+
if (isMethodCompletionCall(type, typeChecker)) {
31+
if (['above', 'remove'].includes(keepOriginal) && preferences.includeCompletionsWithObjectLiteralMethodSnippets) {
32+
const methodEntryIndex = entries.findIndex(e => e.name === entry.name && isObjectLiteralMethodSnippet(e))
33+
const methodEntry = entries[methodEntryIndex]
34+
if (methodEntry) {
35+
entries.splice(methodEntryIndex, 1)
36+
entries.splice(entries.indexOf(entry) + (keepOriginal === 'below' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, {
37+
...methodEntry,
38+
// let correctSorting.enable sort it
39+
sortText: entry.sortText,
40+
})
4141
}
42-
continue
43-
}
44-
if (!enableMoreVariants) continue
45-
const getQuotedSnippet = (): [string, string] => {
46-
const quote = tsFull.getQuoteFromPreference(tsFull.getQuotePreference(node.getSourceFile() as any, preferences))
47-
return [`: ${quote}$1${quote},$0`, `: ${quote}${quote},`]
4842
}
49-
const insertObjectArrayInnerText = c('objectLiteralCompletions.insertNewLine') ? '\n\t$1\n' : '$1'
50-
const completingStyleMap = [
51-
[getQuotedSnippet, isStringCompletion],
52-
[[`: [${insertObjectArrayInnerText}],$0`, `: [],`], isArrayCompletion],
53-
[[`: {${insertObjectArrayInnerText}},$0`, `: {},`], isObjectCompletion],
54-
] as const
55-
const fallbackSnippet = c('objectLiteralCompletions.fallbackVariant') ? ([': $0,', ': ,'] as const) : undefined
56-
const insertSnippetVariant = completingStyleMap.find(([, detector]) => detector(type, typeChecker))?.[0] ?? fallbackSnippet
57-
if (!insertSnippetVariant) continue
58-
const [insertSnippetText, insertSnippetPreview] = typeof insertSnippetVariant === 'function' ? insertSnippetVariant() : insertSnippetVariant
59-
const insertText = entry.name + insertSnippetText
60-
const index = entries.indexOf(entry)
61-
entries.splice(index + (keepOriginal === 'below' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, {
62-
...entry,
63-
// todo setting incompatible!!!
64-
sortText: entry.sortText,
65-
labelDetails: {
66-
detail: insertSnippetPreview,
67-
},
68-
insertText,
69-
isSnippet: true,
70-
})
43+
continue
44+
}
45+
if (!enableMoreVariants) continue
46+
const getQuotedSnippet = (): [string, string] => {
47+
const quote = tsFull.getQuoteFromPreference(tsFull.getQuotePreference(node.getSourceFile() as any, preferences))
48+
return [`: ${quote}$1${quote},$0`, `: ${quote}${quote},`]
7149
}
72-
return entries
50+
const insertObjectArrayInnerText = c('objectLiteralCompletions.insertNewLine') ? '\n\t$1\n' : '$1'
51+
const completingStyleMap = [
52+
[getQuotedSnippet, isStringCompletion],
53+
[[`: [${insertObjectArrayInnerText}],$0`, `: [],`], isArrayCompletion],
54+
[[`: {${insertObjectArrayInnerText}},$0`, `: {},`], isObjectCompletion],
55+
] as const
56+
const fallbackSnippet = c('objectLiteralCompletions.fallbackVariant') ? ([': $0,', ': ,'] as const) : undefined
57+
const insertSnippetVariant = completingStyleMap.find(([, detector]) => detector(type, typeChecker))?.[0] ?? fallbackSnippet
58+
if (!insertSnippetVariant) continue
59+
const [insertSnippetText, insertSnippetPreview] = typeof insertSnippetVariant === 'function' ? insertSnippetVariant() : insertSnippetVariant
60+
const insertText = entry.name + insertSnippetText
61+
const index = entries.indexOf(entry)
62+
entries.splice(index + (keepOriginal === 'below' ? 1 : 0), keepOriginal === 'remove' ? 1 : 0, {
63+
...entry,
64+
// todo setting incompatible!!!
65+
sortText: entry.sortText,
66+
labelDetails: {
67+
detail: insertSnippetPreview,
68+
},
69+
insertText,
70+
isSnippet: true,
71+
})
7372
}
73+
return entries
7474
}
7575
}
7676

typescript/src/completionsAtPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export const getCompletionsAtPosition = (
175175
}
176176

177177
if (node) prior.entries = defaultHelpers(prior.entries, node, languageService) ?? prior.entries
178-
if (node) prior.entries = objectLiteralCompletions(prior.entries, node, languageService, options ?? {}, c) ?? prior.entries
178+
if (exactNode) prior.entries = objectLiteralCompletions(prior.entries, exactNode, languageService, options ?? {}, c) ?? prior.entries
179179

180180
const banAutoImportPackages = c('suggestions.banAutoImportPackages')
181181
if (banAutoImportPackages?.length)

0 commit comments

Comments
 (0)