Skip to content

Commit

Permalink
Add helper functions to simplify getCompletionEntryDisplayNameForSymb…
Browse files Browse the repository at this point in the history
…ol (#20552)
  • Loading branch information
Andy authored Jan 8, 2018
1 parent fd5ed5a commit 20c846d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
35 changes: 8 additions & 27 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 20c846d

Please sign in to comment.