diff --git a/internal/fourslash/_scripts/failingTests.txt b/internal/fourslash/_scripts/failingTests.txt index 91c5f85708..eb1fcd3f47 100644 --- a/internal/fourslash/_scripts/failingTests.txt +++ b/internal/fourslash/_scripts/failingTests.txt @@ -26,7 +26,6 @@ TestAutoImportProvider9 TestAutoImportRelativePathToMonorepoPackage TestAutoImportSortCaseSensitivity1 TestAutoImportTypeImport4 -TestAutoImportTypeOnlyPreferred2 TestAutoImportVerbatimTypeOnly1 TestBestCommonTypeObjectLiterals TestBestCommonTypeObjectLiterals1 @@ -232,7 +231,6 @@ TestImportFixesGlobalTypingsCache TestImportMetaCompletionDetails TestImportNameCodeFix_avoidRelativeNodeModules TestImportNameCodeFix_externalNonRelative1 -TestImportNameCodeFix_importType2 TestImportNameCodeFix_noDestructureNonObjectLiteral TestImportNameCodeFix_order2 TestImportNameCodeFix_preferBaseUrl diff --git a/internal/ls/autoimport/fix.go b/internal/ls/autoimport/fix.go index b2107443f3..800245f903 100644 --- a/internal/ls/autoimport/fix.go +++ b/internal/ls/autoimport/fix.go @@ -695,6 +695,7 @@ 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 @@ -702,7 +703,7 @@ func (v *View) tryAddToExistingImport( 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(), @@ -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 } @@ -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 } @@ -740,7 +751,7 @@ func (v *View) tryAddToExistingImport( continue } - return &Fix{ + fix := &Fix{ AutoImportFix: &lsproto.AutoImportFix{ Kind: lsproto.AutoImportFixKindAddToExisting, Name: export.Name(), @@ -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 {