@@ -184,8 +184,10 @@ namespace ts.codefix {
184184 Equals
185185 }
186186
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 ) ) ;
189191 const actions : ImportCodeAction [ ] = [ ] ;
190192 if ( context . symbolToken ) {
191193 // It is possible that multiple import statements with the same specifier exist in the file.
@@ -207,7 +209,7 @@ namespace ts.codefix {
207209 }
208210 }
209211 }
210- actions . push ( getCodeActionForAddImport ( moduleSymbol , context , declarations ) ) ;
212+ actions . push ( getCodeActionForAddImport ( moduleSymbols , context , declarations ) ) ;
211213 return actions ;
212214 }
213215
@@ -313,16 +315,19 @@ namespace ts.codefix {
313315 }
314316 }
315317
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 ) ;
319322
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 ) ;
326331 }
327332
328333 function tryGetModuleNameFromAmbientModule ( moduleSymbol : Symbol ) : string | undefined {
@@ -543,7 +548,7 @@ namespace ts.codefix {
543548 }
544549
545550 function getCodeActionForAddImport (
546- moduleSymbol : Symbol ,
551+ moduleSymbols : ReadonlyArray < Symbol > ,
547552 ctx : ImportCodeFixOptions ,
548553 declarations : ReadonlyArray < AnyImportSyntax > ) : ImportCodeAction {
549554 const fromExistingImport = firstDefined ( declarations , declaration => {
@@ -565,7 +570,7 @@ namespace ts.codefix {
565570 }
566571
567572 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 ) ;
569574 return getCodeActionForNewImport ( ctx , moduleSpecifier ) ;
570575 }
571576
@@ -659,24 +664,33 @@ namespace ts.codefix {
659664 symbolName = symbol . name ;
660665 }
661666 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" ) ;
663668 }
664669
665- const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports ( compilerOptions ) ;
666-
670+ return getCodeActionForImport ( symbol , { ...context , symbolName, kind : getUmdImportKind ( compilerOptions ) } ) ;
671+ }
672+ function getUmdImportKind ( compilerOptions : CompilerOptions ) {
667673 // 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 ;
670676 }
671- const moduleKind = getEmitModuleKind ( compilerOptions ) ;
672677
673678 // 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 ) ;
676693 }
677-
678- // Fall back to the `import * as ns` style import.
679- return getCodeActionForImport ( symbol , { ...context , symbolName, kind : ImportKind . Namespace } ) ;
680694 }
681695
682696 function getActionsForNonUMDImport ( context : ImportCodeFixContext , allSourceFiles : ReadonlyArray < SourceFile > , cancellationToken : CancellationToken ) : ImportCodeAction [ ] {
0 commit comments