Skip to content

Commit 7a539d0

Browse files
committed
Identifier escaping/unescaping for unique names
1 parent a72abc8 commit 7a539d0

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

src/compiler/emitter.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ namespace ts {
25652565
// Node names generate unique names based on their original node
25662566
// and are cached based on that node's id.
25672567
const node = getNodeForGeneratedName(name);
2568-
return generateNameCached(node, getTextOfNode);
2568+
return generateNameCached(node);
25692569
}
25702570
else {
25712571
// Auto, Loop, and Unique names are cached based on their unique
@@ -2575,9 +2575,9 @@ namespace ts {
25752575
}
25762576
}
25772577

2578-
function generateNameCached(node: Node, getTextOfNode: (node: Node, includeTrivia?: boolean) => string) {
2578+
function generateNameCached(node: Node) {
25792579
const nodeId = getNodeId(node);
2580-
return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = unescapeIdentifier(generateNameForNode(node, getTextOfNode)));
2580+
return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = unescapeIdentifier(generateNameForNode(node)));
25812581
}
25822582

25832583
/**
@@ -2659,7 +2659,7 @@ namespace ts {
26592659
/**
26602660
* Generates a unique name for a ModuleDeclaration or EnumDeclaration.
26612661
*/
2662-
function generateNameForModuleOrEnum(node: ModuleDeclaration | EnumDeclaration, getTextOfNode: (node: Node, includeTrivia?: boolean) => string) {
2662+
function generateNameForModuleOrEnum(node: ModuleDeclaration | EnumDeclaration) {
26632663
const name = getTextOfNode(node.name);
26642664
// Use module/enum name itself if it is unique, otherwise make a unique variation
26652665
return isUniqueLocalName(name, node) ? name : makeUniqueName(name);
@@ -2689,23 +2689,23 @@ namespace ts {
26892689
return makeUniqueName("class");
26902690
}
26912691

2692-
function generateNameForMethodOrAccessor(node: MethodDeclaration | AccessorDeclaration, getTextOfNode: (node: Node, includeTrivia?: boolean) => string) {
2692+
function generateNameForMethodOrAccessor(node: MethodDeclaration | AccessorDeclaration) {
26932693
if (isIdentifier(node.name)) {
2694-
return generateNameCached(node.name, getTextOfNode);
2694+
return generateNameCached(node.name);
26952695
}
26962696
return makeTempVariableName(TempFlags.Auto);
26972697
}
26982698

26992699
/**
27002700
* Generates a unique name from a node.
27012701
*/
2702-
function generateNameForNode(node: Node, getTextOfNode: (node: Node, includeTrivia?: boolean) => string): string {
2702+
function generateNameForNode(node: Node): string {
27032703
switch (node.kind) {
27042704
case SyntaxKind.Identifier:
27052705
return makeUniqueName(getTextOfNode(node));
27062706
case SyntaxKind.ModuleDeclaration:
27072707
case SyntaxKind.EnumDeclaration:
2708-
return generateNameForModuleOrEnum(<ModuleDeclaration | EnumDeclaration>node, getTextOfNode);
2708+
return generateNameForModuleOrEnum(<ModuleDeclaration | EnumDeclaration>node);
27092709
case SyntaxKind.ImportDeclaration:
27102710
case SyntaxKind.ExportDeclaration:
27112711
return generateNameForImportOrExportDeclaration(<ImportDeclaration | ExportDeclaration>node);
@@ -2718,7 +2718,7 @@ namespace ts {
27182718
case SyntaxKind.MethodDeclaration:
27192719
case SyntaxKind.GetAccessor:
27202720
case SyntaxKind.SetAccessor:
2721-
return generateNameForMethodOrAccessor(<MethodDeclaration | AccessorDeclaration>node, getTextOfNode);
2721+
return generateNameForMethodOrAccessor(<MethodDeclaration | AccessorDeclaration>node);
27222722
default:
27232723
return makeTempVariableName(TempFlags.Auto);
27242724
}
@@ -2734,7 +2734,7 @@ namespace ts {
27342734
case GeneratedIdentifierKind.Loop:
27352735
return makeTempVariableName(TempFlags._i);
27362736
case GeneratedIdentifierKind.Unique:
2737-
return makeUniqueName(name.text);
2737+
return makeUniqueName(unescapeIdentifier(name.text));
27382738
}
27392739

27402740
Debug.fail("Unsupported GeneratedIdentifierKind.");

src/compiler/factory.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace ts {
108108

109109
export function createIdentifier(text: string): Identifier {
110110
const node = <Identifier>createSynthesizedNode(SyntaxKind.Identifier);
111-
node.text = text ? escapeIdentifier(text) : undefined;
111+
node.text = escapeIdentifier(text);
112112
node.originalKeywordKind = text ? stringToToken(text) : SyntaxKind.Unknown;
113113
node.autoGenerateKind = GeneratedIdentifierKind.None;
114114
node.autoGenerateId = 0;
@@ -140,8 +140,7 @@ namespace ts {
140140

141141
/** Create a unique name based on the supplied text. */
142142
export function createUniqueName(text: string): Identifier {
143-
const name = createIdentifier("");
144-
name.text = text;
143+
const name = createIdentifier(text);
145144
name.autoGenerateKind = GeneratedIdentifierKind.Unique;
146145
name.autoGenerateId = nextAutoGenerateId;
147146
nextAutoGenerateId++;

src/compiler/transformers/es2015.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,7 +2251,7 @@ namespace ts {
22512251
// we don't want to emit a temporary variable for the RHS, just use it directly.
22522252
const counter = createLoopVariable();
22532253
const rhsReference = expression.kind === SyntaxKind.Identifier
2254-
? createUniqueName((<Identifier>expression).text)
2254+
? createUniqueName(unescapeIdentifier((<Identifier>expression).text))
22552255
: createTempVariable(/*recordTempVariable*/ undefined);
22562256
const elementAccess = createElementAccess(rhsReference, counter);
22572257

@@ -2872,7 +2872,7 @@ namespace ts {
28722872
else {
28732873
loopParameters.push(createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, name));
28742874
if (resolver.getNodeCheckFlags(decl) & NodeCheckFlags.NeedsLoopOutParameter) {
2875-
const outParamName = createUniqueName("out_" + name.text);
2875+
const outParamName = createUniqueName("out_" + unescapeIdentifier(name.text));
28762876
loopOutParameters.push({ originalName: name, outParamName });
28772877
}
28782878
}

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3119,7 +3119,7 @@ namespace ts {
31193119
function getClassAliasIfNeeded(node: ClassDeclaration) {
31203120
if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.ClassWithConstructorReference) {
31213121
enableSubstitutionForClassAliases();
3122-
const classAlias = createUniqueName(node.name && !isGeneratedIdentifier(node.name) ? node.name.text : "default");
3122+
const classAlias = createUniqueName(node.name && !isGeneratedIdentifier(node.name) ? unescapeIdentifier(node.name.text) : "default");
31233123
classAliases[getOriginalNodeId(node)] = classAlias;
31243124
hoistVariableDeclaration(classAlias);
31253125
return classAlias;

0 commit comments

Comments
 (0)