Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 9, 2025

This PR ports microsoft/TypeScript#61261 which fixes a bug where comments were accidentally reused between files in the declaration emitter.

Problem

When emitting declaration files, the emitter could accidentally reuse comment ranges from one file in another file. This happened when import type nodes were updated unnecessarily, causing text ranges (including comments) to be shared between different source files.

Solution

Modified transformImportTypeNode in internal/transformers/declarations/transform.go to avoid creating new nodes when the module specifier hasn't actually changed. The fix:

  1. Calls rewriteModuleSpecifier to get the potentially rewritten specifier
  2. Uses pointer equality to check if the specifier is the same as the original
  3. If unchanged, reuses the original argument node to preserve its text ranges
  4. If changed, creates a new literal type node with the new specifier

This prevents comment ranges from being incorrectly shared between files by avoiding unnecessary node creation and text range copying.

Differences from TypeScript Implementation

The original TypeScript PR made changes in two locations:

  • checker.ts - in the getModuleSpecifierOverride callback
  • expressionToTypeNode.ts - in the import type node handling

The Go port only required changes to the declarations transformer because the Go codebase has a different architecture where module specifier rewriting is consolidated in the transformer rather than split between the checker and syntactic type node builder.

Testing

All existing tests pass, including the three test cases added in the original TypeScript PR:

  • declarationEmitNoInvalidCommentReuse1.ts
  • declarationEmitNoInvalidCommentReuse2.ts
  • declarationEmitNoInvalidCommentReuse3.ts

Fixes the underlying issue described in microsoft/TypeScript#61239.

Original prompt

From the TypeScript repo, port PR#61261

Note

Custom agent used: Strada to Corsa Port Expert
A Go and TypeScript expert who can easily figure out how to port PRs from one language to another


✨ 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 Copilot AI changed the title [WIP] Port changes from PR#61261 in TypeScript repository Port PR#61261: Fix accidentally reused comments between files in the emitter Oct 9, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh October 9, 2025 21:44
Copilot finished work on behalf of RyanCavanaugh October 9, 2025 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants