Skip to content

Commit 49136f7

Browse files
andrewbranchjenamakwong0419
authored
Use getPropertiesForObjectExpression in string literal object completions (microsoft#42020)
Co-authored-by: Johanne Enama <[email protected]> Co-authored-by: Kevin Wong <[email protected]> Co-authored-by: Johanne Enama <[email protected]> Co-authored-by: Kevin Wong <[email protected]>
1 parent a763600 commit 49136f7

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/services/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2676,7 +2676,7 @@ namespace ts.Completions {
26762676
return jsdoc && jsdoc.tags && (rangeContainsPosition(jsdoc, position) ? findLast(jsdoc.tags, tag => tag.pos < position) : undefined);
26772677
}
26782678

2679-
function getPropertiesForObjectExpression(contextualType: Type, completionsType: Type | undefined, obj: ObjectLiteralExpression | JsxAttributes, checker: TypeChecker): Symbol[] {
2679+
export function getPropertiesForObjectExpression(contextualType: Type, completionsType: Type | undefined, obj: ObjectLiteralExpression | JsxAttributes, checker: TypeChecker): Symbol[] {
26802680
const hasCompletionsType = completionsType && completionsType !== contextualType;
26812681
const type = hasCompletionsType && !(completionsType!.flags & TypeFlags.AnyOrUnknown)
26822682
? checker.getUnionType([contextualType, completionsType!])

src/services/stringCompletions.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ namespace ts.Completions.StringCompletions {
163163
// foo({
164164
// '/*completion position*/'
165165
// });
166-
return stringLiteralCompletionsFromProperties(typeChecker.getContextualType(parent.parent));
166+
return stringLiteralCompletionsForObjectLiteral(typeChecker, parent.parent);
167167
}
168168
return fromContextualType();
169169

@@ -254,6 +254,25 @@ namespace ts.Completions.StringCompletions {
254254
};
255255
}
256256

257+
function stringLiteralCompletionsForObjectLiteral(checker: TypeChecker, objectLiteralExpression: ObjectLiteralExpression): StringLiteralCompletionsFromProperties | undefined {
258+
const contextualType = checker.getContextualType(objectLiteralExpression);
259+
if (!contextualType) return undefined;
260+
261+
const completionsType = checker.getContextualType(objectLiteralExpression, ContextFlags.Completions);
262+
const symbols = getPropertiesForObjectExpression(
263+
contextualType,
264+
completionsType,
265+
objectLiteralExpression,
266+
checker
267+
);
268+
269+
return {
270+
kind: StringLiteralCompletionKind.Properties,
271+
symbols,
272+
hasIndexSignature: hasIndexSignature(contextualType)
273+
};
274+
}
275+
257276
function getStringLiteralTypes(type: Type | undefined, uniques = new Map<string, true>()): readonly StringLiteralType[] {
258277
if (!type) return emptyArray;
259278
type = skipConstraint(type);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// interface A {
4+
//// "a-prop": string;
5+
//// }
6+
////
7+
//// interface B {
8+
//// "b-prop": string;
9+
//// }
10+
////
11+
//// const obj: A | B = {
12+
//// "/*1*/"
13+
//// }
14+
15+
verify.completions({
16+
marker: "1",
17+
exact: ["a-prop", "b-prop"]
18+
});

0 commit comments

Comments
 (0)