Skip to content

Commit 38fdce9

Browse files
authored
Expose 'reservedInNestedScopes' option when creating temp and loop variables (#43083)
1 parent e234f0c commit 38fdce9

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

src/compiler/factory/nodeFactory.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,10 @@ namespace ts {
883883

884884
/** Create a unique temporary variable for use in a loop. */
885885
// @api
886-
function createLoopVariable(): Identifier {
887-
return createBaseGeneratedIdentifier("", GeneratedIdentifierFlags.Loop);
886+
function createLoopVariable(reservedInNestedScopes?: boolean): Identifier {
887+
let flags = GeneratedIdentifierFlags.Loop;
888+
if (reservedInNestedScopes) flags |= GeneratedIdentifierFlags.ReservedInNestedScopes;
889+
return createBaseGeneratedIdentifier("", flags);
888890
}
889891

890892
/** Create a unique name based on the supplied text. */

src/compiler/types.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -6785,19 +6785,30 @@ namespace ts {
67856785
/* @internal */ createIdentifier(text: string, typeArguments?: readonly (TypeNode | TypeParameterDeclaration)[], originalKeywordKind?: SyntaxKind): Identifier; // eslint-disable-line @typescript-eslint/unified-signatures
67866786
/* @internal */ updateIdentifier(node: Identifier, typeArguments: NodeArray<TypeNode | TypeParameterDeclaration> | undefined): Identifier;
67876787

6788-
/** Create a unique temporary variable. */
6789-
createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier;
6790-
/* @internal */ createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier; // eslint-disable-line @typescript-eslint/unified-signatures
6788+
/**
6789+
* Create a unique temporary variable.
6790+
* @param recordTempVariable An optional callback used to record the temporary variable name. This
6791+
* should usually be a reference to `hoistVariableDeclaration` from a `TransformationContext`, but
6792+
* can be `undefined` if you plan to record the temporary variable manually.
6793+
* @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes
6794+
* during emit so that the variable can be referenced in a nested function body. This is an alternative to
6795+
* setting `EmitFlags.ReuseTempVariableScope` on the nested function itself.
6796+
*/
6797+
createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier;
67916798

6792-
/** Create a unique temporary variable for use in a loop. */
6793-
createLoopVariable(): Identifier;
6799+
/**
6800+
* Create a unique temporary variable for use in a loop.
6801+
* @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes
6802+
* during emit so that the variable can be referenced in a nested function body. This is an alternative to
6803+
* setting `EmitFlags.ReuseTempVariableScope` on the nested function itself.
6804+
*/
6805+
createLoopVariable(reservedInNestedScopes?: boolean): Identifier;
67946806

67956807
/** Create a unique name based on the supplied text. */
67966808
createUniqueName(text: string, flags?: GeneratedIdentifierFlags): Identifier;
67976809

67986810
/** Create a unique name generated for a node. */
6799-
getGeneratedNameForNode(node: Node | undefined): Identifier;
6800-
/* @internal */ getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier; // eslint-disable-line @typescript-eslint/unified-signatures
6811+
getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier;
68016812

68026813
createPrivateIdentifier(text: string): PrivateIdentifier
68036814

tests/baselines/reference/api/tsserverlibrary.d.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -3176,14 +3176,27 @@ declare namespace ts {
31763176
createStringLiteralFromNode(sourceNode: PropertyNameLiteral, isSingleQuote?: boolean): StringLiteral;
31773177
createRegularExpressionLiteral(text: string): RegularExpressionLiteral;
31783178
createIdentifier(text: string): Identifier;
3179-
/** Create a unique temporary variable. */
3180-
createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier;
3181-
/** Create a unique temporary variable for use in a loop. */
3182-
createLoopVariable(): Identifier;
3179+
/**
3180+
* Create a unique temporary variable.
3181+
* @param recordTempVariable An optional callback used to record the temporary variable name. This
3182+
* should usually be a reference to `hoistVariableDeclaration` from a `TransformationContext`, but
3183+
* can be `undefined` if you plan to record the temporary variable manually.
3184+
* @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes
3185+
* during emit so that the variable can be referenced in a nested function body. This is an alternative to
3186+
* setting `EmitFlags.ReuseTempVariableScope` on the nested function itself.
3187+
*/
3188+
createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier;
3189+
/**
3190+
* Create a unique temporary variable for use in a loop.
3191+
* @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes
3192+
* during emit so that the variable can be referenced in a nested function body. This is an alternative to
3193+
* setting `EmitFlags.ReuseTempVariableScope` on the nested function itself.
3194+
*/
3195+
createLoopVariable(reservedInNestedScopes?: boolean): Identifier;
31833196
/** Create a unique name based on the supplied text. */
31843197
createUniqueName(text: string, flags?: GeneratedIdentifierFlags): Identifier;
31853198
/** Create a unique name generated for a node. */
3186-
getGeneratedNameForNode(node: Node | undefined): Identifier;
3199+
getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier;
31873200
createPrivateIdentifier(text: string): PrivateIdentifier;
31883201
createToken(token: SyntaxKind.SuperKeyword): SuperExpression;
31893202
createToken(token: SyntaxKind.ThisKeyword): ThisExpression;
@@ -10256,7 +10269,7 @@ declare namespace ts {
1025610269
/** @deprecated Use `factory.createRegularExpressionLiteral` or the factory supplied by your transformation context instead. */
1025710270
const createRegularExpressionLiteral: (text: string) => RegularExpressionLiteral;
1025810271
/** @deprecated Use `factory.createLoopVariable` or the factory supplied by your transformation context instead. */
10259-
const createLoopVariable: () => Identifier;
10272+
const createLoopVariable: (reservedInNestedScopes?: boolean | undefined) => Identifier;
1026010273
/** @deprecated Use `factory.createUniqueName` or the factory supplied by your transformation context instead. */
1026110274
const createUniqueName: (text: string, flags?: GeneratedIdentifierFlags | undefined) => Identifier;
1026210275
/** @deprecated Use `factory.createPrivateIdentifier` or the factory supplied by your transformation context instead. */

tests/baselines/reference/api/typescript.d.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -3176,14 +3176,27 @@ declare namespace ts {
31763176
createStringLiteralFromNode(sourceNode: PropertyNameLiteral, isSingleQuote?: boolean): StringLiteral;
31773177
createRegularExpressionLiteral(text: string): RegularExpressionLiteral;
31783178
createIdentifier(text: string): Identifier;
3179-
/** Create a unique temporary variable. */
3180-
createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier;
3181-
/** Create a unique temporary variable for use in a loop. */
3182-
createLoopVariable(): Identifier;
3179+
/**
3180+
* Create a unique temporary variable.
3181+
* @param recordTempVariable An optional callback used to record the temporary variable name. This
3182+
* should usually be a reference to `hoistVariableDeclaration` from a `TransformationContext`, but
3183+
* can be `undefined` if you plan to record the temporary variable manually.
3184+
* @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes
3185+
* during emit so that the variable can be referenced in a nested function body. This is an alternative to
3186+
* setting `EmitFlags.ReuseTempVariableScope` on the nested function itself.
3187+
*/
3188+
createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier;
3189+
/**
3190+
* Create a unique temporary variable for use in a loop.
3191+
* @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes
3192+
* during emit so that the variable can be referenced in a nested function body. This is an alternative to
3193+
* setting `EmitFlags.ReuseTempVariableScope` on the nested function itself.
3194+
*/
3195+
createLoopVariable(reservedInNestedScopes?: boolean): Identifier;
31833196
/** Create a unique name based on the supplied text. */
31843197
createUniqueName(text: string, flags?: GeneratedIdentifierFlags): Identifier;
31853198
/** Create a unique name generated for a node. */
3186-
getGeneratedNameForNode(node: Node | undefined): Identifier;
3199+
getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier;
31873200
createPrivateIdentifier(text: string): PrivateIdentifier;
31883201
createToken(token: SyntaxKind.SuperKeyword): SuperExpression;
31893202
createToken(token: SyntaxKind.ThisKeyword): ThisExpression;
@@ -6513,7 +6526,7 @@ declare namespace ts {
65136526
/** @deprecated Use `factory.createRegularExpressionLiteral` or the factory supplied by your transformation context instead. */
65146527
const createRegularExpressionLiteral: (text: string) => RegularExpressionLiteral;
65156528
/** @deprecated Use `factory.createLoopVariable` or the factory supplied by your transformation context instead. */
6516-
const createLoopVariable: () => Identifier;
6529+
const createLoopVariable: (reservedInNestedScopes?: boolean | undefined) => Identifier;
65176530
/** @deprecated Use `factory.createUniqueName` or the factory supplied by your transformation context instead. */
65186531
const createUniqueName: (text: string, flags?: GeneratedIdentifierFlags | undefined) => Identifier;
65196532
/** @deprecated Use `factory.createPrivateIdentifier` or the factory supplied by your transformation context instead. */

0 commit comments

Comments
 (0)