Clean up stripQuote and add comments

This commit is contained in:
Yui T 2015-07-01 14:25:43 -07:00
parent 605ab0b1fc
commit 7747ad4aa5
2 changed files with 28 additions and 17 deletions

View File

@ -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) {

View File

@ -675,9 +675,16 @@ namespace ts {
(<ImportOrExportSpecifier>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;