From 7747ad4aa586e56d463da389448480c9ec89ecdb Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 1 Jul 2015 14:25:43 -0700 Subject: [PATCH] Clean up stripQuote and add comments --- src/services/services.ts | 36 ++++++++++++++++++++---------------- src/services/utilities.ts | 9 ++++++++- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 6e2698c9e6c..c1c0aee33af 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2800,7 +2800,11 @@ namespace ts { return program.getOptionsDiagnostics().concat(program.getGlobalDiagnostics()); } - /// Completion + /** + * Get the name to be display in completion from a given symbol. + * + * @return undefined if the name is of external module otherwise a name with striped of any quote + */ function getCompletionEntryDisplayNameForSymbol(symbol: Symbol, target: ScriptTarget, performCharacterChecks: boolean, location: Node): string { let displayName: string; @@ -2832,37 +2836,37 @@ namespace ts { return getCompletionEntryDisplayName(displayName, target, performCharacterChecks); } - function getCompletionEntryDisplayName(displayName: string, target: ScriptTarget, performCharacterChecks: boolean): string { - if (!displayName) { + /** + * Get a displayName from a given for completion list, performing any necessary quotes stripping + * and checking whether the name is valid identifier name. + */ + function getCompletionEntryDisplayName(name: string, target: ScriptTarget, performCharacterChecks: boolean): string { + if (!name) { return undefined; } - let firstCharCode = displayName.charCodeAt(0); - if (displayName.length >= 2 && - firstCharCode === displayName.charCodeAt(displayName.length - 1) && - (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) { - // If the user entered name for the symbol was quoted, removing the quotes is not enough, as the name could be an - // invalid identifier name. We need to check if whatever was inside the quotes is actually a valid identifier name. - displayName = displayName.substring(1, displayName.length - 1); - } + name = stripQuotes(name); - if (!displayName) { + // We can simply return name with strip quotes because the name could be an invalid identifier name + // e.g "b a" is valid quoted name but when we strip off the quotes, it is invalid. + // We, thus, need to check if whatever was inside the quotes is actually a valid identifier name. + if (!name) { return undefined; } if (performCharacterChecks) { - if (!isIdentifierStart(displayName.charCodeAt(0), target)) { + if (!isIdentifierStart(name.charCodeAt(0), target)) { return undefined; } - for (let i = 1, n = displayName.length; i < n; i++) { - if (!isIdentifierPart(displayName.charCodeAt(i), target)) { + for (let i = 1, n = name.length; i < n; i++) { + if (!isIdentifierPart(name.charCodeAt(i), target)) { return undefined; } } } - return unescapeIdentifier(displayName); + return unescapeIdentifier(name); } function getCompletionData(fileName: string, position: number) { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 0fd945caa7d..797cbee12cb 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -675,9 +675,16 @@ namespace ts { (location.parent).propertyName === location; } + /** + * Strip off existed single quotes or double quotes from a given string + * + * @return non-quoted string + */ export function stripQuotes(name: string) { let length = name.length; - if (length >= 2 && name.charCodeAt(0) === CharacterCodes.doubleQuote && name.charCodeAt(length - 1) === CharacterCodes.doubleQuote) { + if (length >= 2 && + name.charCodeAt(0) === name.charCodeAt(length - 1) && + (name.charCodeAt(0) === CharacterCodes.doubleQuote || name.charCodeAt(0) === CharacterCodes.singleQuote)) { return name.substring(1, length - 1); }; return name;