@@ -66,7 +66,9 @@ namespace ts.codefix {
66
66
const preferTypeOnlyImport = ! ! usageIsTypeOnly && compilerOptions . importsNotUsedAsValues === ImportsNotUsedAsValues . Error ;
67
67
const useRequire = shouldUseRequire ( sourceFile , program ) ;
68
68
const fix = getImportFixForSymbol ( sourceFile , exportInfos , moduleSymbol , symbolName , program , /*position*/ undefined , preferTypeOnlyImport , useRequire , host , preferences ) ;
69
- addImport ( { fixes : [ fix ] , symbolName } ) ;
69
+ if ( fix ) {
70
+ addImport ( { fixes : [ fix ] , symbolName } ) ;
71
+ }
70
72
}
71
73
72
74
function addImport ( info : FixesInfo ) {
@@ -203,7 +205,7 @@ namespace ts.codefix {
203
205
: getAllReExportingModules ( sourceFile , exportedSymbol , moduleSymbol , symbolName , host , program , /*useAutoImportProvider*/ true ) ;
204
206
const useRequire = shouldUseRequire ( sourceFile , program ) ;
205
207
const preferTypeOnlyImport = compilerOptions . importsNotUsedAsValues === ImportsNotUsedAsValues . Error && ! isSourceFileJS ( sourceFile ) && isValidTypeOnlyAliasUseSite ( getTokenAtPosition ( sourceFile , position ) ) ;
206
- const fix = getImportFixForSymbol ( sourceFile , exportInfos , moduleSymbol , symbolName , program , position , preferTypeOnlyImport , useRequire , host , preferences ) ;
208
+ const fix = Debug . checkDefined ( getImportFixForSymbol ( sourceFile , exportInfos , moduleSymbol , symbolName , program , position , preferTypeOnlyImport , useRequire , host , preferences ) ) ;
207
209
return { moduleSpecifier : fix . moduleSpecifier , codeAction : codeFixActionToCodeAction ( codeActionForFix ( { host, formatContext, preferences } , sourceFile , symbolName , fix , getQuotePreference ( sourceFile , preferences ) ) ) } ;
208
210
}
209
211
@@ -274,7 +276,7 @@ namespace ts.codefix {
274
276
program : Program ,
275
277
host : LanguageServiceHost ,
276
278
preferences : UserPreferences
277
- ) : { exportInfo ?: SymbolExportInfo , moduleSpecifier : string } {
279
+ ) : { exportInfo ?: SymbolExportInfo , moduleSpecifier : string } | undefined {
278
280
return getBestFix ( getNewImportFixes ( program , importingFile , /*position*/ undefined , /*preferTypeOnlyImport*/ false , /*useRequire*/ false , exportInfo , host , preferences ) , importingFile , host ) ;
279
281
}
280
282
@@ -529,7 +531,8 @@ namespace ts.codefix {
529
531
return sort ( fixes , ( a , b ) => compareValues ( a . kind , b . kind ) || compareModuleSpecifiers ( a , b , allowsImportingSpecifier ) ) ;
530
532
}
531
533
532
- function getBestFix < T extends ImportFix > ( fixes : readonly T [ ] , sourceFile : SourceFile , host : LanguageServiceHost ) : T {
534
+ function getBestFix < T extends ImportFix > ( fixes : readonly T [ ] , sourceFile : SourceFile , host : LanguageServiceHost ) : T | undefined {
535
+ if ( ! some ( fixes ) ) return ;
533
536
// These will always be placed first if available, and are better than other kinds
534
537
if ( fixes [ 0 ] . kind === ImportFixKind . UseNamespace || fixes [ 0 ] . kind === ImportFixKind . AddToExisting ) {
535
538
return fixes [ 0 ] ;
0 commit comments