mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-26 19:25:41 -05:00
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:
@@ -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!])
|
||||
|
||||
@@ -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);
|
||||
|
||||
18
tests/cases/fourslash/completionsQuotedObjectLiteralUnion.ts
Normal file
18
tests/cases/fourslash/completionsQuotedObjectLiteralUnion.ts
Normal 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"]
|
||||
});
|
||||
Reference in New Issue
Block a user