From c3c44dc3c81ee12ddf9ede71f94a4b731feb8a45 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 4 Dec 2014 12:55:54 -0800 Subject: [PATCH] Some renaming and added comments as per feedback --- src/compiler/checker.ts | 20 ++++++++++++++++---- src/compiler/types.ts | 4 ++-- src/services/services.ts | 22 +++++++++++----------- src/services/signatureHelp.ts | 2 +- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 056d4895e8c..f58d5808816 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -93,9 +93,9 @@ module ts { getIndexTypeOfType, getReturnTypeOfSignature, getSymbolsInScope, - getSymbolInfoOfLocation, + getSymbolAtLocation, getShorthandAssignmentValueSymbol, - getTypeOfLocation, + getTypeAtLocation, typeToString, getSymbolDisplayBuilder, symbolToString, @@ -4398,18 +4398,30 @@ module ts { ts.forEach(containerNodes, node => { getTypeOfNode(node); }); } - function getSymbolInfoOfLocation(node: Node): Symbol { + function getSymbolAtLocation(node: Node): Symbol { resolveLocation(node); return getSymbolInfo(node); } - function getTypeOfLocation(node: Node): Type { + function getTypeAtLocation(node: Node): Type { resolveLocation(node); return getTypeOfNode(node); } function getTypeOfSymbolAtLocation(symbol: Symbol, node: Node): Type { resolveLocation(node); + // Get the narrowed type of symbol at given location instead of just getting + // the type of the symbol. + // eg. + // function foo(a: string | number) { + // if (typeof a === "string") { + // a/**/ + // } + // } + // getTypeOfSymbol for a would return type of parameter symbol string | number + // Unless we provide location /**/, checker wouldn't know how to narrow the type + // By using getNarrowedTypeOfSymbol would return string since it would be able to narrow + // it by typeguard in the if true condition return getNarrowedTypeOfSymbol(symbol, node); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index befd9c9def4..f16444ba609 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -891,9 +891,9 @@ module ts { getIndexTypeOfType(type: Type, kind: IndexKind): Type; getReturnTypeOfSignature(signature: Signature): Type; getSymbolsInScope(location: Node, meaning: SymbolFlags): Symbol[]; - getSymbolInfoOfLocation(node: Node): Symbol; + getSymbolAtLocation(node: Node): Symbol; getShorthandAssignmentValueSymbol(location: Node): Symbol; - getTypeOfLocation(node: Node): Type; + getTypeAtLocation(node: Node): Type; typeToString(type: Type, enclosingDeclaration?: Node, flags?: TypeFormatFlags): string; symbolToString(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): string; getSymbolDisplayBuilder(): SymbolDisplayBuilder; diff --git a/src/services/services.ts b/src/services/services.ts index 08f3d1fa11d..d2222a3ae9d 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2428,7 +2428,7 @@ module ts { isMemberCompletion = true; if (node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName || node.kind === SyntaxKind.PropertyAccessExpression) { - var symbol = typeInfoResolver.getSymbolInfoOfLocation(node); + var symbol = typeInfoResolver.getSymbolAtLocation(node); // This is an alias, follow what it aliases if (symbol && symbol.flags & SymbolFlags.Import) { @@ -2445,7 +2445,7 @@ module ts { } } - var type = typeInfoResolver.getTypeOfLocation(node); + var type = typeInfoResolver.getTypeAtLocation(node); if (type) { // Filter private properties forEach(type.getApparentProperties(), symbol => { @@ -3089,7 +3089,7 @@ module ts { displayParts.push(punctuationPart(SyntaxKind.CloseParenToken)); } else { - var internalAliasSymbol = typeResolver.getSymbolInfoOfLocation(importDeclaration.moduleReference); + var internalAliasSymbol = typeResolver.getSymbolAtLocation(importDeclaration.moduleReference); if (internalAliasSymbol) { displayParts.push(spacePart()); displayParts.push(operatorPart(SyntaxKind.EqualsToken)); @@ -3199,7 +3199,7 @@ module ts { return undefined; } - var symbol = typeInfoResolver.getSymbolInfoOfLocation(node); + var symbol = typeInfoResolver.getSymbolAtLocation(node); if (!symbol) { // Try getting just type at this position and show switch (node.kind) { @@ -3209,7 +3209,7 @@ module ts { case SyntaxKind.ThisKeyword: case SyntaxKind.SuperKeyword: // For the identifiers/this/super etc get the type at position - var type = typeInfoResolver.getTypeOfLocation(node); + var type = typeInfoResolver.getTypeAtLocation(node); if (type) { return { kind: ScriptElementKind.unknown, @@ -3326,7 +3326,7 @@ module ts { return undefined; } - var symbol = typeInfoResolver.getSymbolInfoOfLocation(node); + var symbol = typeInfoResolver.getSymbolAtLocation(node); // Could not find a symbol e.g. node is string or number keyword, // or the symbol was an internal symbol and does not have a declaration e.g. undefined symbol @@ -3958,7 +3958,7 @@ module ts { return getReferencesForSuperKeyword(node); } - var symbol = typeInfoResolver.getSymbolInfoOfLocation(node); + var symbol = typeInfoResolver.getSymbolAtLocation(node); // Could not find a symbol e.g. unknown identifier if (!symbol) { @@ -4210,7 +4210,7 @@ module ts { return; } - var referenceSymbol = typeInfoResolver.getSymbolInfoOfLocation(referenceLocation); + var referenceSymbol = typeInfoResolver.getSymbolAtLocation(referenceLocation); if (referenceSymbol) { var referenceSymbolDeclaration = referenceSymbol.valueDeclaration; var shorthandValueSymbol = typeInfoResolver.getShorthandAssignmentValueSymbol(referenceSymbolDeclaration); @@ -4443,7 +4443,7 @@ module ts { function getPropertySymbolFromTypeReference(typeReference: TypeReferenceNode) { if (typeReference) { - var type = typeInfoResolver.getTypeOfLocation(typeReference); + var type = typeInfoResolver.getTypeAtLocation(typeReference); if (type) { var propertySymbol = typeInfoResolver.getPropertyOfType(type, propertyName); if (propertySymbol) { @@ -4967,7 +4967,7 @@ module ts { // Only walk into nodes that intersect the requested span. if (node && span.intersectsWith(node.getStart(), node.getWidth())) { if (node.kind === SyntaxKind.Identifier && node.getWidth() > 0) { - var symbol = typeInfoResolver.getSymbolInfoOfLocation(node); + var symbol = typeInfoResolver.getSymbolAtLocation(node); if (symbol) { var type = classifySymbol(symbol, getMeaningFromLocation(node)); if (type) { @@ -5396,7 +5396,7 @@ module ts { // Can only rename an identifier. if (node && node.kind === SyntaxKind.Identifier) { - var symbol = typeInfoResolver.getSymbolInfoOfLocation(node); + var symbol = typeInfoResolver.getSymbolAtLocation(node); // Only allow a symbol to be renamed if it actually has at least one declaration. if (symbol && symbol.getDeclarations() && symbol.getDeclarations().length > 0) { diff --git a/src/services/signatureHelp.ts b/src/services/signatureHelp.ts index a8e0ce185f6..23d5c34fefb 100644 --- a/src/services/signatureHelp.ts +++ b/src/services/signatureHelp.ts @@ -457,7 +457,7 @@ module ts.SignatureHelp { var invocation = argumentListInfo.invocation; var callTarget = getInvokedExpression(invocation) - var callTargetSymbol = typeInfoResolver.getSymbolInfoOfLocation(callTarget); + var callTargetSymbol = typeInfoResolver.getSymbolAtLocation(callTarget); var callTargetDisplayParts = callTargetSymbol && symbolToDisplayParts(typeInfoResolver, callTargetSymbol, /*enclosingDeclaration*/ undefined, /*meaning*/ undefined); var items: SignatureHelpItem[] = map(candidates, candidateSignature => { var signatureHelpParameters: SignatureHelpParameter[];