Skip to content

Completion list for type literals in type arguments #43526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ namespace ts {
return node && getContextualTypeForJsxAttribute(node);
},
isContextSensitive,
getTypeOfPropertyOfContextualType,
getFullyQualifiedName,
getResolvedSignature: (node, candidatesOutArray, argumentCount) =>
getResolvedSignatureWorker(node, candidatesOutArray, argumentCount, CheckMode.Normal),
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4127,6 +4127,7 @@ namespace ts {
/* @internal */ getContextualTypeForArgumentAtIndex(call: CallLikeExpression, argIndex: number): Type | undefined;
/* @internal */ getContextualTypeForJsxAttribute(attribute: JsxAttribute | JsxSpreadAttribute): Type | undefined;
/* @internal */ isContextSensitive(node: Expression | MethodDeclaration | ObjectLiteralElementLike | JsxAttributeLike): boolean;
/* @internal */ getTypeOfPropertyOfContextualType(type: Type, name: __String): Type | undefined;

/**
* returns unknownSignature in the case of an error.
Expand Down
72 changes: 71 additions & 1 deletion src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,8 @@ namespace ts.Completions {
}

function tryGetGlobalSymbols(): boolean {
const result: GlobalsSearch = tryGetObjectLikeCompletionSymbols()
const result: GlobalsSearch = tryGetObjectTypeLiteralInTypeArgumentCompletionSymbols()
|| tryGetObjectLikeCompletionSymbols()
|| tryGetImportCompletionSymbols()
|| tryGetImportOrExportClauseCompletionSymbols()
|| tryGetLocalNamedExportCompletionSymbols()
Expand Down Expand Up @@ -1911,6 +1912,32 @@ namespace ts.Completions {
position === contextToken.end && (!!contextToken.isUnterminated || isRegularExpressionLiteral(contextToken)));
}

function tryGetObjectTypeLiteralInTypeArgumentCompletionSymbols(): GlobalsSearch | undefined {
const typeLiteralNode = tryGetTypeLiteralNode(contextToken);
if (!typeLiteralNode) return GlobalsSearch.Continue;

const intersectionTypeNode = isIntersectionTypeNode(typeLiteralNode.parent) ? typeLiteralNode.parent : undefined;
const containerTypeNode = intersectionTypeNode || typeLiteralNode;

const containerExpectedType = getConstraintOfTypeArgumentProperty(containerTypeNode, typeChecker);
if (!containerExpectedType) return GlobalsSearch.Continue;

const containerActualType = typeChecker.getTypeFromTypeNode(containerTypeNode);

const members = getPropertiesForCompletion(containerExpectedType, typeChecker);
const existingMembers = getPropertiesForCompletion(containerActualType, typeChecker);

const existingMemberEscapedNames: Set<__String> = new Set();
existingMembers.forEach(s => existingMemberEscapedNames.add(s.escapedName));

symbols = filter(members, s => !existingMemberEscapedNames.has(s.escapedName));

completionKind = CompletionKind.ObjectPropertyDeclaration;
isNewIdentifierLocation = true;

return GlobalsSearch.Success;
}

/**
* Aggregates relevant symbols for completion in object literals and object binding patterns.
* Relevant symbols are stored in the captured 'symbols' variable.
Expand Down Expand Up @@ -2857,6 +2884,49 @@ namespace ts.Completions {
}
}

function tryGetTypeLiteralNode(node: Node): TypeLiteralNode | undefined {
if (!node) return undefined;

const parent = node.parent;

switch (node.kind) {
case SyntaxKind.OpenBraceToken:
if (isTypeLiteralNode(parent)) {
return parent;
}
break;
case SyntaxKind.SemicolonToken:
case SyntaxKind.CommaToken:
case SyntaxKind.Identifier:
if (parent.kind === SyntaxKind.PropertySignature && isTypeLiteralNode(parent.parent)) {
return parent.parent;
}
break;
}

return undefined;
}

function getConstraintOfTypeArgumentProperty(node: Node, checker: TypeChecker): Type | undefined {
if (!node) return undefined;

if (isTypeNode(node) && isTypeReferenceType(node.parent)) {
return checker.getTypeArgumentConstraint(node);
}

const t = getConstraintOfTypeArgumentProperty(node.parent, checker);
if (!t) return undefined;

switch (node.kind) {
case SyntaxKind.PropertySignature:
return checker.getTypeOfPropertyOfContextualType(t, node.symbol.escapedName);
case SyntaxKind.IntersectionType:
case SyntaxKind.TypeLiteral:
case SyntaxKind.UnionType:
return t;
}
}

// TODO: GH#19856 Would like to return `node is Node & { parent: (ClassElement | TypeElement) & { parent: ObjectTypeDeclaration } }` but then compilation takes > 10 minutes
function isFromObjectTypeDeclaration(node: Node): boolean {
return node.parent && isClassOrTypeElement(node.parent) && isObjectTypeDeclaration(node.parent.parent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
//// 333: symbol;
//// '4four': boolean;
//// '5 five': object;
//// number: string;
//// Object: number;
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{/**/

verify.completions({
marker: "",
exact: ["one", "two", "\"333\"", "\"4four\"", "\"5 five\"", "number", "Object"],
isNewIdentifierLocation: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{ on/**/

verify.completions({ marker: "", exact: ["one", "two"], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{ one: string, /**/

verify.completions({ marker: "", exact: "two", isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{ one: string } & {/**/

verify.completions({ marker: "", exact: "two", isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{ prop1: string } & {/**/

verify.completions({ marker: "", exact: ["one", "two"], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: number;
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{ one: string } | {/**/

verify.completions({ marker: "", exact: ["one", "two"], isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: {
//// three: number;
//// }
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{
//// two: {/**/

verify.completions({ marker: "", exact: "three", isNewIdentifierLocation: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: {
//// three: {
//// four: number;
//// five: string;
//// }
//// }
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{
//// two: {
//// three: {
//// five: string,
//// /*4*/
//// },
//// /*0*/
//// },
//// /*1*/
////}>;

verify.completions(
{ marker: "4", exact: "four", isNewIdentifierLocation: true },
{ marker: "0", exact: [], isNewIdentifierLocation: true },
{ marker: "1", exact: "one", isNewIdentifierLocation: true },
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path="fourslash.ts" />

////interface Foo {
//// one: string;
//// two: {
//// three: {
//// four: number;
//// five: string;
//// }
//// }
////}
////
////interface Bar<T extends Foo> {
//// foo: T;
////}
////
////var foobar: Bar<{
//// two: {
//// three: { five:/*g*/ } & {/*4*/},
//// }
////}>;

verify.completions({ marker: "g", includes: ["Foo", "Bar", ...completion.globalTypes] });
verify.completions({ marker: "4", exact: "four", isNewIdentifierLocation: true });