From 540ae01015a4c6b9438883cfd71e119aa46f3308 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 21 Dec 2015 17:07:18 -0800 Subject: [PATCH] Address PR --- src/compiler/emitter.ts | 4 ++++ src/compiler/utilities.ts | 12 ++---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 512ab883e17..f015af92a9f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3889,6 +3889,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge // We create a synthetic copy of the identifier in order to avoid the rewriting that might // otherwise occur when the identifier is emitted. index = createSynthesizedNode(propName.kind); + // We need to escape identifier here because when parsing an identifier prefixing with "__" + // the parser need to append "_" in order to escape colliding with magic identifiers such as "__proto__" + // Therefore, in order to correctly emit identifiers that are written in original TypeScript file, + // we will unescapeIdentifier to remove additional underscore (if no underscore is added, the function will return original input string) (index).text = unescapeIdentifier((propName).text); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2ea1b989eba..818b24ca8fd 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -230,18 +230,10 @@ 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__' export function escapeIdentifier(identifier: string): string { - return prefixWithUnderscoreUnderscore(identifier) ? "_" + identifier : identifier; + const prefixWithUnderscore = identifier.length > 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._; + return prefixWithUnderscore ? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier