Skip to content

Commit 7747ad4

Browse files
author
Yui T
committed
Clean up stripQuote and add comments
1 parent 605ab0b commit 7747ad4

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/services/services.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,11 @@ namespace ts {
28002800
return program.getOptionsDiagnostics().concat(program.getGlobalDiagnostics());
28012801
}
28022802

2803-
/// Completion
2803+
/**
2804+
* Get the name to be display in completion from a given symbol.
2805+
*
2806+
* @return undefined if the name is of external module otherwise a name with striped of any quote
2807+
*/
28042808
function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean, location: Node): string {
28052809
let displayName: string;
28062810

@@ -2832,37 +2836,37 @@ namespace ts {
28322836
return getCompletionEntryDisplayName(displayName, target, performCharacterChecks);
28332837
}
28342838

2835-
function getCompletionEntryDisplayName(displayName: string, target: ScriptTarget, performCharacterChecks: boolean): string {
2836-
if (!displayName) {
2839+
/**
2840+
* Get a displayName from a given for completion list, performing any necessary quotes stripping
2841+
* and checking whether the name is valid identifier name.
2842+
*/
2843+
function getCompletionEntryDisplayName(name: string, target: ScriptTarget, performCharacterChecks: boolean): string {
2844+
if (!name) {
28372845
return undefined;
28382846
}
28392847

2840-
let firstCharCode = displayName.charCodeAt(0);
2841-
if (displayName.length >= 2 &&
2842-
firstCharCode === displayName.charCodeAt(displayName.length - 1) &&
2843-
(firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) {
2844-
// If the user entered name for the symbol was quoted, removing the quotes is not enough, as the name could be an
2845-
// invalid identifier name. We need to check if whatever was inside the quotes is actually a valid identifier name.
2846-
displayName = displayName.substring(1, displayName.length - 1);
2847-
}
2848+
name = stripQuotes(name);
28482849

2849-
if (!displayName) {
2850+
// We can simply return name with strip quotes because the name could be an invalid identifier name
2851+
// e.g "b a" is valid quoted name but when we strip off the quotes, it is invalid.
2852+
// We, thus, need to check if whatever was inside the quotes is actually a valid identifier name.
2853+
if (!name) {
28502854
return undefined;
28512855
}
28522856

28532857
if (performCharacterChecks) {
2854-
if (!isIdentifierStart(displayName.charCodeAt(0), target)) {
2858+
if (!isIdentifierStart(name.charCodeAt(0), target)) {
28552859
return undefined;
28562860
}
28572861

2858-
for (let i = 1, n = displayName.length; i < n; i++) {
2859-
if (!isIdentifierPart(displayName.charCodeAt(i), target)) {
2862+
for (let i = 1, n = name.length; i < n; i++) {
2863+
if (!isIdentifierPart(name.charCodeAt(i), target)) {
28602864
return undefined;
28612865
}
28622866
}
28632867
}
28642868

2865-
return unescapeIdentifier(displayName);
2869+
return unescapeIdentifier(name);
28662870
}
28672871

28682872
function getCompletionData(fileName: string, position: number) {

src/services/utilities.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,16 @@ namespace ts {
675675
(<ImportOrExportSpecifier>location.parent).propertyName === location;
676676
}
677677

678+
/**
679+
* Strip off existed single quotes or double quotes from a given string
680+
*
681+
* @return non-quoted string
682+
*/
678683
export function stripQuotes(name: string) {
679684
let length = name.length;
680-
if (length >= 2 && name.charCodeAt(0) === CharacterCodes.doubleQuote && name.charCodeAt(length - 1) === CharacterCodes.doubleQuote) {
685+
if (length >= 2 &&
686+
name.charCodeAt(0) === name.charCodeAt(length - 1) &&
687+
(name.charCodeAt(0) === CharacterCodes.doubleQuote || name.charCodeAt(0) === CharacterCodes.singleQuote)) {
681688
return name.substring(1, length - 1);
682689
};
683690
return name;

0 commit comments

Comments
 (0)