Port PR#61261: Fix accidentally reused comments between files in the emitter #1859
+12
−4
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.
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
ininternal/transformers/declarations/transform.go
to avoid creating new nodes when the module specifier hasn't actually changed. The fix:rewriteModuleSpecifier
to get the potentially rewritten specifierThis 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 thegetModuleSpecifierOverride
callbackexpressionToTypeNode.ts
- in the import type node handlingThe 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
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.