Skip to content

Commit c114de1

Browse files
Basic completion in object destructuring working.
1 parent 513d73a commit c114de1

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11993,7 +11993,7 @@ namespace ts {
1199311993
return resolveExternalModuleName(node, <LiteralExpression>node);
1199411994
}
1199511995

11996-
// Intentional fall-through
11996+
// fall through
1199711997
case SyntaxKind.NumericLiteral:
1199811998
// index access
1199911999
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
@@ -12060,6 +12060,10 @@ namespace ts {
1206012060
return symbol && getTypeOfSymbol(symbol);
1206112061
}
1206212062

12063+
if (isBindingPattern(node)) {
12064+
return getTypeForVariableLikeDeclaration(<VariableLikeDeclaration>node.parent);
12065+
}
12066+
1206312067
if (isInRightSideOfImportOrExportAssignment(<Identifier>node)) {
1206412068
let symbol = getSymbolInfo(node);
1206512069
let declaredType = symbol && getDeclaredTypeOfSymbol(symbol);

src/services/services.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,21 +2985,32 @@ namespace ts {
29852985
}
29862986

29872987
function tryGetGlobalSymbols(): boolean {
2988-
let containingObjectLiteral = <ObjectLiteralExpression>getContainingObjectLiteralOrBindingPatternIfApplicableForCompletion(contextToken);
2989-
if (containingObjectLiteral && containingObjectLiteral.kind === SyntaxKind.ObjectLiteralExpression) {
2988+
let objectLikeContainer = getContainingObjectLiteralOrBindingPatternIfApplicableForCompletion(contextToken);
2989+
if (objectLikeContainer) {
29902990
// Object literal expression, look up possible property names from contextual type
29912991
isMemberCompletion = true;
29922992
isNewIdentifierLocation = true;
29932993

2994-
let contextualType = typeChecker.getContextualType(containingObjectLiteral);
2995-
if (!contextualType) {
2994+
let typeForObject: Type;
2995+
let existingMembers: Declaration[];
2996+
2997+
if (objectLikeContainer.kind === SyntaxKind.ObjectLiteralExpression) {
2998+
typeForObject = typeChecker.getContextualType(<ObjectLiteralExpression>objectLikeContainer);
2999+
existingMembers = (<ObjectLiteralExpression>objectLikeContainer).properties;
3000+
}
3001+
else {
3002+
typeForObject = typeChecker.getTypeAtLocation(objectLikeContainer);
3003+
existingMembers = (<BindingPattern>objectLikeContainer).elements;
3004+
}
3005+
3006+
if (!typeForObject) {
29963007
return false;
29973008
}
29983009

2999-
let contextualTypeMembers = typeChecker.getPropertiesOfType(contextualType);
3000-
if (contextualTypeMembers && contextualTypeMembers.length > 0) {
3010+
let typeMembers = typeChecker.getPropertiesOfType(typeForObject);
3011+
if (typeMembers && typeMembers.length > 0) {
30013012
// Add filtered items to the completion list
3002-
symbols = filterContextualMembersList(contextualTypeMembers, containingObjectLiteral.properties);
3013+
symbols = filterContextualMembersList(typeMembers, existingMembers);
30033014
}
30043015
}
30053016
else if (getAncestor(contextToken, SyntaxKind.ImportClause)) {
@@ -3235,8 +3246,7 @@ namespace ts {
32353246
containingNodeKind === SyntaxKind.ClassDeclaration || // class A<T, |
32363247
containingNodeKind === SyntaxKind.FunctionDeclaration || // function A<T, |
32373248
containingNodeKind === SyntaxKind.InterfaceDeclaration || // interface A<T, |
3238-
containingNodeKind === SyntaxKind.ArrayBindingPattern || // var [x, y|
3239-
containingNodeKind === SyntaxKind.ObjectBindingPattern; // function func({ x, y|
3249+
containingNodeKind === SyntaxKind.ArrayBindingPattern; // var [x, y|
32403250

32413251
case SyntaxKind.DotToken:
32423252
return containingNodeKind === SyntaxKind.ArrayBindingPattern; // var [.|
@@ -3254,8 +3264,7 @@ namespace ts {
32543264
case SyntaxKind.OpenBraceToken:
32553265
return containingNodeKind === SyntaxKind.EnumDeclaration || // enum a { |
32563266
containingNodeKind === SyntaxKind.InterfaceDeclaration || // interface a { |
3257-
containingNodeKind === SyntaxKind.TypeLiteral || // let x : { |
3258-
containingNodeKind === SyntaxKind.ObjectBindingPattern; // function func({ x|
3267+
containingNodeKind === SyntaxKind.TypeLiteral; // let x : { |
32593268

32603269
case SyntaxKind.SemicolonToken:
32613270
return containingNodeKind === SyntaxKind.PropertySignature &&

0 commit comments

Comments
 (0)