Use getPropertiesForObjectExpression in string literal object completions (#42020)

Co-authored-by: Johanne Enama <jenama@users.noreply.github.com>
Co-authored-by: Kevin Wong <kwong0419@users.noreply.github.com>

Co-authored-by: Johanne Enama <jenama@users.noreply.github.com>
Co-authored-by: Kevin Wong <kwong0419@users.noreply.github.com>
This commit is contained in:
Andrew Branch
2020-12-18 14:20:41 -08:00
committed by GitHub
parent a763600cc4
commit 49136f7879
3 changed files with 39 additions and 2 deletions

View File

@@ -2676,7 +2676,7 @@ namespace ts.Completions {
return jsdoc && jsdoc.tags && (rangeContainsPosition(jsdoc, position) ? findLast(jsdoc.tags, tag => tag.pos < position) : undefined);
}
function getPropertiesForObjectExpression(contextualType: Type, completionsType: Type | undefined, obj: ObjectLiteralExpression | JsxAttributes, checker: TypeChecker): Symbol[] {
export function getPropertiesForObjectExpression(contextualType: Type, completionsType: Type | undefined, obj: ObjectLiteralExpression | JsxAttributes, checker: TypeChecker): Symbol[] {
const hasCompletionsType = completionsType && completionsType !== contextualType;
const type = hasCompletionsType && !(completionsType!.flags & TypeFlags.AnyOrUnknown)
? checker.getUnionType([contextualType, completionsType!])

View File

@@ -163,7 +163,7 @@ namespace ts.Completions.StringCompletions {
// foo({
// '/*completion position*/'
// });
return stringLiteralCompletionsFromProperties(typeChecker.getContextualType(parent.parent));
return stringLiteralCompletionsForObjectLiteral(typeChecker, parent.parent);
}
return fromContextualType();
@@ -254,6 +254,25 @@ namespace ts.Completions.StringCompletions {
};
}
function stringLiteralCompletionsForObjectLiteral(checker: TypeChecker, objectLiteralExpression: ObjectLiteralExpression): StringLiteralCompletionsFromProperties | undefined {
const contextualType = checker.getContextualType(objectLiteralExpression);
if (!contextualType) return undefined;
const completionsType = checker.getContextualType(objectLiteralExpression, ContextFlags.Completions);
const symbols = getPropertiesForObjectExpression(
contextualType,
completionsType,
objectLiteralExpression,
checker
);
return {
kind: StringLiteralCompletionKind.Properties,
symbols,
hasIndexSignature: hasIndexSignature(contextualType)
};
}
function getStringLiteralTypes(type: Type | undefined, uniques = new Map<string, true>()): readonly StringLiteralType[] {
if (!type) return emptyArray;
type = skipConstraint(type);

View File

@@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />
//// interface A {
//// "a-prop": string;
//// }
////
//// interface B {
//// "b-prop": string;
//// }
////
//// const obj: A | B = {
//// "/*1*/"
//// }
verify.completions({
marker: "1",
exact: ["a-prop", "b-prop"]
});