From b5b1b7b5bb7155fe49e1de7f838258be52425fe0 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 7 Jul 2015 15:26:01 -0700 Subject: [PATCH] Use symbol.getName for classExpression and functionExpression since it now correctly represent declared-name. --- src/compiler/checker.ts | 13 ++++++++----- src/services/services.ts | 39 ++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 773f1e3f7d0..d18d0faba9b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13414,7 +13414,7 @@ namespace ts { case SyntaxKind.ClassExpression: let className = (location).name; if (className) { - copySymbol(className.text, location.symbol, meaning); + copySymbol(location.symbol, meaning); } // fall through; this fall-through is necessary because we would like to handle // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration @@ -13432,7 +13432,7 @@ namespace ts { case SyntaxKind.ClassExpression: let funcName = (location).name; if (funcName) { - copySymbol(funcName.text, location.symbol, meaning); + copySymbol(location.symbol, meaning); } break; } @@ -13451,9 +13451,12 @@ namespace ts { * @param symbol the symbol to be added into symbol table * @param meaning meaning of symbol to filter by before adding to symbol table */ - function copySymbol(key: string, symbol: Symbol, meaning: SymbolFlags): void { + function copySymbol(symbol: Symbol, meaning: SymbolFlags): void { if (symbol.flags & meaning) { - let id = key || symbol.name; + let id = symbol.name; + // We will copy all symbol regardless of its reserved name because + // symbolsToArray will check whether the key is a reserved name and + // it will not copy symbol with reserved name to the array if (!hasProperty(symbols, id)) { symbols[id] = symbol; } @@ -13464,7 +13467,7 @@ namespace ts { if (meaning) { for (let id in source) { let symbol = source[id]; - copySymbol(symbol.name, symbol, meaning); + copySymbol(symbol, meaning); } } } diff --git a/src/services/services.ts b/src/services/services.ts index 8260d41c837..756bb200687 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2808,30 +2808,23 @@ namespace ts { * @return undefined if the name is of external module otherwise a name with striped of any quote */ function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean, location: Node): string { - let displayName: string; + let displayName: string = symbol.getName(); - // In the case of default export, function expression and class expression, - // the binder bind them with "default", "__function", "__class" respectively. - // However, for completion entry, we want to display its declared name rather than binder name. - if (getLocalSymbolForExportDefault(symbol) || - getDeclarationOfKind(symbol, SyntaxKind.FunctionExpression) || - getDeclarationOfKind(symbol, SyntaxKind.ClassExpression)) { - let typeChecker = program.getTypeChecker(); - displayName = getDeclaredName(typeChecker, symbol, location); - - // At this point, we expect that all completion list entries have declared name including function expression and class expression - // because when we gather all relevant symbols, we check that the function expression and class expression must have declared name - // before adding the symbol into our symbols table. (see: getSymbolsInScope) - Debug.assert(displayName !== undefined, "Expected displayed name from declaration to existed in this symbol: " + symbol.getName()); - } - else { - displayName = symbol.getName(); - let firstCharCode = displayName.charCodeAt(0); - // First check of the displayName is not external module; if it is an external module, it is not valid entry - if ((symbol.flags & SymbolFlags.Namespace) && (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) { - // If the symbol is external module, don't show it in the completion list - // (i.e declare module "http" { let x; } | // <= request completion here, "http" should not be there) - return undefined; + if (displayName) { + if (displayName === "default") { + // In the case of default export, the binder bind them with "default". + // However, for completion entry, we want to display its declared name rather than binder name. + let typeChecker = program.getTypeChecker(); + displayName = getDeclaredName(typeChecker, symbol, location); + } + else { + let firstCharCode = displayName.charCodeAt(0); + // First check of the displayName is not external module; if it is an external module, it is not valid entry + if ((symbol.flags & SymbolFlags.Namespace) && (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) { + // If the symbol is external module, don't show it in the completion list + // (i.e declare module "http" { let x; } | // <= request completion here, "http" should not be there) + return undefined; + } } }