-
Notifications
You must be signed in to change notification settings - Fork 806
Port PR #61376: Fix spurious "used before being assigned" errors in for of/in loops #2462
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
Conversation
Port of microsoft/TypeScript#61376. Adds check for isForInOrOfStatement to prevent false positive TS2454 errors for variables declared in for-in/for-of loops when referenced in closures within the loop body. Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
RyanCavanaugh
left a 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.
Diff looks wonky because of the line breaks, what's actually happening is this
-isNeverInitialized := immediateDeclaration != nil && ast.IsVariableDeclaration(immediateDeclaration) && immediateDeclaration.Initializer() == nil && immediateDeclaration.AsVariableDeclaration().ExclamationToken == nil && c.isMutableLocalVariableDeclaration(immediateDeclaration) && !c.isSymbolAssignedDefinitely(symbol)
+isNeverInitialized := immediateDeclaration != nil && ast.IsVariableDeclaration(immediateDeclaration) && !ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent) && immediateDeclaration.Initializer() == nil && immediateDeclaration.AsVariableDeclaration().ExclamationToken == nil && c.isMutableLocalVariableDeclaration(immediateDeclaration) && !c.isSymbolAssignedDefinitely(symbol)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.
Pull request overview
This pull request ports TypeScript PR #61376, which fixes spurious "used before being assigned" errors (TS2454) for variables declared in for-in/for-of loops when they are referenced in closures within the loop body.
Key Changes:
- Added a check
!ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent)to theisNeverInitializedcondition incheckIdentifierfunction - Variables in for-in/for-of loops are now correctly recognized as implicitly initialized by the loop iteration
RyanCavanaugh
left a 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.
@copilot also add the new tests from the upstream PR as local tests in this repo
Added test from upstream PR microsoft/TypeScript#61376 to verify the fix for spurious "used before being assigned" errors (TS2454) in for-in/of loops. Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Port of microsoft/TypeScript#61376.
Variables declared in for-in/for-of loops were incorrectly triggering TS2454 ("used before being assigned") when referenced in closures within the loop body.
Changes
!ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent)check toisNeverInitializedininternal/checker/checker.gounusedLocalsInForInOrOf1.tsfrom upstream PR with baselinesVariables in for-in/for-of loops are implicitly initialized by the loop iteration and should not be treated as uninitialized.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.