fix(3758): show completion for object literals in an assignment pattern (#40976)

This commit is contained in:
Alex T
2020-10-08 22:27:53 +03:00
committed by GitHub
parent 197ac804d1
commit 23c5f9260c
3 changed files with 31 additions and 4 deletions

View File

@@ -1891,10 +1891,12 @@ namespace ts.Completions {
let existingMembers: readonly Declaration[] | undefined;
if (objectLikeContainer.kind === SyntaxKind.ObjectLiteralExpression) {
const instantiatedType = typeChecker.getContextualType(objectLikeContainer);
const completionsType = instantiatedType && typeChecker.getContextualType(objectLikeContainer, ContextFlags.Completions);
if (!instantiatedType || !completionsType) return GlobalsSearch.Fail;
isNewIdentifierLocation = hasIndexSignature(instantiatedType || completionsType);
const instantiatedType = tryGetObjectLiteralContextualType(objectLikeContainer, typeChecker);
if (instantiatedType === undefined) {
return GlobalsSearch.Fail;
}
const completionsType = typeChecker.getContextualType(objectLikeContainer, ContextFlags.Completions);
isNewIdentifierLocation = hasIndexSignature(completionsType || instantiatedType);
typeMembers = getPropertiesForObjectExpression(instantiatedType, completionsType, objectLikeContainer, typeChecker);
existingMembers = objectLikeContainer.properties;
}
@@ -2830,4 +2832,15 @@ namespace ts.Completions {
function isStaticProperty(symbol: Symbol) {
return !!(symbol.valueDeclaration && getEffectiveModifierFlags(symbol.valueDeclaration) & ModifierFlags.Static && isClassLike(symbol.valueDeclaration.parent));
}
function tryGetObjectLiteralContextualType(node: ObjectLiteralExpression, typeChecker: TypeChecker) {
const type = typeChecker.getContextualType(node);
if (type) {
return type;
}
if (isBinaryExpression(node.parent) && node.parent.operatorToken.kind === SyntaxKind.EqualsToken) {
return typeChecker.getTypeAtLocation(node.parent);
}
return undefined;
}
}

View File

@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />
////let x = { a: 1, b: 2 };
////let y = ({ /**/ } = x, 1);
verify.completions({ marker: "", exact: ["a", "b"] });

View File

@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />
////let x = { a: 1, b: 2 };
////let y = ({ a, /**/ } = x, 1);
verify.completions({ marker: "", exact: ["b"] });