diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 4bf1422e888..a32cbc505a3 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -230,9 +230,19 @@ namespace ts { return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); } + function prefixWithUnderscoreUnderscore(identifier: string): boolean { + if (identifier.length <= 2) { + return false; + } + else { + return identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? true : false; + } + } + // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' + // TODO(yuisu): comment export function escapeIdentifier(identifier: string): string { - return identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? "_" + identifier : identifier; + return prefixWithUnderscoreUnderscore(identifier)? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier