Add helper functions to simplify getCompletionEntryDisplayNameForSymbol (#20552)

This commit is contained in:
Andy 2018-01-08 11:48:25 -08:00 committed by GitHub
parent fd5ed5ac79
commit 20c846d671
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 29 deletions

View File

@ -8128,7 +8128,7 @@ namespace ts {
}
function getLiteralTypeFromPropertyName(prop: Symbol) {
return getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier || startsWith(prop.escapedName as string, "__@") ?
return getDeclarationModifierFlagsFromSymbol(prop) & ModifierFlags.NonPublicAccessibilityModifier || isKnownSymbol(prop) ?
neverType :
getLiteralType(symbolName(prop));
}

View File

@ -2119,6 +2119,10 @@ namespace ts {
return "__@" + symbolName as __String;
}
export function isKnownSymbol(symbol: Symbol): boolean {
return startsWith(symbol.escapedName as string, "__@");
}
/**
* Includes the word "Symbol" with unicode escapes
*/

View File

@ -1988,36 +1988,17 @@ namespace ts.Completions {
/**
* Get the name to be display in completion from a given symbol.
*
* @return undefined if the name is of external module
*/
function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean, allowStringLiteral: boolean, origin: SymbolOriginInfo | undefined): string | undefined {
const name = getSymbolName(symbol, origin, target);
if (!name) return undefined;
// 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) {
const firstCharCode = name.charCodeAt(0);
if (isSingleOrDoubleQuote(firstCharCode)) {
// If the symbol is external module, don't show it in the completion list
// (i.e declare module "http" { const x; } | // <= request completion here, "http" should not be there)
return undefined;
}
}
// If the symbol is for a member of an object type and is the internal name of an ES
// symbol, it is not a valid entry. Internal names for ES symbols start with "__@"
if (symbol.flags & SymbolFlags.ClassMember) {
const escapedName = symbol.escapedName as string;
if (escapedName.length >= 3 &&
escapedName.charCodeAt(0) === CharacterCodes._ &&
escapedName.charCodeAt(1) === CharacterCodes._ &&
escapedName.charCodeAt(2) === CharacterCodes.at) {
return undefined;
}
}
return getCompletionEntryDisplayName(name, target, performCharacterChecks, allowStringLiteral);
return name === undefined
// If the symbol is external module, don't show it in the completion list
// (i.e declare module "http" { const x; } | // <= request completion here, "http" should not be there)
|| symbol.flags & SymbolFlags.Module && startsWithQuote(name)
// If the symbol is the internal name of an ES symbol, it is not a valid entry. Internal names for ES symbols start with "__@"
|| isKnownSymbol(symbol)
? undefined
: getCompletionEntryDisplayName(name, target, performCharacterChecks, allowStringLiteral);
}
/**

View File

@ -1300,12 +1300,16 @@ namespace ts {
*/
export function stripQuotes(name: string) {
const length = name.length;
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && isSingleOrDoubleQuote(name.charCodeAt(0))) {
if (length >= 2 && name.charCodeAt(0) === name.charCodeAt(length - 1) && startsWithQuote(name)) {
return name.substring(1, length - 1);
}
return name;
}
export function startsWithQuote(name: string): boolean {
return isSingleOrDoubleQuote(name.charCodeAt(0));
}
export function scriptKindIs(fileName: string, host: LanguageServiceHost, ...scriptKinds: ScriptKind[]): boolean {
const scriptKind = getScriptKind(fileName, host);
return forEach(scriptKinds, k => k === scriptKind);