Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions internal/fourslash/_scripts/failingTests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ TestAutoImportProvider9
TestAutoImportRelativePathToMonorepoPackage
TestAutoImportSortCaseSensitivity1
TestAutoImportTypeImport4
TestAutoImportTypeOnlyPreferred2
TestAutoImportVerbatimTypeOnly1
TestBestCommonTypeObjectLiterals
TestBestCommonTypeObjectLiterals1
Expand Down Expand Up @@ -232,7 +231,6 @@ TestImportFixesGlobalTypingsCache
TestImportMetaCompletionDetails
TestImportNameCodeFix_avoidRelativeNodeModules
TestImportNameCodeFix_externalNonRelative1
TestImportNameCodeFix_importType2
TestImportNameCodeFix_noDestructureNonObjectLiteral
TestImportNameCodeFix_order2
TestImportNameCodeFix_preferBaseUrl
Expand Down
32 changes: 27 additions & 5 deletions internal/ls/autoimport/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,14 +695,15 @@ func (v *View) tryAddToExistingImport(

addAsTypeOnly := getAddAsTypeOnly(isValidTypeOnlyUseSite, export, v.program.Options())

var best *Fix
for _, existingImport := range matchingDeclarations {
if existingImport.node.Kind == ast.KindImportEqualsDeclaration {
continue
}

if existingImport.node.Kind == ast.KindVariableDeclaration {
if (importKind == lsproto.ImportKindNamed || importKind == lsproto.ImportKindDefault) && existingImport.node.Name().Kind == ast.KindObjectBindingPattern {
return &Fix{
fix := &Fix{
AutoImportFix: &lsproto.AutoImportFix{
Kind: lsproto.AutoImportFixKindAddToExisting,
Name: export.Name(),
Expand All @@ -712,6 +713,15 @@ func (v *View) tryAddToExistingImport(
AddAsTypeOnly: addAsTypeOnly,
},
}
// Variable declarations are never type-only.
// Give preference to putting types in existing type-only imports and avoiding conversions
// of import statements to/from type-only.
if addAsTypeOnly == lsproto.AddAsTypeOnlyNotAllowed {
return fix
}
if best == nil {
best = fix
}
}
continue
}
Expand All @@ -730,8 +740,9 @@ func (v *View) tryAddToExistingImport(
continue
}

if importKind == lsproto.ImportKindDefault && importClause.Name() != nil {
// Cannot add a default import to a declaration that already has one
if importKind == lsproto.ImportKindDefault && (importClause.Name() != nil ||
// Cannot add a default import as type-only if the import already has named bindings
addAsTypeOnly == lsproto.AddAsTypeOnlyRequired && namedBindings != nil) {
continue
}

Expand All @@ -740,7 +751,7 @@ func (v *View) tryAddToExistingImport(
continue
}

return &Fix{
fix := &Fix{
AutoImportFix: &lsproto.AutoImportFix{
Kind: lsproto.AutoImportFixKindAddToExisting,
Name: export.Name(),
Expand All @@ -750,9 +761,20 @@ func (v *View) tryAddToExistingImport(
AddAsTypeOnly: addAsTypeOnly,
},
}

isTypeOnly := importClause.IsTypeOnly()
// Give preference to putting types in existing type-only imports and avoiding conversions
// of import statements to/from type-only.
if (addAsTypeOnly != lsproto.AddAsTypeOnlyNotAllowed && isTypeOnly) ||
(addAsTypeOnly == lsproto.AddAsTypeOnlyNotAllowed && !isTypeOnly) {
return fix
}
if best == nil {
best = fix
}
}

return nil
return best
}

func getImportKind(importingFile *ast.SourceFile, export *Export, program *compiler.Program) lsproto.ImportKind {
Expand Down
Loading