@@ -302,6 +302,10 @@ namespace ts.codefix {
302
302
return literal ;
303
303
}
304
304
305
+ function usesJsExtensionOnImports ( sourceFile : SourceFile ) : boolean {
306
+ return firstDefined ( sourceFile . imports , ( { text } ) => pathIsRelative ( text ) ? fileExtensionIs ( text , Extension . Js ) : undefined ) || false ;
307
+ }
308
+
305
309
function createImportClauseOfKind ( kind : ImportKind . Default | ImportKind . Named | ImportKind . Namespace , symbolName : string ) {
306
310
const id = createIdentifier ( symbolName ) ;
307
311
switch ( kind ) {
@@ -325,18 +329,19 @@ namespace ts.codefix {
325
329
host : LanguageServiceHost ,
326
330
) : string [ ] {
327
331
const { baseUrl, paths, rootDirs } = options ;
332
+ const addJsExtension = usesJsExtensionOnImports ( sourceFile ) ;
328
333
const choicesForEachExportingModule = flatMap ( moduleSymbols , moduleSymbol =>
329
334
getAllModulePaths ( program , moduleSymbol . valueDeclaration . getSourceFile ( ) ) . map ( moduleFileName => {
330
335
const sourceDirectory = getDirectoryPath ( sourceFile . fileName ) ;
331
336
const global = tryGetModuleNameFromAmbientModule ( moduleSymbol )
332
- || tryGetModuleNameFromTypeRoots ( options , host , getCanonicalFileName , moduleFileName )
337
+ || tryGetModuleNameFromTypeRoots ( options , host , getCanonicalFileName , moduleFileName , addJsExtension )
333
338
|| tryGetModuleNameAsNodeModule ( options , moduleFileName , host , getCanonicalFileName , sourceDirectory )
334
339
|| rootDirs && tryGetModuleNameFromRootDirs ( rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ;
335
340
if ( global ) {
336
341
return [ global ] ;
337
342
}
338
343
339
- const relativePath = removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , options ) ;
344
+ const relativePath = removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , options , addJsExtension ) ;
340
345
if ( ! baseUrl ) {
341
346
return [ relativePath ] ;
342
347
}
@@ -346,7 +351,7 @@ namespace ts.codefix {
346
351
return [ relativePath ] ;
347
352
}
348
353
349
- const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , options ) ;
354
+ const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , options , addJsExtension ) ;
350
355
if ( paths ) {
351
356
const fromPaths = tryGetModuleNameFromPaths ( removeFileExtension ( relativeToBaseUrl ) , importRelativeToBaseUrl , paths ) ;
352
357
if ( fromPaths ) {
@@ -455,12 +460,13 @@ namespace ts.codefix {
455
460
host : GetEffectiveTypeRootsHost ,
456
461
getCanonicalFileName : ( file : string ) => string ,
457
462
moduleFileName : string ,
463
+ addJsExtension : boolean ,
458
464
) : string | undefined {
459
465
const roots = getEffectiveTypeRoots ( options , host ) ;
460
466
return roots && firstDefined ( roots , unNormalizedTypeRoot => {
461
467
const typeRoot = toPath ( unNormalizedTypeRoot , /*basePath*/ undefined , getCanonicalFileName ) ;
462
468
if ( startsWith ( moduleFileName , typeRoot ) ) {
463
- return removeExtensionAndIndexPostFix ( moduleFileName . substring ( typeRoot . length + 1 ) , options ) ;
469
+ return removeExtensionAndIndexPostFix ( moduleFileName . substring ( typeRoot . length + 1 ) , options , addJsExtension ) ;
464
470
}
465
471
} ) ;
466
472
}
@@ -594,9 +600,13 @@ namespace ts.codefix {
594
600
return firstDefined ( rootDirs , rootDir => getRelativePathIfInDirectory ( path , rootDir , getCanonicalFileName ) ) ;
595
601
}
596
602
597
- function removeExtensionAndIndexPostFix ( fileName : string , options : CompilerOptions ) : string {
603
+ function removeExtensionAndIndexPostFix ( fileName : string , options : CompilerOptions , addJsExtension : boolean ) : string {
598
604
const noExtension = removeFileExtension ( fileName ) ;
599
- return getEmitModuleResolutionKind ( options ) === ModuleResolutionKind . NodeJs ? removeSuffix ( noExtension , "/index" ) : noExtension ;
605
+ return addJsExtension
606
+ ? noExtension + ".js"
607
+ : getEmitModuleResolutionKind ( options ) === ModuleResolutionKind . NodeJs
608
+ ? removeSuffix ( noExtension , "/index" )
609
+ : noExtension ;
600
610
}
601
611
602
612
function getRelativePathIfInDirectory ( path : string , directoryPath : string , getCanonicalFileName : GetCanonicalFileName ) : string | undefined {
0 commit comments