Fix auto-import to prefer existing import type declarations#3244
Open
Fix auto-import to prefer existing import type declarations#3244
import type declarations#3244Conversation
When adding a type import, the tryAddToExistingImport function now gives preference to existing `import type` declarations instead of returning the first valid match. This matches the TypeScript reference implementation behavior. Also adds missing check for default imports when addAsTypeOnly is Required and namedBindings exist. Fixes #3029 Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/a6cc4bfe-4eb1-48f4-8a31-62a398c4fbca Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix auto-import to merge existing import type declarations
Fix auto-import to prefer existing Mar 26, 2026
import type declarations
Member
|
@copilot since this causes existing tests to be removed from the failing list, you can delete your new test. |
Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/b33f7744-e985-4803-99ef-7d2ff91321aa Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
Contributor
Author
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Go port’s auto-import “add to existing import” selection logic to prefer merging type-only additions into existing import type declarations (matching TypeScript’s reference behavior), rather than always picking the first matching import.
Changes:
- Update
tryAddToExistingImportto track a “best” candidate and prefer a “perfect match” (type-only import for type additions; value import for value additions) instead of returning the first match. - Add a missing guard to prevent attempting to add a type-only default import to an existing import that already has named bindings (which the current edit-application logic can’t safely express as type-only).
- Remove two entries from the fourslash “known failing tests” list because they now pass.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
internal/ls/autoimport/fix.go |
Adjusts existing-import selection to prefer import type for type additions and adds a safety guard for type-only default imports. |
internal/fourslash/_scripts/failingTests.txt |
Removes two tests from the known-failures list after the behavior fix. |
andrewbranch
approved these changes
Mar 26, 2026
jakebailey
approved these changes
Mar 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
tryAddToExistingImportreturned the first valid matching import declaration, which meant a value import appearing before animport typewould always win. Types would get added as inlinetypemodifiers on the value import instead of merging into the existingimport typeblock.Changes
internal/ls/autoimport/fix.go: Port the preference logic from the TS reference (importFixes.tsL1101–1117). Instead of returning the first valid match, track abestcandidate and return immediately on a "perfect match" — type-only imports for type additions, value imports for value additions. Also added the missing guard foraddAsTypeOnly == Required && namedBindings != nilon default imports.internal/fourslash/_scripts/failingTests.txt: RemovedTestAutoImportTypeOnlyPreferred2andTestImportNameCodeFix_importType2— both now pass.📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.