@@ -240,6 +240,7 @@ namespace ts.codefix {
240
240
preferences : UserPreferences ,
241
241
) : ReadonlyArray < NewImportInfo > {
242
242
const { baseUrl, paths, rootDirs } = compilerOptions ;
243
+ const moduleResolutionKind = getEmitModuleResolutionKind ( compilerOptions ) ;
243
244
const addJsExtension = usesJsExtensionOnImports ( sourceFile ) ;
244
245
const choicesForEachExportingModule = flatMap < SymbolExportInfo , NewImportInfo [ ] > ( moduleSymbols , ( { moduleSymbol, importKind } ) => {
245
246
const modulePathsGroups = getAllModulePaths ( program , moduleSymbol . valueDeclaration . getSourceFile ( ) ) . map ( moduleFileName => {
@@ -252,7 +253,7 @@ namespace ts.codefix {
252
253
return [ global ] ;
253
254
}
254
255
255
- const relativePath = removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , compilerOptions , addJsExtension ) ;
256
+ const relativePath = removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , moduleResolutionKind , addJsExtension ) ;
256
257
if ( ! baseUrl || preferences . importModuleSpecifierPreference === "relative" ) {
257
258
return [ relativePath ] ;
258
259
}
@@ -262,7 +263,7 @@ namespace ts.codefix {
262
263
return [ relativePath ] ;
263
264
}
264
265
265
- const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , compilerOptions , addJsExtension ) ;
266
+ const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , moduleResolutionKind , addJsExtension ) ;
266
267
if ( paths ) {
267
268
const fromPaths = tryGetModuleNameFromPaths ( removeFileExtension ( relativeToBaseUrl ) , importRelativeToBaseUrl , paths ) ;
268
269
if ( fromPaths ) {
@@ -390,7 +391,8 @@ namespace ts.codefix {
390
391
return firstDefined ( roots , unNormalizedTypeRoot => {
391
392
const typeRoot = toPath ( unNormalizedTypeRoot , /*basePath*/ undefined , getCanonicalFileName ) ;
392
393
if ( startsWith ( moduleFileName , typeRoot ) ) {
393
- return removeExtensionAndIndexPostFix ( moduleFileName . substring ( typeRoot . length + 1 ) , options , addJsExtension ) ;
394
+ // For a type definition, we can strip `/index` even with classic resolution.
395
+ return removeExtensionAndIndexPostFix ( moduleFileName . substring ( typeRoot . length + 1 ) , ModuleResolutionKind . NodeJs , addJsExtension ) ;
394
396
}
395
397
} ) ;
396
398
}
@@ -527,11 +529,11 @@ namespace ts.codefix {
527
529
} ) ;
528
530
}
529
531
530
- function removeExtensionAndIndexPostFix ( fileName : string , options : CompilerOptions , addJsExtension : boolean ) : string {
532
+ function removeExtensionAndIndexPostFix ( fileName : string , moduleResolutionKind : ModuleResolutionKind , addJsExtension : boolean ) : string {
531
533
const noExtension = removeFileExtension ( fileName ) ;
532
534
return addJsExtension
533
535
? noExtension + ".js"
534
- : getEmitModuleResolutionKind ( options ) === ModuleResolutionKind . NodeJs
536
+ : moduleResolutionKind === ModuleResolutionKind . NodeJs
535
537
? removeSuffix ( noExtension , "/index" )
536
538
: noExtension ;
537
539
}
0 commit comments