Skip to content

Commit 44e8244

Browse files
authored
Move auto-generated identifier info into separate object (microsoft#51900)
1 parent fe18527 commit 44e8244

File tree

10 files changed

+61
-59
lines changed

10 files changed

+61
-59
lines changed

src/compiler/emitter.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5628,15 +5628,15 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
56285628
* Generate the text for a generated identifier.
56295629
*/
56305630
function generateName(name: GeneratedIdentifier | GeneratedPrivateIdentifier) {
5631-
if ((name.autoGenerateFlags & GeneratedIdentifierFlags.KindMask) === GeneratedIdentifierFlags.Node) {
5631+
if ((name.autoGenerate.flags & GeneratedIdentifierFlags.KindMask) === GeneratedIdentifierFlags.Node) {
56325632
// Node names generate unique names based on their original node
56335633
// and are cached based on that node's id.
5634-
return generateNameCached(getNodeForGeneratedName(name), isPrivateIdentifier(name), name.autoGenerateFlags, name.autoGeneratePrefix, name.autoGenerateSuffix);
5634+
return generateNameCached(getNodeForGeneratedName(name), isPrivateIdentifier(name), name.autoGenerate.flags, name.autoGenerate.prefix, name.autoGenerate.suffix);
56355635
}
56365636
else {
56375637
// Auto, Loop, and Unique names are cached based on their unique
56385638
// autoGenerateId.
5639-
const autoGenerateId = name.autoGenerateId!;
5639+
const autoGenerateId = name.autoGenerate.id;
56405640
return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = makeName(name));
56415641
}
56425642
}
@@ -5889,27 +5889,27 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
58895889
* Generates a unique identifier for a node.
58905890
*/
58915891
function makeName(name: GeneratedIdentifier | GeneratedPrivateIdentifier) {
5892-
const prefix = formatGeneratedNamePart(name.autoGeneratePrefix, generateName);
5893-
const suffix = formatGeneratedNamePart (name.autoGenerateSuffix);
5894-
switch (name.autoGenerateFlags & GeneratedIdentifierFlags.KindMask) {
5892+
const prefix = formatGeneratedNamePart(name.autoGenerate.prefix, generateName);
5893+
const suffix = formatGeneratedNamePart (name.autoGenerate.suffix);
5894+
switch (name.autoGenerate.flags & GeneratedIdentifierFlags.KindMask) {
58955895
case GeneratedIdentifierFlags.Auto:
5896-
return makeTempVariableName(TempFlags.Auto, !!(name.autoGenerateFlags & GeneratedIdentifierFlags.ReservedInNestedScopes), isPrivateIdentifier(name), prefix, suffix);
5896+
return makeTempVariableName(TempFlags.Auto, !!(name.autoGenerate.flags & GeneratedIdentifierFlags.ReservedInNestedScopes), isPrivateIdentifier(name), prefix, suffix);
58975897
case GeneratedIdentifierFlags.Loop:
58985898
Debug.assertNode(name, isIdentifier);
5899-
return makeTempVariableName(TempFlags._i, !!(name.autoGenerateFlags & GeneratedIdentifierFlags.ReservedInNestedScopes), /*privateName*/ false, prefix, suffix);
5899+
return makeTempVariableName(TempFlags._i, !!(name.autoGenerate.flags & GeneratedIdentifierFlags.ReservedInNestedScopes), /*privateName*/ false, prefix, suffix);
59005900
case GeneratedIdentifierFlags.Unique:
59015901
return makeUniqueName(
59025902
idText(name),
5903-
(name.autoGenerateFlags & GeneratedIdentifierFlags.FileLevel) ? isFileLevelUniqueName : isUniqueName,
5904-
!!(name.autoGenerateFlags & GeneratedIdentifierFlags.Optimistic),
5905-
!!(name.autoGenerateFlags & GeneratedIdentifierFlags.ReservedInNestedScopes),
5903+
(name.autoGenerate.flags & GeneratedIdentifierFlags.FileLevel) ? isFileLevelUniqueName : isUniqueName,
5904+
!!(name.autoGenerate.flags & GeneratedIdentifierFlags.Optimistic),
5905+
!!(name.autoGenerate.flags & GeneratedIdentifierFlags.ReservedInNestedScopes),
59065906
isPrivateIdentifier(name),
59075907
prefix,
59085908
suffix
59095909
);
59105910
}
59115911

5912-
return Debug.fail(`Unsupported GeneratedIdentifierKind: ${Debug.formatEnum(name.autoGenerateFlags & GeneratedIdentifierFlags.KindMask, (ts as any).GeneratedIdentifierFlags, /*isFlags*/ true)}.`);
5912+
return Debug.fail(`Unsupported GeneratedIdentifierKind: ${Debug.formatEnum(name.autoGenerate.flags & GeneratedIdentifierFlags.KindMask, (ts as any).GeneratedIdentifierFlags, /*isFlags*/ true)}.`);
59135913
}
59145914

59155915
// Comments

src/compiler/factory/nodeFactory.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,16 +1154,24 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
11541154
const node = baseFactory.createBaseIdentifierNode(SyntaxKind.Identifier) as Mutable<Identifier>;
11551155
node.originalKeywordKind = originalKeywordKind;
11561156
node.escapedText = escapedText;
1157-
node.autoGenerateFlags = GeneratedIdentifierFlags.None;
1157+
node.autoGenerate = undefined;
1158+
node.typeArguments = undefined;
1159+
node.hasExtendedUnicodeEscape = undefined;
1160+
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
1161+
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
1162+
node.flowNode = undefined; // initialized by binder (FlowContainer)
1163+
node.symbol = undefined!; // initialized by checker
11581164
return node;
11591165
}
11601166

11611167
function createBaseGeneratedIdentifier(text: string, autoGenerateFlags: GeneratedIdentifierFlags, prefix: string | GeneratedNamePart | undefined, suffix: string | undefined) {
11621168
const node = createBaseIdentifier(escapeLeadingUnderscores(text), /*originalKeywordKind*/ undefined) as Mutable<GeneratedIdentifier>;
1163-
node.autoGenerateFlags = autoGenerateFlags;
1164-
node.autoGenerateId = nextAutoGenerateId;
1165-
node.autoGeneratePrefix = prefix;
1166-
node.autoGenerateSuffix = suffix;
1169+
node.autoGenerate = {
1170+
flags: autoGenerateFlags,
1171+
id: nextAutoGenerateId,
1172+
prefix,
1173+
suffix
1174+
};
11671175
nextAutoGenerateId++;
11681176
return node;
11691177
}
@@ -1180,10 +1188,6 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
11801188
const node = createBaseIdentifier(escapeLeadingUnderscores(text), originalKeywordKind);
11811189
node.typeArguments = asNodeArray(typeArguments);
11821190
node.hasExtendedUnicodeEscape = hasExtendedUnicodeEscape;
1183-
node.jsDoc = undefined; // initialized by parser (JsDocContainer)
1184-
node.jsDocCache = undefined; // initialized by parser (JsDocContainer)
1185-
node.flowNode = undefined; // initialized by binder (FlowContainer)
1186-
node.symbol = undefined!; // initialized by checker
11871191

11881192
// NOTE: we do not include transform flags of typeArguments in an identifier as they do not contribute to transformations
11891193
if (node.originalKeywordKind === SyntaxKind.AwaitKeyword) {
@@ -1246,7 +1250,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
12461250
function createBasePrivateIdentifier(escapedText: __String) {
12471251
const node = baseFactory.createBasePrivateIdentifierNode(SyntaxKind.PrivateIdentifier) as Mutable<PrivateIdentifier>;
12481252
node.escapedText = escapedText;
1249-
node.autoGenerateFlags = GeneratedIdentifierFlags.None;
1253+
node.autoGenerate = undefined;
12501254
node.transformFlags |= TransformFlags.ContainsClassFields;
12511255
return node;
12521256
}
@@ -1259,10 +1263,12 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
12591263

12601264
function createBaseGeneratedPrivateIdentifier(text: string, autoGenerateFlags: GeneratedIdentifierFlags, prefix: string | GeneratedNamePart | undefined, suffix: string | undefined) {
12611265
const node = createBasePrivateIdentifier(escapeLeadingUnderscores(text));
1262-
node.autoGenerateFlags = autoGenerateFlags;
1263-
node.autoGenerateId = nextAutoGenerateId;
1264-
node.autoGeneratePrefix = prefix;
1265-
node.autoGenerateSuffix = suffix;
1266+
node.autoGenerate = {
1267+
flags: autoGenerateFlags,
1268+
id: nextAutoGenerateId,
1269+
prefix,
1270+
suffix,
1271+
};
12661272
nextAutoGenerateId++;
12671273
return node;
12681274
}
@@ -6374,10 +6380,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
63746380
function cloneGeneratedIdentifier(node: GeneratedIdentifier): GeneratedIdentifier {
63756381
const clone = createBaseIdentifier(node.escapedText, /*originalKeywordKind*/ undefined) as Mutable<GeneratedIdentifier>;
63766382
clone.flags |= node.flags & ~NodeFlags.Synthesized;
6377-
clone.autoGenerateFlags = node.autoGenerateFlags;
6378-
clone.autoGenerateId = node.autoGenerateId;
6379-
clone.autoGeneratePrefix = node.autoGeneratePrefix;
6380-
clone.autoGenerateSuffix = node.autoGenerateSuffix;
6383+
clone.autoGenerate = { ...node.autoGenerate };
63816384
clone.transformFlags = node.transformFlags;
63826385
setOriginalNode(clone, node);
63836386
return clone;
@@ -6400,10 +6403,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
64006403
function cloneGeneratedPrivateIdentifier(node: GeneratedPrivateIdentifier): GeneratedPrivateIdentifier {
64016404
const clone = createBasePrivateIdentifier(node.escapedText) as Mutable<GeneratedPrivateIdentifier>;
64026405
clone.flags |= node.flags & ~NodeFlags.Synthesized;
6403-
clone.autoGenerateFlags = node.autoGenerateFlags;
6404-
clone.autoGenerateId = node.autoGenerateId;
6405-
clone.autoGeneratePrefix = node.autoGeneratePrefix;
6406-
clone.autoGenerateSuffix = node.autoGenerateSuffix;
6406+
clone.autoGenerate = { ...node.autoGenerate };
64076407
clone.transformFlags = node.transformFlags;
64086408
setOriginalNode(clone, node);
64096409
return clone;

src/compiler/factory/utilities.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,17 +1489,18 @@ export function elideNodes<T extends Node>(factory: NodeFactory, nodes: NodeArra
14891489
* @internal
14901490
*/
14911491
export function getNodeForGeneratedName(name: GeneratedIdentifier | GeneratedPrivateIdentifier) {
1492-
if (name.autoGenerateFlags & GeneratedIdentifierFlags.Node) {
1493-
const autoGenerateId = name.autoGenerateId;
1492+
if (name.autoGenerate.flags & GeneratedIdentifierFlags.Node) {
1493+
const autoGenerateId = name.autoGenerate.id;
14941494
let node = name as Node;
14951495
let original = node.original;
14961496
while (original) {
14971497
node = original;
14981498

14991499
// if "node" is a different generated name (having a different "autoGenerateId"), use it and stop traversing.
1500-
if (isMemberName(node)
1501-
&& !!(node.autoGenerateFlags! & GeneratedIdentifierFlags.Node)
1502-
&& node.autoGenerateId !== autoGenerateId) {
1500+
if (isMemberName(node) && (
1501+
node.autoGenerate === undefined ||
1502+
!!(node.autoGenerate.flags & GeneratedIdentifierFlags.Node) &&
1503+
node.autoGenerate.id !== autoGenerateId)) {
15031504
break;
15041505
}
15051506

src/compiler/transformers/classFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ export function transformClassFields(context: TransformationContext): (x: Source
15121512
// record an alias as the class name is not in scope for statics.
15131513
enableSubstitutionForClassAliases();
15141514
const alias = factory.cloneNode(temp) as GeneratedIdentifier;
1515-
alias.autoGenerateFlags &= ~GeneratedIdentifierFlags.ReservedInNestedScopes;
1515+
alias.autoGenerate.flags &= ~GeneratedIdentifierFlags.ReservedInNestedScopes;
15161516
classAliases[getOriginalNodeId(node)] = alias;
15171517
}
15181518

src/compiler/transformers/module/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ export function transformModule(context: TransformationContext): (x: SourceFile
20642064
}
20652065
return node;
20662066
}
2067-
else if (!(isGeneratedIdentifier(node) && !(node.autoGenerateFlags & GeneratedIdentifierFlags.AllowNameSubstitution)) && !isLocalName(node)) {
2067+
else if (!(isGeneratedIdentifier(node) && !(node.autoGenerate.flags & GeneratedIdentifierFlags.AllowNameSubstitution)) && !isLocalName(node)) {
20682068
const exportContainer = resolver.getReferencedExportContainer(node, isExportName(node));
20692069
if (exportContainer && exportContainer.kind === SyntaxKind.SourceFile) {
20702070
return setTextRange(

src/compiler/types.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,10 +1678,7 @@ export interface Identifier extends PrimaryExpression, Declaration, JSDocContain
16781678
*/
16791679
readonly escapedText: __String;
16801680
readonly originalKeywordKind?: SyntaxKind; // Original syntaxKind which get set so that we can report an error later
1681-
/** @internal */ readonly autoGenerateFlags?: GeneratedIdentifierFlags; // Specifies whether to auto-generate the text for an identifier.
1682-
/** @internal */ readonly autoGenerateId?: number; // Ensures unique generated identifiers get unique names, but clones get the same name.
1683-
/** @internal */ readonly autoGeneratePrefix?: string | GeneratedNamePart;
1684-
/** @internal */ readonly autoGenerateSuffix?: string;
1681+
/** @internal */ readonly autoGenerate: AutoGenerateInfo | undefined; // Used for auto-generated identifiers.
16851682
/** @internal */ generatedImportReference?: ImportSpecifier; // Reference to the generated import specifier this identifier refers to
16861683
isInJSDocNamespace?: boolean; // if the node is a member in a JSDoc namespace
16871684
/** @internal */ typeArguments?: NodeArray<TypeNode | TypeParameterDeclaration>; // Only defined on synthesized nodes. Though not syntactically valid, used in emitting diagnostics, quickinfo, and signature help.
@@ -1694,9 +1691,17 @@ export interface TransientIdentifier extends Identifier {
16941691
resolvedSymbol: Symbol;
16951692
}
16961693

1694+
/** @internal */
1695+
export interface AutoGenerateInfo {
1696+
flags: GeneratedIdentifierFlags; // Specifies whether to auto-generate the text for an identifier.
1697+
readonly id: number; // Ensures unique generated identifiers get unique names, but clones get the same name.
1698+
readonly prefix?: string | GeneratedNamePart;
1699+
readonly suffix?: string;
1700+
}
1701+
16971702
/** @internal */
16981703
export interface GeneratedIdentifier extends Identifier {
1699-
autoGenerateFlags: GeneratedIdentifierFlags;
1704+
readonly autoGenerate: AutoGenerateInfo;
17001705
}
17011706

17021707
export interface QualifiedName extends Node, FlowContainer {
@@ -1774,15 +1779,12 @@ export interface PrivateIdentifier extends PrimaryExpression {
17741779
// escaping not strictly necessary
17751780
// avoids gotchas in transforms and utils
17761781
readonly escapedText: __String;
1777-
/** @internal */ readonly autoGenerateFlags?: GeneratedIdentifierFlags; // Specifies whether to auto-generate the text for an identifier.
1778-
/** @internal */ readonly autoGenerateId?: number; // Ensures unique generated identifiers get unique names, but clones get the same name.
1779-
/** @internal */ readonly autoGeneratePrefix?: string | GeneratedNamePart;
1780-
/** @internal */ readonly autoGenerateSuffix?: string;
1782+
/** @internal */ readonly autoGenerate: AutoGenerateInfo | undefined; // Used for auto-generated identifiers.
17811783
}
17821784

17831785
/** @internal */
17841786
export interface GeneratedPrivateIdentifier extends PrivateIdentifier {
1785-
autoGenerateFlags: GeneratedIdentifierFlags;
1787+
readonly autoGenerate: AutoGenerateInfo;
17861788
}
17871789

17881790
/** @internal */

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,7 @@ export function tryGetTextOfPropertyName(name: PropertyName | NoSubstitutionTemp
16511651
switch (name.kind) {
16521652
case SyntaxKind.Identifier:
16531653
case SyntaxKind.PrivateIdentifier:
1654-
return name.autoGenerateFlags ? undefined : name.escapedText;
1654+
return name.autoGenerate ? undefined : name.escapedText;
16551655
case SyntaxKind.StringLiteral:
16561656
case SyntaxKind.NumericLiteral:
16571657
case SyntaxKind.NoSubstitutionTemplateLiteral:

src/compiler/utilitiesPublic.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import {
6666
FunctionLikeDeclaration,
6767
FunctionTypeNode,
6868
GeneratedIdentifier,
69-
GeneratedIdentifierFlags,
7069
GeneratedPrivateIdentifier,
7170
GetAccessorDeclaration,
7271
getAssignmentDeclarationKind,
@@ -1476,12 +1475,12 @@ export function isStringTextContainingNode(node: Node): node is StringLiteral |
14761475

14771476
/** @internal */
14781477
export function isGeneratedIdentifier(node: Node): node is GeneratedIdentifier {
1479-
return isIdentifier(node) && (node.autoGenerateFlags! & GeneratedIdentifierFlags.KindMask) > GeneratedIdentifierFlags.None;
1478+
return isIdentifier(node) && node.autoGenerate !== undefined;
14801479
}
14811480

14821481
/** @internal */
14831482
export function isGeneratedPrivateIdentifier(node: Node): node is GeneratedPrivateIdentifier {
1484-
return isPrivateIdentifier(node) && (node.autoGenerateFlags! & GeneratedIdentifierFlags.KindMask) > GeneratedIdentifierFlags.None;
1483+
return isPrivateIdentifier(node) && node.autoGenerate !== undefined;
14851484
}
14861485

14871486
// Private Identifiers

src/harness/harnessUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function sourceFileToJSON(file: ts.Node): string {
201201
case "symbolCount":
202202
case "identifierCount":
203203
case "scriptSnapshot":
204-
case "autoGenerateFlags":
204+
case "autoGenerate":
205205
// Blocklist of items we never put in the baseline file.
206206
break;
207207

src/services/services.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ApplicableRefactorInfo,
77
ApplyCodeActionCommandResult,
88
AssignmentDeclarationKind,
9+
AutoGenerateInfo,
910
BaseType,
1011
BinaryExpression,
1112
BreakpointResolver,
@@ -85,7 +86,6 @@ import {
8586
FormatCodeSettings,
8687
formatting,
8788
FunctionLikeDeclaration,
88-
GeneratedIdentifierFlags,
8989
getAdjustedRenameLocation,
9090
getAllSuperTypeNodes,
9191
getAssignmentDeclarationKind,
@@ -735,7 +735,7 @@ class TokenObject<TKind extends SyntaxKind> extends TokenOrIdentifierObject impl
735735
class IdentifierObject extends TokenOrIdentifierObject implements Identifier {
736736
public kind: SyntaxKind.Identifier = SyntaxKind.Identifier;
737737
public escapedText!: __String;
738-
public autoGenerateFlags!: GeneratedIdentifierFlags;
738+
public autoGenerate: AutoGenerateInfo | undefined;
739739
declare _primaryExpressionBrand: any;
740740
declare _memberExpressionBrand: any;
741741
declare _leftHandSideExpressionBrand: any;
@@ -758,7 +758,7 @@ IdentifierObject.prototype.kind = SyntaxKind.Identifier;
758758
class PrivateIdentifierObject extends TokenOrIdentifierObject implements PrivateIdentifier {
759759
public kind: SyntaxKind.PrivateIdentifier = SyntaxKind.PrivateIdentifier;
760760
public escapedText!: __String;
761-
// public symbol!: Symbol;
761+
public autoGenerate: AutoGenerateInfo | undefined;
762762
declare _primaryExpressionBrand: any;
763763
declare _memberExpressionBrand: any;
764764
declare _leftHandSideExpressionBrand: any;

0 commit comments

Comments
 (0)