@@ -461,28 +461,32 @@ namespace ts.codefix {
461
461
const defaultExport = checker . tryGetMemberInModuleExports ( InternalSymbolName . Default , moduleSymbol ) ;
462
462
if ( defaultExport ) return { symbol : defaultExport , kind : ImportKind . Default } ;
463
463
const exportEquals = checker . resolveExternalModuleSymbol ( moduleSymbol ) ;
464
- return exportEquals === moduleSymbol ? undefined : { symbol : exportEquals , kind : getExportEqualsImportKind ( importingFile , compilerOptions , checker ) } ;
464
+ return exportEquals === moduleSymbol ? undefined : { symbol : exportEquals , kind : getExportEqualsImportKind ( importingFile , compilerOptions ) } ;
465
465
}
466
466
467
- function getExportEqualsImportKind ( importingFile : SourceFile , compilerOptions : CompilerOptions , checker : TypeChecker ) : ImportKind {
467
+ function getExportEqualsImportKind ( importingFile : SourceFile , compilerOptions : CompilerOptions ) : ImportKind {
468
+ const allowSyntheticDefaults = getAllowSyntheticDefaultImports ( compilerOptions ) ;
469
+ // 1. 'import =' will not work in es2015+, so the decision is between a default
470
+ // and a namespace import, based on allowSyntheticDefaultImports/esModuleInterop.
468
471
if ( getEmitModuleKind ( compilerOptions ) >= ModuleKind . ES2015 ) {
469
- return getAllowSyntheticDefaultImports ( compilerOptions ) ? ImportKind . Default : ImportKind . Namespace ;
472
+ return allowSyntheticDefaults ? ImportKind . Default : ImportKind . Namespace ;
470
473
}
474
+ // 2. 'import =' will not work in JavaScript, so the decision is between a default
475
+ // and const/require.
471
476
if ( isInJSFile ( importingFile ) ) {
472
477
return isExternalModule ( importingFile ) ? ImportKind . Default : ImportKind . ConstEquals ;
473
478
}
479
+ // 3. At this point the most correct choice is probably 'import =', but people
480
+ // really hate that, so look to see if the importing file has any precedent
481
+ // on how to handle it.
474
482
for ( const statement of importingFile . statements ) {
475
483
if ( isImportEqualsDeclaration ( statement ) ) {
476
484
return ImportKind . Equals ;
477
485
}
478
- if ( isImportDeclaration ( statement ) && statement . importClause && statement . importClause . name ) {
479
- const moduleSymbol = checker . getImmediateAliasedSymbol ( statement . importClause . symbol ) ;
480
- if ( moduleSymbol && moduleSymbol . name !== InternalSymbolName . Default ) {
481
- return ImportKind . Default ;
482
- }
483
- }
484
486
}
485
- return ImportKind . Equals ;
487
+ // 4. We have no precedent to go on, so just use a default import if
488
+ // allowSyntheticDefaultImports/esModuleInterop is enabled.
489
+ return allowSyntheticDefaults ? ImportKind . Default : ImportKind . Equals ;
486
490
}
487
491
488
492
function getDefaultExportInfoWorker ( defaultExport : Symbol , moduleSymbol : Symbol , checker : TypeChecker , compilerOptions : CompilerOptions ) : { readonly symbolForMeaning : Symbol , readonly name : string } | undefined {
0 commit comments