Port codeactions import fixes to new autoimport stack
Port codeactions import fixes to new autoimport stack
Summary
This PR continues the migration of auto-import codefixes from the old SymbolExportInfo-based system to the new autoimport.Registry/autoimport.View infrastructure.
Changes
JSDoc import type fixes
- Added
UsagePositionfield toFixstruct - Added
usagePositionparameter toGetFixes()function - Implemented
AutoImportFixKindJsdocTypeImportcase inEdits()to insertimport("./a").Tprefix - For JS files with pure type exports, uses inline
import()syntax instead of adding import statements
Type-only import handling
- Added
isValidTypeOnlyUseSiteparameter toGetFixes()andGetCompletions() - Added
getAddAsTypeOnly()function to compute theAddAsTypeOnlyvalue - Fixed
AddToExistingto properly useaddAsTypeOnlywhen creating import specifiers - Fixed
topLevelTypeOnlycondition to handle named-imports-only case
Bug fixes
- Fixed panic in
tryAddToExistingImportfor side-effect imports (check for nil ImportClause)
Tests now passing
- TestImportNameCodeFix_importType (JSDoc import type in JS files)
- TestImportNameCodeFix_importType3 (type-only import specifiers)
- TestImportNameCodeFixExistingImport5 (side-effect imports)
- TestImportNameCodeFix_typeOnly
Tests remaining: 19 failing
Most remaining failures are related to:
- Default export handling
- CommonJS/synthetic imports
- JSX-related imports
- Namespace qualification fixes
Original prompt
I'm in the middle of a rewrite of auto-imports. Auto-imports is a feature exposed by two language service features: completions and codefixes. #file:completions.go has already been rewritten to use the new stack, located in the #file:autoimport directory. #file:codeactions_importfixes.go is codefixes, and I just barely started to swap it over to the new stack. I want you to work on continuing the port. The current code calls into many functions in #file:autoimport.go, #file:autoimportsexportinfo.go, and #file:autoimportfixes.go, which will eventually be completely deleted. The export info map in particular is fully supplanted by the new data structure (autoimport.Registry, exposed through autoimport.View). A lot of things on the export-finding part of the pipeline will have to be rewritten or adapted because the new data structure doesn't store Symbols or Nodes directly, instead cheaply pre-computing relevant info and storing it in the registry. Much of the fix-generation code, though, can be reused with modest adaptations. Many functions have already been copied and adapted into #file:fix.go. More functions will likely need to be copied from the current auto-import files in
lsinto thels/autoimportdirectory. Do not delete any of the old stack yet, just copy and adapt as necessary.Important: I do not expect you to cleanly finish this job, I just want you to make some progress on it. I will finish later, so don't make any big design decisions. In particular, do whatever you can do without modifying the autoimport.Registry or autoimport.exportExtractor. There may be some features that cannot be completed without making changes to the export extractor, but just do what you can without touching that.
The user has attached the following file paths as relevant context:
- internal/ls/autoimport/fix.go
- internal/ls/autoimportfixes.go
- internal/ls/autoimportsexportinfo.go
- internal/project/autoimport.go
- internal/ls/codeactions_importfixes.go
- internal/ls/autoimport
- internal/ls/completions.go
- .github/copilot-instructions.md
Created from VS Code.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.