@@ -184,8 +184,10 @@ namespace ts.codefix {
184
184
Equals
185
185
}
186
186
187
- export function getCodeActionForImport ( moduleSymbol : Symbol , context : ImportCodeFixOptions ) : ImportCodeAction [ ] {
188
- const declarations = getImportDeclarations ( moduleSymbol , context . checker , context . sourceFile , context . cachedImportDeclarations ) ;
187
+ export function getCodeActionForImport ( moduleSymbols : Symbol | ReadonlyArray < Symbol > , context : ImportCodeFixOptions ) : ImportCodeAction [ ] {
188
+ moduleSymbols = toArray ( moduleSymbols ) ;
189
+ const declarations = flatMap ( moduleSymbols , moduleSymbol =>
190
+ getImportDeclarations ( moduleSymbol , context . checker , context . sourceFile , context . cachedImportDeclarations ) ) ;
189
191
const actions : ImportCodeAction [ ] = [ ] ;
190
192
if ( context . symbolToken ) {
191
193
// It is possible that multiple import statements with the same specifier exist in the file.
@@ -207,7 +209,7 @@ namespace ts.codefix {
207
209
}
208
210
}
209
211
}
210
- actions . push ( getCodeActionForAddImport ( moduleSymbol , context , declarations ) ) ;
212
+ actions . push ( getCodeActionForAddImport ( moduleSymbols , context , declarations ) ) ;
211
213
return actions ;
212
214
}
213
215
@@ -313,16 +315,19 @@ namespace ts.codefix {
313
315
}
314
316
}
315
317
316
- export function getModuleSpecifierForNewImport ( sourceFile : SourceFile , moduleSymbol : Symbol , options : CompilerOptions , getCanonicalFileName : ( file : string ) => string , host : LanguageServiceHost ) : string | undefined {
317
- const moduleFileName = moduleSymbol . valueDeclaration . getSourceFile ( ) . fileName ;
318
- const sourceDirectory = getDirectoryPath ( sourceFile . fileName ) ;
318
+ export function getModuleSpecifierForNewImport ( sourceFile : SourceFile , moduleSymbols : ReadonlyArray < Symbol > , options : CompilerOptions , getCanonicalFileName : ( file : string ) => string , host : LanguageServiceHost ) : string | undefined {
319
+ const choices = mapIterator ( arrayIterator ( moduleSymbols ) , moduleSymbol => {
320
+ const moduleFileName = moduleSymbol . valueDeclaration . getSourceFile ( ) . fileName ;
321
+ const sourceDirectory = getDirectoryPath ( sourceFile . fileName ) ;
319
322
320
- return tryGetModuleNameFromAmbientModule ( moduleSymbol ) ||
321
- tryGetModuleNameFromTypeRoots ( options , host , getCanonicalFileName , moduleFileName ) ||
322
- tryGetModuleNameAsNodeModule ( options , moduleFileName , host , getCanonicalFileName , sourceDirectory ) ||
323
- tryGetModuleNameFromBaseUrl ( options , moduleFileName , getCanonicalFileName ) ||
324
- options . rootDirs && tryGetModuleNameFromRootDirs ( options . rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ||
325
- removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , options ) ;
323
+ return tryGetModuleNameFromAmbientModule ( moduleSymbol ) ||
324
+ tryGetModuleNameFromTypeRoots ( options , host , getCanonicalFileName , moduleFileName ) ||
325
+ tryGetModuleNameAsNodeModule ( options , moduleFileName , host , getCanonicalFileName , sourceDirectory ) ||
326
+ tryGetModuleNameFromBaseUrl ( options , moduleFileName , getCanonicalFileName ) ||
327
+ options . rootDirs && tryGetModuleNameFromRootDirs ( options . rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ||
328
+ removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , options ) ;
329
+ } ) ;
330
+ return best ( choices , ( a , b ) => a . length < b . length ) ;
326
331
}
327
332
328
333
function tryGetModuleNameFromAmbientModule ( moduleSymbol : Symbol ) : string | undefined {
@@ -543,7 +548,7 @@ namespace ts.codefix {
543
548
}
544
549
545
550
function getCodeActionForAddImport (
546
- moduleSymbol : Symbol ,
551
+ moduleSymbols : ReadonlyArray < Symbol > ,
547
552
ctx : ImportCodeFixOptions ,
548
553
declarations : ReadonlyArray < AnyImportSyntax > ) : ImportCodeAction {
549
554
const fromExistingImport = firstDefined ( declarations , declaration => {
@@ -565,7 +570,7 @@ namespace ts.codefix {
565
570
}
566
571
567
572
const moduleSpecifier = firstDefined ( declarations , moduleSpecifierFromAnyImport )
568
- || getModuleSpecifierForNewImport ( ctx . sourceFile , moduleSymbol , ctx . compilerOptions , ctx . getCanonicalFileName , ctx . host ) ;
573
+ || getModuleSpecifierForNewImport ( ctx . sourceFile , moduleSymbols , ctx . compilerOptions , ctx . getCanonicalFileName , ctx . host ) ;
569
574
return getCodeActionForNewImport ( ctx , moduleSpecifier ) ;
570
575
}
571
576
@@ -659,24 +664,33 @@ namespace ts.codefix {
659
664
symbolName = symbol . name ;
660
665
}
661
666
else {
662
- Debug . fail ( "Either the symbol or the JSX namespace should be a UMD global if we got here" ) ;
667
+ throw Debug . fail ( "Either the symbol or the JSX namespace should be a UMD global if we got here" ) ;
663
668
}
664
669
665
- const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports ( compilerOptions ) ;
666
-
670
+ return getCodeActionForImport ( symbol , { ...context , symbolName, kind : getUmdImportKind ( compilerOptions ) } ) ;
671
+ }
672
+ function getUmdImportKind ( compilerOptions : CompilerOptions ) {
667
673
// Import a synthetic `default` if enabled.
668
- if ( allowSyntheticDefaultImports ) {
669
- return getCodeActionForImport ( symbol , { ... context , symbolName , kind : ImportKind . Default } ) ;
674
+ if ( getAllowSyntheticDefaultImports ( compilerOptions ) ) {
675
+ return ImportKind . Default ;
670
676
}
671
- const moduleKind = getEmitModuleKind ( compilerOptions ) ;
672
677
673
678
// When a synthetic `default` is unavailable, use `import..require` if the module kind supports it.
674
- if ( moduleKind === ModuleKind . AMD || moduleKind === ModuleKind . CommonJS || moduleKind === ModuleKind . UMD ) {
675
- return getCodeActionForImport ( symbol , { ...context , symbolName, kind : ImportKind . Equals } ) ;
679
+ const moduleKind = getEmitModuleKind ( compilerOptions ) ;
680
+ switch ( moduleKind ) {
681
+ case ModuleKind . AMD :
682
+ case ModuleKind . CommonJS :
683
+ case ModuleKind . UMD :
684
+ return ImportKind . Equals ;
685
+ case ModuleKind . System :
686
+ case ModuleKind . ES2015 :
687
+ case ModuleKind . ESNext :
688
+ case ModuleKind . None :
689
+ // Fall back to the `import * as ns` style import.
690
+ return ImportKind . Namespace ;
691
+ default :
692
+ throw Debug . assertNever ( moduleKind ) ;
676
693
}
677
-
678
- // Fall back to the `import * as ns` style import.
679
- return getCodeActionForImport ( symbol , { ...context , symbolName, kind : ImportKind . Namespace } ) ;
680
694
}
681
695
682
696
function getActionsForNonUMDImport ( context : ImportCodeFixContext , allSourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken ) : ImportCodeAction [ ] {
0 commit comments