typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Port missing collision detection checks from checkCollisionsForDeclarationName

Open Copilot opened this issue 1 month ago • 4 comments

Ports all missing collision detection checks from TypeScript's checkCollisionsForDeclarationName function to the Go implementation.

Changes Made

  • WeakMap/WeakSet collision detection: Records potential collisions when WeakMap/WeakSet identifiers are declared (for ES2021 and earlier targets), marks enclosing block scopes with ContainsClassWithPrivateIdentifiers flag when private fields are found, and reports TS18027 errors for collisions in scopes with private identifiers.

  • Promise collision detection: Implements checkCollisionWithGlobalPromiseInGeneratedCode to detect when the Promise identifier is shadowed in external/CommonJS modules for ES2016 and earlier targets.

  • Reflect collision detection: Implements recordPotentialCollisionWithReflectInGeneratedCode and checkReflectCollision to detect when the Reflect identifier is shadowed in contexts containing super property access in static initializers (for ES2021 and earlier).

  • Object class name collision: Implements checkClassNameCollisionWithObject to validate that class names are not "Object" when targeting ES5 or above with certain module formats.

  • Infrastructure: Added potentialWeakMapSetCollisions and potentialReflectCollisions arrays to Checker struct, implemented setNodeLinksForPrivateIdentifierScope to mark scopes with private identifiers, and integrated all collision checks into checkSourceFile for deferred processing.

Implementation Details

All collision checks use errorSkippedOnNoEmit to match TypeScript's behavior, ensuring errors are only reported when actually emitting code (not with --noEmit flag). The implementation properly handles nil checks and follows the same deferred checking pattern as TypeScript for collisions that depend on information gathered during the entire source file analysis.

Testing

  • ✅ All tests passing
  • ✅ Baselines accepted for new collision checks
  • ✅ Fixes issue microsoft/typescript-go#2086

The changes bring the Go implementation into parity with TypeScript's collision detection in checkCollisionsForDeclarationName.

Fixes microsoft/typescript-go#2086

Original prompt

This section details on the original issue you should resolve

<issue_title>Missing error when shadowing WeakMap/WeakSet in class private field's scope</issue_title> <issue_description>## Steps to reproduce

    function test() {
        let WeakMap;
            ~~~~~~~
!!! error TS18027: Compiler reserves name 'WeakMap' when emitting private identifier downlevel.
        let WeakSet;
            ~~~~~~~
!!! error TS18027: Compiler reserves name 'WeakSet' when emitting private identifier downlevel.
        class C {
            #x;
        }
    }

Behavior with [email protected]

As shown

Behavior with tsgo

No error

Likely just because this transform isn't implemented yet, but logging to document an accepted error delta</issue_description>

<agent_instructions>Look for checkCollisionsForDeclarationName in the original checker.ts and in checker.go, then port the missing collision functions</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@jakebailey This would be a checker bug; `recordPotentialCollisionWithWeakMapSetInGeneratedCode` was not ported.
  • Fixes microsoft/typescript-go#2086

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Nov 14 '25 00:11 Copilot