Skip to content

Commit 20c846d

Browse files
author
Andy
authored
Add helper functions to simplify getCompletionEntryDisplayNameForSymbol (#20552)
1 parent fd5ed5a commit 20c846d

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8128,7 +8128,7 @@ namespace ts {
81288128
}
81298129

81308130
function getLiteralTypeFromPropertyName(prop: Symbol) {
8131-
return getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier || startsWith(prop.escapedName as string, "__@") ?
8131+
return getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier || isKnownSymbol(prop) ?
81328132
neverType :
81338133
getLiteralType(symbolName(prop));
81348134
}

src/compiler/utilities.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,10 @@ namespace ts {
21192119
return "__@" + symbolName as __String;
21202120
}
21212121

2122+
export function isKnownSymbol(symbol: Symbol): boolean {
2123+
return startsWith(symbol.escapedName as string, "__@");
2124+
}
2125+
21222126
/**
21232127
* Includes the word "Symbol" with unicode escapes
21242128
*/

src/services/completions.ts

+8-27
Original file line numberDiff line numberDiff line change
@@ -1988,36 +1988,17 @@ namespace ts.Completions {
19881988

19891989
/**
19901990
* Get the name to be display in completion from a given symbol.
1991-
*
1992-
* @return undefined if the name is of external module
19931991
*/
19941992
function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean, allowStringLiteral: boolean, origin: SymbolOriginInfo | undefined): string | undefined {
19951993
const name = getSymbolName(symbol, origin, target);
1996-
if (!name) return undefined;
1997-
1998-
// First check of the displayName is not external module; if it is an external module, it is not valid entry
1999-
if (symbol.flags & SymbolFlags.Namespace) {
2000-
const firstCharCode = name.charCodeAt(0);
2001-
if (isSingleOrDoubleQuote(firstCharCode)) {
2002-
// If the symbol is external module, don't show it in the completion list
2003-
// (i.e declare module "http" { const x; } | // <= request completion here, "http" should not be there)
2004-
return undefined;
2005-
}
2006-
}
2007-
2008-
// If the symbol is for a member of an object type and is the internal name of an ES
2009-
// symbol, it is not a valid entry. Internal names for ES symbols start with "__@"
2010-
if (symbol.flags & SymbolFlags.ClassMember) {
2011-
const escapedName = symbol.escapedName as string;
2012-
if (escapedName.length >= 3 &&
2013-
escapedName.charCodeAt(0) === CharacterCodes._ &&
2014-
escapedName.charCodeAt(1) === CharacterCodes._ &&
2015-
escapedName.charCodeAt(2) === CharacterCodes.at) {
2016-
return undefined;
2017-
}
2018-
}
2019-
2020-
return getCompletionEntryDisplayName(name, target, performCharacterChecks, allowStringLiteral);
1994+
return name === undefined
1995+
// If the symbol is external module, don't show it in the completion list
1996+
// (i.e declare module "http" { const x; } | // <= request completion here, "http" should not be there)
1997+
|| symbol.flags & SymbolFlags.Module && startsWithQuote(name)
1998+
// If the symbol is the internal name of an ES symbol, it is not a valid entry. Internal names for ES symbols start with "__@"
1999+
|| isKnownSymbol(symbol)
2000+
? undefined
2001+
: getCompletionEntryDisplayName(name, target, performCharacterChecks, allowStringLiteral);
20212002
}
20222003

20232004
/**

src/services/utilities.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1300,12 +1300,16 @@ namespace ts {
13001300
*/
13011301
export function stripQuotes(name: string) {
13021302
const length = name.length;
1303-
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && isSingleOrDoubleQuote(name.charCodeAt(0))) {
1303+
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && startsWithQuote(name)) {
13041304
return name.substring(1, length - 1);
13051305
}
13061306
return name;
13071307
}
13081308

1309+
export function startsWithQuote(name: string): boolean {
1310+
return isSingleOrDoubleQuote(name.charCodeAt(0));
1311+
}
1312+
13091313
export function scriptKindIs(fileName: string, host: LanguageServiceHost, ...scriptKinds: ScriptKind[]): boolean {
13101314
const scriptKind = getScriptKind(fileName, host);
13111315
return forEach(scriptKinds, k => k === scriptKind);

0 commit comments

Comments
 (0)