@@ -560,17 +560,21 @@ namespace ts.codefix {
560
560
: undefined ;
561
561
}
562
562
563
- export function getImportKind ( importingFile : SourceFile , exportKind : ExportKind , compilerOptions : CompilerOptions ) : ImportKind {
563
+ /**
564
+ * @param forceImportKeyword Indicates that the user has already typed `import`, so the result must start with `import`.
565
+ * (In other words, do not allow `const x = require("...")` for JS files.)
566
+ */
567
+ export function getImportKind ( importingFile : SourceFile , exportKind : ExportKind , compilerOptions : CompilerOptions , forceImportKeyword ?: boolean ) : ImportKind {
564
568
switch ( exportKind ) {
565
569
case ExportKind . Named : return ImportKind . Named ;
566
570
case ExportKind . Default : return ImportKind . Default ;
567
- case ExportKind . ExportEquals : return getExportEqualsImportKind ( importingFile , compilerOptions ) ;
568
- case ExportKind . UMD : return getUmdImportKind ( importingFile , compilerOptions ) ;
571
+ case ExportKind . ExportEquals : return getExportEqualsImportKind ( importingFile , compilerOptions , ! ! forceImportKeyword ) ;
572
+ case ExportKind . UMD : return getUmdImportKind ( importingFile , compilerOptions , ! ! forceImportKeyword ) ;
569
573
default : return Debug . assertNever ( exportKind ) ;
570
574
}
571
575
}
572
576
573
- function getUmdImportKind ( importingFile : SourceFile , compilerOptions : CompilerOptions ) : ImportKind {
577
+ function getUmdImportKind ( importingFile : SourceFile , compilerOptions : CompilerOptions , forceImportKeyword : boolean ) : ImportKind {
574
578
// Import a synthetic `default` if enabled.
575
579
if ( getAllowSyntheticDefaultImports ( compilerOptions ) ) {
576
580
return ImportKind . Default ;
@@ -583,7 +587,7 @@ namespace ts.codefix {
583
587
case ModuleKind . CommonJS :
584
588
case ModuleKind . UMD :
585
589
if ( isInJSFile ( importingFile ) ) {
586
- return isExternalModule ( importingFile ) ? ImportKind . Namespace : ImportKind . CommonJS ;
590
+ return isExternalModule ( importingFile ) || forceImportKeyword ? ImportKind . Namespace : ImportKind . CommonJS ;
587
591
}
588
592
return ImportKind . CommonJS ;
589
593
case ModuleKind . System :
@@ -671,18 +675,20 @@ namespace ts.codefix {
671
675
return originalSymbolToExportInfos ;
672
676
}
673
677
674
- function getExportEqualsImportKind ( importingFile : SourceFile , compilerOptions : CompilerOptions ) : ImportKind {
678
+ function getExportEqualsImportKind ( importingFile : SourceFile , compilerOptions : CompilerOptions , forceImportKeyword : boolean ) : ImportKind {
675
679
const allowSyntheticDefaults = getAllowSyntheticDefaultImports ( compilerOptions ) ;
676
680
const isJS = isInJSFile ( importingFile ) ;
677
681
// 1. 'import =' will not work in es2015+ TS files, so the decision is between a default
678
682
// and a namespace import, based on allowSyntheticDefaultImports/esModuleInterop.
679
683
if ( ! isJS && getEmitModuleKind ( compilerOptions ) >= ModuleKind . ES2015 ) {
680
684
return allowSyntheticDefaults ? ImportKind . Default : ImportKind . Namespace ;
681
685
}
682
- // 2. 'import =' will not work in JavaScript, so the decision is between a default
683
- // and const/require.
686
+ // 2. 'import =' will not work in JavaScript, so the decision is between a default import,
687
+ // a namespace import, and const/require.
684
688
if ( isJS ) {
685
- return isExternalModule ( importingFile ) ? ImportKind . Default : ImportKind . CommonJS ;
689
+ return isExternalModule ( importingFile ) || forceImportKeyword
690
+ ? allowSyntheticDefaults ? ImportKind . Default : ImportKind . Namespace
691
+ : ImportKind . CommonJS ;
686
692
}
687
693
// 3. At this point the most correct choice is probably 'import =', but people
688
694
// really hate that, so look to see if the importing file has any precedent
0 commit comments