-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Fix using transform variable shadowing in for-of loops #61934
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
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/fix-61714-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
34e17b3
Initial plan for issue
Copilot 237579e
Implement variable renaming for using declaration shadowing in for-of…
Copilot 6fead46
Add test baselines for using declaration shadowing fix
Copilot 2738f69
Add importHelpers directive and fix line endings
Copilot 8f3df02
Implement block scoping approach for using declaration shadowing in f…
Copilot 0b3bf33
Fix empty block generation in using declaration for-of loops and remo…
Copilot 957cf79
Fix empty statement handling in using declaration for-of loops
Copilot 88c9bfa
Fix trailing whitespace in esnext.ts
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,67 +1,69 @@ | ||||||||||||
import { | ||||||||||||
addEmitHelpers, | ||||||||||||
addRange, | ||||||||||||
append, | ||||||||||||
arrayFrom, | ||||||||||||
BindingElement, | ||||||||||||
Block, | ||||||||||||
Bundle, | ||||||||||||
CaseOrDefaultClause, | ||||||||||||
chainBundle, | ||||||||||||
ClassDeclaration, | ||||||||||||
Debug, | ||||||||||||
EmitFlags, | ||||||||||||
ExportAssignment, | ||||||||||||
ExportSpecifier, | ||||||||||||
Expression, | ||||||||||||
firstOrUndefined, | ||||||||||||
ForOfStatement, | ||||||||||||
ForStatement, | ||||||||||||
GeneratedIdentifierFlags, | ||||||||||||
getEmitFlags, | ||||||||||||
hasSyntacticModifier, | ||||||||||||
Identifier, | ||||||||||||
IdentifierNameMap, | ||||||||||||
isArray, | ||||||||||||
isBindingPattern, | ||||||||||||
isBlock, | ||||||||||||
isCaseClause, | ||||||||||||
isCustomPrologue, | ||||||||||||
isExpression, | ||||||||||||
isGeneratedIdentifier, | ||||||||||||
isIdentifier, | ||||||||||||
isLocalName, | ||||||||||||
isNamedEvaluation, | ||||||||||||
isOmittedExpression, | ||||||||||||
isPrologueDirective, | ||||||||||||
isSourceFile, | ||||||||||||
isStatement, | ||||||||||||
isVariableDeclarationList, | ||||||||||||
isVariableStatement, | ||||||||||||
ModifierFlags, | ||||||||||||
Node, | ||||||||||||
NodeFlags, | ||||||||||||
setCommentRange, | ||||||||||||
setEmitFlags, | ||||||||||||
setOriginalNode, | ||||||||||||
setSourceMapRange, | ||||||||||||
setTextRange, | ||||||||||||
skipOuterExpressions, | ||||||||||||
SourceFile, | ||||||||||||
Statement, | ||||||||||||
SwitchStatement, | ||||||||||||
SyntaxKind, | ||||||||||||
TransformationContext, | ||||||||||||
TransformFlags, | ||||||||||||
transformNamedEvaluation, | ||||||||||||
VariableDeclaration, | ||||||||||||
VariableDeclarationList, | ||||||||||||
VariableStatement, | ||||||||||||
visitArray, | ||||||||||||
visitEachChild, | ||||||||||||
visitNode, | ||||||||||||
visitNodes, | ||||||||||||
VisitResult, | ||||||||||||
import { | ||||||||||||
addEmitHelpers, | ||||||||||||
addRange, | ||||||||||||
append, | ||||||||||||
arrayFrom, | ||||||||||||
BindingElement, | ||||||||||||
Block, | ||||||||||||
Bundle, | ||||||||||||
CaseOrDefaultClause, | ||||||||||||
chainBundle, | ||||||||||||
ClassDeclaration, | ||||||||||||
Debug, | ||||||||||||
EmitFlags, | ||||||||||||
ExportAssignment, | ||||||||||||
ExportSpecifier, | ||||||||||||
Expression, | ||||||||||||
firstOrUndefined, | ||||||||||||
forEachChild, | ||||||||||||
ForOfStatement, | ||||||||||||
ForStatement, | ||||||||||||
GeneratedIdentifierFlags, | ||||||||||||
getEmitFlags, | ||||||||||||
hasSyntacticModifier, | ||||||||||||
Identifier, | ||||||||||||
IdentifierNameMap, | ||||||||||||
isArray, | ||||||||||||
isBindingPattern, | ||||||||||||
isBlock, | ||||||||||||
isCaseClause, | ||||||||||||
isCustomPrologue, | ||||||||||||
isExpression, | ||||||||||||
isGeneratedIdentifier, | ||||||||||||
isIdentifier, | ||||||||||||
isLocalName, | ||||||||||||
isNamedEvaluation, | ||||||||||||
isOmittedExpression, | ||||||||||||
isPrologueDirective, | ||||||||||||
isSourceFile, | ||||||||||||
isStatement, | ||||||||||||
isVariableDeclaration, | ||||||||||||
isVariableDeclarationList, | ||||||||||||
isVariableStatement, | ||||||||||||
ModifierFlags, | ||||||||||||
Node, | ||||||||||||
NodeFlags, | ||||||||||||
setCommentRange, | ||||||||||||
setEmitFlags, | ||||||||||||
setOriginalNode, | ||||||||||||
setSourceMapRange, | ||||||||||||
setTextRange, | ||||||||||||
skipOuterExpressions, | ||||||||||||
SourceFile, | ||||||||||||
Statement, | ||||||||||||
SwitchStatement, | ||||||||||||
SyntaxKind, | ||||||||||||
TransformationContext, | ||||||||||||
TransformFlags, | ||||||||||||
transformNamedEvaluation, | ||||||||||||
VariableDeclaration, | ||||||||||||
VariableDeclarationList, | ||||||||||||
VariableStatement, | ||||||||||||
visitArray, | ||||||||||||
visitEachChild, | ||||||||||||
visitNode, | ||||||||||||
visitNodes, | ||||||||||||
VisitResult, | ||||||||||||
} from "../_namespaces/ts.js"; | ||||||||||||
|
||||||||||||
const enum UsingKind { | ||||||||||||
|
@@ -289,54 +291,121 @@ export function transformESNext(context: TransformationContext): (x: SourceFile | |||||||||||
); | ||||||||||||
} | ||||||||||||
|
||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
} | ||||||||||||
|
||||||||||||
function visitForOfStatement(node: ForOfStatement) { | ||||||||||||
if (isUsingVariableDeclarationList(node.initializer)) { | ||||||||||||
// given: | ||||||||||||
// | ||||||||||||
// for (using x of y) { ... } | ||||||||||||
// | ||||||||||||
// produces a shallow transformation to: | ||||||||||||
// | ||||||||||||
// for (const x_1 of y) { | ||||||||||||
// using x = x; | ||||||||||||
// ... | ||||||||||||
// } | ||||||||||||
// | ||||||||||||
// before handing the shallow transformation back to the visitor for an in-depth transformation. | ||||||||||||
const forInitializer = node.initializer; | ||||||||||||
const forDecl = firstOrUndefined(forInitializer.declarations) || factory.createVariableDeclaration(factory.createTempVariable(/*recordTempVariable*/ undefined)); | ||||||||||||
|
||||||||||||
const isAwaitUsing = getUsingKindOfVariableDeclarationList(forInitializer) === UsingKind.Async; | ||||||||||||
const temp = factory.getGeneratedNameForNode(forDecl.name); | ||||||||||||
const usingVar = factory.updateVariableDeclaration(forDecl, forDecl.name, /*exclamationToken*/ undefined, /*type*/ undefined, temp); | ||||||||||||
const usingVarList = factory.createVariableDeclarationList([usingVar], isAwaitUsing ? NodeFlags.AwaitUsing : NodeFlags.Using); | ||||||||||||
const usingVarStatement = factory.createVariableStatement(/*modifiers*/ undefined, usingVarList); | ||||||||||||
return visitNode( | ||||||||||||
factory.updateForOfStatement( | ||||||||||||
node, | ||||||||||||
node.awaitModifier, | ||||||||||||
factory.createVariableDeclarationList([ | ||||||||||||
factory.createVariableDeclaration(temp), | ||||||||||||
], NodeFlags.Const), | ||||||||||||
node.expression, | ||||||||||||
isBlock(node.statement) ? | ||||||||||||
factory.updateBlock(node.statement, [ | ||||||||||||
usingVarStatement, | ||||||||||||
...node.statement.statements, | ||||||||||||
]) : | ||||||||||||
factory.createBlock([ | ||||||||||||
usingVarStatement, | ||||||||||||
node.statement, | ||||||||||||
], /*multiLine*/ true), | ||||||||||||
), | ||||||||||||
visitor, | ||||||||||||
isStatement, | ||||||||||||
); | ||||||||||||
} | ||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Collects all variable declarations that shadow a given identifier name in a statement. | ||||||||||||
*/ | ||||||||||||
function collectShadowingVariables(statement: Statement, shadowedName: string): VariableDeclaration[] { | ||||||||||||
const shadowingVars: VariableDeclaration[] = []; | ||||||||||||
|
||||||||||||
function visit(node: Node): void { | ||||||||||||
if (isVariableStatement(node)) { | ||||||||||||
for (const declaration of node.declarationList.declarations) { | ||||||||||||
if (isIdentifier(declaration.name) && declaration.name.escapedText === shadowedName) { | ||||||||||||
shadowingVars.push(declaration); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} | ||||||||||||
forEachChild(node, visit); | ||||||||||||
} | ||||||||||||
|
||||||||||||
visit(statement); | ||||||||||||
return shadowingVars; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Creates a visitor that renames shadowing variables to avoid conflicts. | ||||||||||||
*/ | ||||||||||||
function createShadowingVariableRenamer(shadowedName: string): (node: Node) => VisitResult<Node> { | ||||||||||||
const renamingMap = new Map<string, Identifier>(); | ||||||||||||
|
||||||||||||
return function renameShadowingVariables(node: Node): VisitResult<Node> { | ||||||||||||
if (isVariableDeclaration(node) && isIdentifier(node.name) && node.name.escapedText === shadowedName) { | ||||||||||||
// Create a unique name for this shadowing variable | ||||||||||||
const uniqueName = factory.createUniqueName(shadowedName as string, GeneratedIdentifierFlags.Optimistic); | ||||||||||||
renamingMap.set(node.name.escapedText as string, uniqueName); | ||||||||||||
|
||||||||||||
return factory.updateVariableDeclaration( | ||||||||||||
node, | ||||||||||||
uniqueName, | ||||||||||||
node.exclamationToken, | ||||||||||||
node.type, | ||||||||||||
visitNode(node.initializer, renameShadowingVariables, isExpression) | ||||||||||||
); | ||||||||||||
} | ||||||||||||
|
||||||||||||
if (isIdentifier(node)) { | ||||||||||||
const renamed = renamingMap.get(node.escapedText as string); | ||||||||||||
if (renamed) { | ||||||||||||
return renamed; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
return visitEachChild(node, renameShadowingVariables, context); | ||||||||||||
}; | ||||||||||||
} | ||||||||||||
|
||||||||||||
function visitForOfStatement(node: ForOfStatement) { | ||||||||||||
if (isUsingVariableDeclarationList(node.initializer)) { | ||||||||||||
// given: | ||||||||||||
// | ||||||||||||
// for (using x of y) { ... } | ||||||||||||
// | ||||||||||||
// produces a shallow transformation to: | ||||||||||||
// | ||||||||||||
// for (const x_1 of y) { | ||||||||||||
// using x = x; | ||||||||||||
// ... | ||||||||||||
// } | ||||||||||||
// | ||||||||||||
// before handing the shallow transformation back to the visitor for an in-depth transformation. | ||||||||||||
const forInitializer = node.initializer; | ||||||||||||
const forDecl = firstOrUndefined(forInitializer.declarations) || factory.createVariableDeclaration(factory.createTempVariable(/*recordTempVariable*/ undefined)); | ||||||||||||
|
||||||||||||
const isAwaitUsing = getUsingKindOfVariableDeclarationList(forInitializer) === UsingKind.Async; | ||||||||||||
const temp = factory.getGeneratedNameForNode(forDecl.name); | ||||||||||||
const usingVar = factory.updateVariableDeclaration(forDecl, forDecl.name, /*exclamationToken*/ undefined, /*type*/ undefined, temp); | ||||||||||||
const usingVarList = factory.createVariableDeclarationList([usingVar], isAwaitUsing ? NodeFlags.AwaitUsing : NodeFlags.Using); | ||||||||||||
const usingVarStatement = factory.createVariableStatement(/*modifiers*/ undefined, usingVarList); | ||||||||||||
|
||||||||||||
// Check if the loop body contains shadowing variables and rename them if necessary | ||||||||||||
const shadowedName = isIdentifier(forDecl.name) ? forDecl.name.escapedText as string : undefined; | ||||||||||||
let transformedStatement = node.statement; | ||||||||||||
|
||||||||||||
if (shadowedName) { | ||||||||||||
const shadowingVars = collectShadowingVariables(node.statement, shadowedName); | ||||||||||||
if (shadowingVars.length > 0) { | ||||||||||||
// Apply the renaming visitor to the loop body | ||||||||||||
const renamer = createShadowingVariableRenamer(shadowedName); | ||||||||||||
transformedStatement = visitNode(node.statement, renamer, isStatement); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
return visitNode( | ||||||||||||
factory.updateForOfStatement( | ||||||||||||
node, | ||||||||||||
node.awaitModifier, | ||||||||||||
factory.createVariableDeclarationList([ | ||||||||||||
factory.createVariableDeclaration(temp), | ||||||||||||
], NodeFlags.Const), | ||||||||||||
node.expression, | ||||||||||||
isBlock(transformedStatement) ? | ||||||||||||
factory.updateBlock(transformedStatement, [ | ||||||||||||
usingVarStatement, | ||||||||||||
...transformedStatement.statements, | ||||||||||||
]) : | ||||||||||||
factory.createBlock([ | ||||||||||||
usingVarStatement, | ||||||||||||
transformedStatement, | ||||||||||||
], /*multiLine*/ true), | ||||||||||||
), | ||||||||||||
visitor, | ||||||||||||
isStatement, | ||||||||||||
); | ||||||||||||
} | ||||||||||||
return visitEachChild(node, visitor, context); | ||||||||||||
} | ||||||||||||
|
||||||||||||
function visitCaseOrDefaultClause(node: CaseOrDefaultClause, envBinding: Identifier) { | ||||||||||||
|
19 changes: 19 additions & 0 deletions
19
tests/baselines/reference/usingDeclarationsInForOfShadowing(target=es2015).errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
usingDeclarationsInForOfShadowing.ts(12,7): error TS2481: Cannot initialize outer scoped variable 'baz' in the same scope as block scoped declaration 'baz'. | ||
|
||
|
||
==== usingDeclarationsInForOfShadowing.ts (1 errors) ==== | ||
class Foo {} | ||
|
||
for (using foo of []) { | ||
const foo = new Foo(); | ||
} | ||
|
||
for (using bar of []) { | ||
let bar = "test"; | ||
} | ||
|
||
for (using baz of []) { | ||
var baz = 42; | ||
~~~ | ||
!!! error TS2481: Cannot initialize outer scoped variable 'baz' in the same scope as block scoped declaration 'baz'. | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleted the comment in commit 0b3bf33. The obsolete comment about collecting variable declarations has been removed from esnext.ts.