Skip to content

Add duplicate identifier error for using declarations shadowed in for-of loops #61925

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

Closed
wants to merge 3 commits into from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 23, 2025

When a using declaration in a for-of loop head has the same name as a declaration in the loop body, the transformed output generates conflicting variable declarations that can cause runtime errors. This PR adds a compile-time error to catch this issue early.

Problem:

class Foo {}
for (using foo of [{ [Symbol.dispose]() {} }]) {
  const foo = new Foo(); // This shadows the using declaration
}

When compiled to ES5, this generates:

// ... transform helpers ...
try {
    var foo = __addDisposableResource(env_1, foo_1, false);
    var foo = new Foo(); // Redeclaration error at runtime
}

Solution:
Added a new check in checkForOfStatement that detects when:

  • A variable declaration list has the Using flag (covers both using and await using)
  • The target language version requires downleveling (< LanguageFeatureMinimumTarget.UsingAndAwaitUsing)
  • The loop body contains variable declarations with the same names as the using declarations

When these conditions are met, TypeScript now reports:

error TS2300: Duplicate identifier 'foo'.

Key Features:

  • Only applies when downleveling is required (no error in ESNext target)
  • Handles both using and await using declarations
  • Recursively searches all statements in the loop body, including nested blocks
  • Works with all variable declaration types (const, let, var)

Testing:
Added comprehensive test cases covering various scenarios and verified that existing functionality remains unaffected.

Fixes #61714.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Jun 23, 2025
…-of loops

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] using transform throws when a for body binding shadows the for head binding Add duplicate identifier error for using declarations shadowed in for-of loops Jun 23, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh June 23, 2025 22:49
Copilot finished work on behalf of RyanCavanaugh June 23, 2025 22:49
Copy link
Member

@RyanCavanaugh RyanCavanaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not leave TODOs in your code. You didn't need two testcases here since one already tests both configurations. There was no need to typecast both .escapedTexts to string.

Start over from scratch. Duplicate identifier logic generally occurs in the binder. Look at how other cases work.

…-of loops

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 23, 2025

You should not leave TODOs in your code. You didn't need two testcases here since one already tests both configurations. There was no need to typecast both .escapedTexts to string.

Start over fro...

I've completely rewritten the implementation based on your feedback. The duplicate identifier logic now correctly resides in the binder (rather than the checker), following the same pattern as other duplicate identifier cases. I've also simplified the test case and removed unnecessary type assertions and TODOs. The fix is now implemented in bindForInOrForOfStatement and properly detects shadowing only when using declarations will be downleveled. Commit f6e16c3.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • plugins.dprint.dev
    • Triggering command: /home/REDACTED/work/TypeScript/TypeScript/node_modules/dprint/dprint fmt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@Copilot Copilot AI requested a review from RyanCavanaugh June 23, 2025 23:19
Copilot finished work on behalf of RyanCavanaugh June 23, 2025 23:19
@RyanCavanaugh
Copy link
Member

This seems worse than a proper fix, my bad.

@jakebailey jakebailey deleted the copilot/fix-61714 branch July 1, 2025 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Error loading sessions

Retrying...

Successfully merging this pull request may close these issues.

using transform throws when a for body binding shadows the for head binding
3 participants