Skip to content

Expose 'reservedInNestedScopes' option when creating temp and loop vars #43083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
25 changes: 18 additions & 7 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeNode | TypeParameterDeclaration> | 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

Expand Down
25 changes: 19 additions & 6 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
25 changes: 19 additions & 6 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down