Skip to content

Commit 514d054

Browse files
author
Yui T
committed
Address CR: Use getDeclaredName and getDeclarationOfKind
1 parent 744f640 commit 514d054

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

src/compiler/utilities.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ namespace ts {
1616

1717
export function getDeclarationOfKind(symbol: Symbol, kind: SyntaxKind): Declaration {
1818
let declarations = symbol.declarations;
19-
for (let declaration of declarations) {
20-
if (declaration.kind === kind) {
21-
return declaration;
19+
if (declarations) {
20+
for (let declaration of declarations) {
21+
if (declaration.kind === kind) {
22+
return declaration;
23+
}
2224
}
2325
}
2426

src/services/services.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,34 +2801,25 @@ namespace ts {
28012801
}
28022802

28032803
/// Completion
2804-
function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean): string {
2805-
let displayName = symbol.getName();
2806-
if (displayName) {
2807-
// If this is the default export, get the name of the declaration if it exists
2808-
if (displayName === "default") {
2809-
let localSymbol = getLocalSymbolForExportDefault(symbol);
2810-
if (localSymbol && localSymbol.name) {
2811-
displayName = symbol.valueDeclaration.localSymbol.name;
2812-
}
2813-
}
2814-
2815-
// Special case for function expression and class expression because despite sometimes having a name, the binder
2816-
// binds them to a symbol with the name "__function" and "__class" respectively. However, for completion entry, we want
2817-
// to display its declared name rather than "__function" and "__class".
2818-
// var x = function foo () {
2819-
// fo$ <- completion list should contain local name "foo"
2820-
// }
2821-
// foo$ <- completion list should not contain "foo"
2822-
// TODO (yuisu): Use getDeclaredName instead once the functions is rewritten
2823-
if (displayName === "__function" || displayName === "__class") {
2824-
displayName = symbol.declarations[0].name.getText();
2825-
2826-
// At this point, we expect that all completion list entries have declared name including function expression
2827-
// because when we gather all relevant symbols, we check that the function expression must have declared name
2828-
// before adding the symbol into our symbols table. (see: getSymbolsInScope)
2829-
Debug.assert(displayName !== undefined,"Expected this function expression to have declared name");
2830-
}
2831-
2804+
function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean, location: Node): string {
2805+
let displayName: string;
2806+
2807+
// In the case of default export, function expression and class expression,
2808+
// the binder bind them with "default", "__function", "__class" respectively.
2809+
// However, for completion entry, we want to display its declared name rather than binder name.
2810+
if (getLocalSymbolForExportDefault(symbol) ||
2811+
getDeclarationOfKind(symbol, SyntaxKind.FunctionExpression) ||
2812+
getDeclarationOfKind(symbol, SyntaxKind.ClassExpression)) {
2813+
let typeChecker = program.getTypeChecker();
2814+
displayName = getDeclaredName(typeChecker, symbol, location);
2815+
2816+
// At this point, we expect that all completion list entries have declared name including function expression and class expression
2817+
// because when we gather all relevant symbols, we check that the function expression and class expression must have declared name
2818+
// before adding the symbol into our symbols table. (see: getSymbolsInScope)
2819+
Debug.assert(displayName !== undefined, "Expected displayed name from declaration to existed in this symbol: " + symbol.getName());
2820+
}
2821+
else {
2822+
displayName = symbol.getName();
28322823
let firstCharCode = displayName.charCodeAt(0);
28332824
// First check of the displayName is not external module; if it is an external module, it is not valid entry
28342825
if ((symbol.flags & SymbolFlags.Namespace) && (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) {
@@ -3534,7 +3525,7 @@ namespace ts {
35343525
// Try to get a valid display name for this symbol, if we could not find one, then ignore it.
35353526
// We would like to only show things that can be added after a dot, so for instance numeric properties can
35363527
// not be accessed with a dot (a.1 <- invalid)
3537-
let displayName = getCompletionEntryDisplayNameForSymbol(symbol, program.getCompilerOptions().target, /*performCharacterChecks:*/ true);
3528+
let displayName = getCompletionEntryDisplayNameForSymbol(symbol, program.getCompilerOptions().target, /*performCharacterChecks:*/ true, location);
35383529
if (!displayName) {
35393530
return undefined;
35403531
}
@@ -3591,7 +3582,7 @@ namespace ts {
35913582
// We don't need to perform character checks here because we're only comparing the
35923583
// name against 'entryName' (which is known to be good), not building a new
35933584
// completion entry.
3594-
let symbol = forEach(symbols, s => getCompletionEntryDisplayNameForSymbol(s, target, /*performCharacterChecks:*/ false) === entryName ? s : undefined);
3585+
let symbol = forEach(symbols, s => getCompletionEntryDisplayNameForSymbol(s, target, /*performCharacterChecks:*/ false, location) === entryName ? s : undefined);
35953586

35963587
if (symbol) {
35973588
let { displayParts, documentation, symbolKind } = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getValidSourceFile(fileName), location, location, SemanticMeaning.All);

0 commit comments

Comments
 (0)