diff --git a/src/compiler/factory/nodeFactory.ts b/src/compiler/factory/nodeFactory.ts index 0a2918aedf587..ddb8c8cf59ea7 100644 --- a/src/compiler/factory/nodeFactory.ts +++ b/src/compiler/factory/nodeFactory.ts @@ -883,8 +883,10 @@ namespace ts { /** Create a unique temporary variable for use in a loop. */ // @api - function createLoopVariable(): Identifier { - return createBaseGeneratedIdentifier("", GeneratedIdentifierFlags.Loop); + function createLoopVariable(reservedInNestedScopes?: boolean): Identifier { + let flags = GeneratedIdentifierFlags.Loop; + if (reservedInNestedScopes) flags |= GeneratedIdentifierFlags.ReservedInNestedScopes; + return createBaseGeneratedIdentifier("", flags); } /** Create a unique name based on the supplied text. */ diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5b6bd03eecfd8..f4e9639416e0b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6785,19 +6785,30 @@ namespace ts { /* @internal */ createIdentifier(text: string, typeArguments?: readonly (TypeNode | TypeParameterDeclaration)[], originalKeywordKind?: SyntaxKind): Identifier; // eslint-disable-line @typescript-eslint/unified-signatures /* @internal */ updateIdentifier(node: Identifier, typeArguments: NodeArray | undefined): Identifier; - /** Create a unique temporary variable. */ - createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier; - /* @internal */ createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier; // eslint-disable-line @typescript-eslint/unified-signatures + /** + * Create a unique temporary variable. + * @param recordTempVariable An optional callback used to record the temporary variable name. This + * should usually be a reference to `hoistVariableDeclaration` from a `TransformationContext`, but + * can be `undefined` if you plan to record the temporary variable manually. + * @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes + * during emit so that the variable can be referenced in a nested function body. This is an alternative to + * setting `EmitFlags.ReuseTempVariableScope` on the nested function itself. + */ + createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier; - /** Create a unique temporary variable for use in a loop. */ - createLoopVariable(): Identifier; + /** + * Create a unique temporary variable for use in a loop. + * @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes + * during emit so that the variable can be referenced in a nested function body. This is an alternative to + * setting `EmitFlags.ReuseTempVariableScope` on the nested function itself. + */ + createLoopVariable(reservedInNestedScopes?: boolean): Identifier; /** Create a unique name based on the supplied text. */ createUniqueName(text: string, flags?: GeneratedIdentifierFlags): Identifier; /** Create a unique name generated for a node. */ - getGeneratedNameForNode(node: Node | undefined): Identifier; - /* @internal */ getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier; // eslint-disable-line @typescript-eslint/unified-signatures + getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier; createPrivateIdentifier(text: string): PrivateIdentifier diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index aa6a43193d819..a640f1f5513c3 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -3176,14 +3176,27 @@ declare namespace ts { createStringLiteralFromNode(sourceNode: PropertyNameLiteral, isSingleQuote?: boolean): StringLiteral; createRegularExpressionLiteral(text: string): RegularExpressionLiteral; createIdentifier(text: string): Identifier; - /** Create a unique temporary variable. */ - createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier; - /** Create a unique temporary variable for use in a loop. */ - createLoopVariable(): Identifier; + /** + * Create a unique temporary variable. + * @param recordTempVariable An optional callback used to record the temporary variable name. This + * should usually be a reference to `hoistVariableDeclaration` from a `TransformationContext`, but + * can be `undefined` if you plan to record the temporary variable manually. + * @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes + * during emit so that the variable can be referenced in a nested function body. This is an alternative to + * setting `EmitFlags.ReuseTempVariableScope` on the nested function itself. + */ + createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier; + /** + * Create a unique temporary variable for use in a loop. + * @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes + * during emit so that the variable can be referenced in a nested function body. This is an alternative to + * setting `EmitFlags.ReuseTempVariableScope` on the nested function itself. + */ + createLoopVariable(reservedInNestedScopes?: boolean): Identifier; /** Create a unique name based on the supplied text. */ createUniqueName(text: string, flags?: GeneratedIdentifierFlags): Identifier; /** Create a unique name generated for a node. */ - getGeneratedNameForNode(node: Node | undefined): Identifier; + getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier; createPrivateIdentifier(text: string): PrivateIdentifier; createToken(token: SyntaxKind.SuperKeyword): SuperExpression; createToken(token: SyntaxKind.ThisKeyword): ThisExpression; @@ -10256,7 +10269,7 @@ declare namespace ts { /** @deprecated Use `factory.createRegularExpressionLiteral` or the factory supplied by your transformation context instead. */ const createRegularExpressionLiteral: (text: string) => RegularExpressionLiteral; /** @deprecated Use `factory.createLoopVariable` or the factory supplied by your transformation context instead. */ - const createLoopVariable: () => Identifier; + const createLoopVariable: (reservedInNestedScopes?: boolean | undefined) => Identifier; /** @deprecated Use `factory.createUniqueName` or the factory supplied by your transformation context instead. */ const createUniqueName: (text: string, flags?: GeneratedIdentifierFlags | undefined) => Identifier; /** @deprecated Use `factory.createPrivateIdentifier` or the factory supplied by your transformation context instead. */ diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 775a7a4114400..197ebfcf88c42 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -3176,14 +3176,27 @@ declare namespace ts { createStringLiteralFromNode(sourceNode: PropertyNameLiteral, isSingleQuote?: boolean): StringLiteral; createRegularExpressionLiteral(text: string): RegularExpressionLiteral; createIdentifier(text: string): Identifier; - /** Create a unique temporary variable. */ - createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined): Identifier; - /** Create a unique temporary variable for use in a loop. */ - createLoopVariable(): Identifier; + /** + * Create a unique temporary variable. + * @param recordTempVariable An optional callback used to record the temporary variable name. This + * should usually be a reference to `hoistVariableDeclaration` from a `TransformationContext`, but + * can be `undefined` if you plan to record the temporary variable manually. + * @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes + * during emit so that the variable can be referenced in a nested function body. This is an alternative to + * setting `EmitFlags.ReuseTempVariableScope` on the nested function itself. + */ + createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, reservedInNestedScopes?: boolean): Identifier; + /** + * Create a unique temporary variable for use in a loop. + * @param reservedInNestedScopes When `true`, reserves the temporary variable name in all nested scopes + * during emit so that the variable can be referenced in a nested function body. This is an alternative to + * setting `EmitFlags.ReuseTempVariableScope` on the nested function itself. + */ + createLoopVariable(reservedInNestedScopes?: boolean): Identifier; /** Create a unique name based on the supplied text. */ createUniqueName(text: string, flags?: GeneratedIdentifierFlags): Identifier; /** Create a unique name generated for a node. */ - getGeneratedNameForNode(node: Node | undefined): Identifier; + getGeneratedNameForNode(node: Node | undefined, flags?: GeneratedIdentifierFlags): Identifier; createPrivateIdentifier(text: string): PrivateIdentifier; createToken(token: SyntaxKind.SuperKeyword): SuperExpression; createToken(token: SyntaxKind.ThisKeyword): ThisExpression; @@ -6513,7 +6526,7 @@ declare namespace ts { /** @deprecated Use `factory.createRegularExpressionLiteral` or the factory supplied by your transformation context instead. */ const createRegularExpressionLiteral: (text: string) => RegularExpressionLiteral; /** @deprecated Use `factory.createLoopVariable` or the factory supplied by your transformation context instead. */ - const createLoopVariable: () => Identifier; + const createLoopVariable: (reservedInNestedScopes?: boolean | undefined) => Identifier; /** @deprecated Use `factory.createUniqueName` or the factory supplied by your transformation context instead. */ const createUniqueName: (text: string, flags?: GeneratedIdentifierFlags | undefined) => Identifier; /** @deprecated Use `factory.createPrivateIdentifier` or the factory supplied by your transformation context instead. */