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