Merge pull request #1079 from Microsoft/showNarrowedType

Show narrowed type in quickInfo/completion entry
This commit is contained in:
Sheetal Nandi 2014-11-06 17:17:51 -08:00
commit beef2ebbe6
6 changed files with 97 additions and 5 deletions

View File

@ -85,7 +85,7 @@ module ts {
checkProgram: checkProgram,
emitFiles: invokeEmitter,
getParentOfSymbol: getParentOfSymbol,
getTypeOfSymbol: getTypeOfSymbol,
getNarrowedTypeOfSymbol: getNarrowedTypeOfSymbol,
getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol,
getPropertiesOfType: getPropertiesOfType,
getPropertyOfType: getPropertyOfType,

View File

@ -701,7 +701,7 @@ module ts {
checkProgram(): void;
emitFiles(targetSourceFile?: SourceFile): EmitResult;
getParentOfSymbol(symbol: Symbol): Symbol;
getTypeOfSymbol(symbol: Symbol): Type;
getNarrowedTypeOfSymbol(symbol: Symbol, node: Node): Type;
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
getPropertiesOfType(type: Type): Symbol[];
getPropertyOfType(type: Type, propertyName: string): Symbol;

View File

@ -2689,14 +2689,13 @@ module ts {
var symbol = lookUp(activeCompletionSession.symbols, entryName);
if (symbol) {
var type = session.typeChecker.getTypeOfSymbol(symbol);
Debug.assert(type !== undefined, "Could not find type for symbol");
var completionEntry = createCompletionEntry(symbol, session.typeChecker);
// TODO(drosen): Right now we just permit *all* semantic meanings when calling 'getSymbolKind'
// which is permissible given that it is backwards compatible; but really we should consider
// passing the meaning for the node so that we don't report that a suggestion for a value is an interface.
// We COULD also just do what 'getSymbolModifiers' does, which is to use the first declaration.
var location = getTouchingPropertyName(sourceFile, position);
Debug.assert(session.typeChecker.getNarrowedTypeOfSymbol(symbol, location) !== undefined, "Could not find type for symbol");
var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), location, session.typeChecker, location, SemanticMeaning.All);
return {
name: entryName,
@ -2868,7 +2867,7 @@ module ts {
symbolKind = ScriptElementKind.memberVariableElement;
}
var type = typeResolver.getTypeOfSymbol(symbol);
var type = typeResolver.getNarrowedTypeOfSymbol(symbol, location);
if (type) {
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccess) {
var right = (<PropertyAccess>location.parent).right;

View File

@ -0,0 +1,20 @@
/// <reference path='fourslash.ts'/>
////function foo(strOrNum: string | number) {
//// /*1*/
//// if (typeof strOrNum === "number") {
//// /*2*/
//// }
//// else {
//// /*3*/
//// }
////}
goTo.marker('1');
verify.completionListContains("strOrNum", "(parameter) strOrNum: string | number");
goTo.marker('2');
verify.completionListContains("strOrNum", "(parameter) strOrNum: number");
goTo.marker('3');
verify.completionListContains("strOrNum", "(parameter) strOrNum: string");

View File

@ -0,0 +1,22 @@
/// <reference path='fourslash.ts'/>
////function foo(strOrNum: string | number) {
//// if (typeof /*1*/strOrNum === "number") {
//// return /*2*/strOrNum;
//// }
//// else {
//// return /*3*/strOrNum.length;
//// }
////}
goTo.marker('1');
verify.quickInfoIs('(parameter) strOrNum: string | number');
verify.completionListContains("strOrNum", "(parameter) strOrNum: string | number");
goTo.marker('2');
verify.quickInfoIs('(parameter) strOrNum: number');
verify.completionListContains("strOrNum", "(parameter) strOrNum: number");
goTo.marker('3');
verify.quickInfoIs('(parameter) strOrNum: string');
verify.completionListContains("strOrNum", "(parameter) strOrNum: string");

View File

@ -0,0 +1,51 @@
/// <reference path='fourslash.ts'/>
////var strOrNum: string | number;
////module m {
//// var nonExportedStrOrNum: string | number;
//// export var exportedStrOrNum: string | number;
//// var num: number;
//// var str: string;
//// if (typeof /*1*/nonExportedStrOrNum === "number") {
//// num = /*2*/nonExportedStrOrNum;
//// }
//// else {
//// str = /*3*/nonExportedStrOrNum.length;
//// }
//// if (typeof /*4*/exportedStrOrNum === "number") {
//// strOrNum = /*5*/exportedStrOrNum;
//// }
//// else {
//// strOrNum = /*6*/exportedStrOrNum;
//// }
////}
////if (typeof m./*7*/exportedStrOrNum === "number") {
//// strOrNum = m./*8*/exportedStrOrNum;
////}
////else {
//// strOrNum = m./*9*/exportedStrOrNum;
////}
goTo.marker('1');
verify.quickInfoIs('(var) nonExportedStrOrNum: string | number');
verify.completionListContains("nonExportedStrOrNum", "(var) nonExportedStrOrNum: string | number");
goTo.marker('2');
verify.quickInfoIs('(var) nonExportedStrOrNum: number');
verify.completionListContains("nonExportedStrOrNum", "(var) nonExportedStrOrNum: number");
goTo.marker('3');
verify.quickInfoIs('(var) nonExportedStrOrNum: string');
verify.completionListContains("nonExportedStrOrNum", "(var) nonExportedStrOrNum: string");
['4', '5', '6', '7', '8', '9'].forEach((marker, index, arr) => {
goTo.marker(marker);
verify.quickInfoIs('(var) m.exportedStrOrNum: string | number');
verify.completionListContains("exportedStrOrNum", "(var) m.exportedStrOrNum: string | number");
});
['7', '8', '9'].forEach((marker, index, arr) => {
goTo.marker(marker);
verify.quickInfoIs('(var) m.exportedStrOrNum: string | number');
verify.memberListContains("exportedStrOrNum", "(var) m.exportedStrOrNum: string | number");
});