Skip to content

Commit 1939c7f

Browse files
committed
Fix the getSymbolInfo and getTypeOfNode entry points to resolve the context sensitive information before resolving the actual node
1 parent c40e0f6 commit 1939c7f

6 files changed

Lines changed: 55 additions & 22 deletions

File tree

src/compiler/checker.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ module ts {
9393
getIndexTypeOfType,
9494
getReturnTypeOfSignature,
9595
getSymbolsInScope,
96-
getSymbolInfo,
96+
getSymbolInfoOfLocation,
9797
getShorthandAssignmentValueSymbol,
98-
getTypeOfNode,
98+
getTypeOfLocation,
9999
typeToString,
100100
getSymbolDisplayBuilder,
101101
symbolToString,
@@ -4385,18 +4385,33 @@ module ts {
43854385
}
43864386
}
43874387

4388-
function getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type {
4388+
function resolveLocation(node: Node) {
4389+
// Resolve location from top down towards node if it is a context sensitive expression
4390+
// That helps in making sure not assigning types as any when resolved out of order
43894391
var containerNodes: Node[] = [];
43904392
for (var parent = node.parent; parent; parent = parent.parent) {
4391-
if (isExpression(parent) && isContextSensitiveExpression(<Expression>parent)) {
4392-
containerNodes.unshift(parent)
4393-
};
4393+
if (isExpression(parent) && isContextSensitiveExpression(<Expression>parent)) {
4394+
containerNodes.unshift(parent);
4395+
}
43944396
}
43954397

43964398
ts.forEach(containerNodes, node => { getTypeOfNode(node); });
4399+
}
4400+
4401+
function getSymbolInfoOfLocation(node: Node): Symbol {
4402+
resolveLocation(node);
4403+
return getSymbolInfo(node);
4404+
}
4405+
4406+
function getTypeOfLocation(node: Node): Type {
4407+
resolveLocation(node);
4408+
return getTypeOfNode(node);
4409+
}
43974410

4411+
function getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type {
4412+
resolveLocation(node);
43984413
return getNarrowedTypeOfSymbol(symbol, node);
4399-
}
4414+
}
44004415

44014416
// Get the narrowed type of a given symbol at a given location
44024417
function getNarrowedTypeOfSymbol(symbol: Symbol, node: Node) {

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,9 @@ module ts {
891891
getIndexTypeOfType(type: Type, kind: IndexKind): Type;
892892
getReturnTypeOfSignature(signature: Signature): Type;
893893
getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[];
894-
getSymbolInfo(node: Node): Symbol;
894+
getSymbolInfoOfLocation(node: Node): Symbol;
895895
getShorthandAssignmentValueSymbol(location: Node): Symbol;
896-
getTypeOfNode(node: Node): Type;
896+
getTypeOfLocation(node: Node): Type;
897897
typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string;
898898
symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string;
899899
getSymbolDisplayBuilder(): SymbolDisplayBuilder;

src/harness/typeWriter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class TypeWriterWalker {
9494
}
9595

9696
private getTypeOfNode(node: ts.Node): ts.Type {
97-
var type = this.checker.getTypeOfNode(node);
97+
var type = this.checker.getTypeOfLocation(node);
9898
ts.Debug.assert(type !== undefined, "type doesn't exist");
9999
return type;
100100
}

src/services/services.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@ module ts {
24282428
isMemberCompletion = true;
24292429

24302430
if (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName || node.kind === SyntaxKind.PropertyAccessExpression) {
2431-
var symbol = typeInfoResolver.getSymbolInfo(node);
2431+
var symbol = typeInfoResolver.getSymbolInfoOfLocation(node);
24322432

24332433
// This is an alias, follow what it aliases
24342434
if (symbol && symbol.flags & SymbolFlags.Import) {
@@ -2445,7 +2445,7 @@ module ts {
24452445
}
24462446
}
24472447

2448-
var type = typeInfoResolver.getTypeOfNode(node);
2448+
var type = typeInfoResolver.getTypeOfLocation(node);
24492449
if (type) {
24502450
// Filter private properties
24512451
forEach(type.getApparentProperties(), symbol => {
@@ -3089,7 +3089,7 @@ module ts {
30893089
displayParts.push(punctuationPart(SyntaxKind.CloseParenToken));
30903090
}
30913091
else {
3092-
var internalAliasSymbol = typeResolver.getSymbolInfo(importDeclaration.moduleReference);
3092+
var internalAliasSymbol = typeResolver.getSymbolInfoOfLocation(importDeclaration.moduleReference);
30933093
if (internalAliasSymbol) {
30943094
displayParts.push(spacePart());
30953095
displayParts.push(operatorPart(SyntaxKind.EqualsToken));
@@ -3199,7 +3199,7 @@ module ts {
31993199
return undefined;
32003200
}
32013201

3202-
var symbol = typeInfoResolver.getSymbolInfo(node);
3202+
var symbol = typeInfoResolver.getSymbolInfoOfLocation(node);
32033203
if (!symbol) {
32043204
// Try getting just type at this position and show
32053205
switch (node.kind) {
@@ -3209,7 +3209,7 @@ module ts {
32093209
case SyntaxKind.ThisKeyword:
32103210
case SyntaxKind.SuperKeyword:
32113211
// For the identifiers/this/super etc get the type at position
3212-
var type = typeInfoResolver.getTypeOfNode(node);
3212+
var type = typeInfoResolver.getTypeOfLocation(node);
32133213
if (type) {
32143214
return {
32153215
kind: ScriptElementKind.unknown,
@@ -3326,7 +3326,7 @@ module ts {
33263326
return undefined;
33273327
}
33283328

3329-
var symbol = typeInfoResolver.getSymbolInfo(node);
3329+
var symbol = typeInfoResolver.getSymbolInfoOfLocation(node);
33303330

33313331
// Could not find a symbol e.g. node is string or number keyword,
33323332
// or the symbol was an internal symbol and does not have a declaration e.g. undefined symbol
@@ -3958,7 +3958,7 @@ module ts {
39583958
return getReferencesForSuperKeyword(node);
39593959
}
39603960

3961-
var symbol = typeInfoResolver.getSymbolInfo(node);
3961+
var symbol = typeInfoResolver.getSymbolInfoOfLocation(node);
39623962

39633963
// Could not find a symbol e.g. unknown identifier
39643964
if (!symbol) {
@@ -4210,7 +4210,7 @@ module ts {
42104210
return;
42114211
}
42124212

4213-
var referenceSymbol = typeInfoResolver.getSymbolInfo(referenceLocation);
4213+
var referenceSymbol = typeInfoResolver.getSymbolInfoOfLocation(referenceLocation);
42144214
if (referenceSymbol) {
42154215
var referenceSymbolDeclaration = referenceSymbol.valueDeclaration;
42164216
var shorthandValueSymbol = typeInfoResolver.getShorthandAssignmentValueSymbol(referenceSymbolDeclaration);
@@ -4443,7 +4443,7 @@ module ts {
44434443

44444444
function getPropertySymbolFromTypeReference(typeReference: TypeReferenceNode) {
44454445
if (typeReference) {
4446-
var type = typeInfoResolver.getTypeOfNode(typeReference);
4446+
var type = typeInfoResolver.getTypeOfLocation(typeReference);
44474447
if (type) {
44484448
var propertySymbol = typeInfoResolver.getPropertyOfType(type, propertyName);
44494449
if (propertySymbol) {
@@ -4967,7 +4967,7 @@ module ts {
49674967
// Only walk into nodes that intersect the requested span.
49684968
if (node && span.intersectsWith(node.getStart(), node.getWidth())) {
49694969
if (node.kind === SyntaxKind.Identifier && node.getWidth() > 0) {
4970-
var symbol = typeInfoResolver.getSymbolInfo(node);
4970+
var symbol = typeInfoResolver.getSymbolInfoOfLocation(node);
49714971
if (symbol) {
49724972
var type = classifySymbol(symbol, getMeaningFromLocation(node));
49734973
if (type) {
@@ -5396,7 +5396,7 @@ module ts {
53965396

53975397
// Can only rename an identifier.
53985398
if (node && node.kind === SyntaxKind.Identifier) {
5399-
var symbol = typeInfoResolver.getSymbolInfo(node);
5399+
var symbol = typeInfoResolver.getSymbolInfoOfLocation(node);
54005400

54015401
// Only allow a symbol to be renamed if it actually has at least one declaration.
54025402
if (symbol && symbol.getDeclarations() && symbol.getDeclarations().length > 0) {

src/services/signatureHelp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ module ts.SignatureHelp {
457457

458458
var invocation = argumentListInfo.invocation;
459459
var callTarget = getInvokedExpression(invocation)
460-
var callTargetSymbol = typeInfoResolver.getSymbolInfo(callTarget);
460+
var callTargetSymbol = typeInfoResolver.getSymbolInfoOfLocation(callTarget);
461461
var callTargetDisplayParts = callTargetSymbol && symbolToDisplayParts(typeInfoResolver, callTargetSymbol, /*enclosingDeclaration*/ undefined, /*meaning*/ undefined);
462462
var items: SignatureHelpItem[] = map(candidates, candidateSignature => {
463463
var signatureHelpParameters: SignatureHelpParameter[];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface IMap<T> {
4+
//// [key: string]: T;
5+
////}
6+
////var map: IMap<{ a1: string; }[]>;
7+
////var categories: string[];
8+
////each(categories, category => {
9+
//// var changes = map[category];
10+
//// changes[0]./*1*/a1;
11+
//// return each(changes, change => {
12+
//// });
13+
////});
14+
////function each<T>(items: T[], handler: (item: T) => void) { }
15+
16+
goTo.marker('1');
17+
verify.quickInfoIs("(property) a1: string");
18+
verify.memberListContains("a1", "(property) a1: string");

0 commit comments

Comments
 (0)