Skip to content

Commit 540ae01

Browse files
author
Yui T
committed
Address PR
1 parent 4759cdb commit 540ae01

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/compiler/emitter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,6 +3889,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
38893889
// We create a synthetic copy of the identifier in order to avoid the rewriting that might
38903890
// otherwise occur when the identifier is emitted.
38913891
index = <Identifier | LiteralExpression>createSynthesizedNode(propName.kind);
3892+
// We need to escape identifier here because when parsing an identifier prefixing with "__"
3893+
// the parser need to append "_" in order to escape colliding with magic identifiers such as "__proto__"
3894+
// Therefore, in order to correctly emit identifiers that are written in original TypeScript file,
3895+
// we will unescapeIdentifier to remove additional underscore (if no underscore is added, the function will return original input string)
38923896
(<Identifier | LiteralExpression>index).text = unescapeIdentifier((<Identifier | LiteralExpression>propName).text);
38933897
}
38943898

src/compiler/utilities.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,10 @@ namespace ts {
230230
return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia);
231231
}
232232

233-
function prefixWithUnderscoreUnderscore(identifier: string): boolean {
234-
if (identifier.length <= 2) {
235-
return false;
236-
}
237-
else {
238-
return identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? true : false;
239-
}
240-
}
241-
242233
// Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__'
243234
export function escapeIdentifier(identifier: string): string {
244-
return prefixWithUnderscoreUnderscore(identifier) ? "_" + identifier : identifier;
235+
const prefixWithUnderscore = identifier.length > 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._;
236+
return prefixWithUnderscore ? "_" + identifier : identifier;
245237
}
246238

247239
// Remove extra underscore from escaped identifier

0 commit comments

Comments
 (0)