@@ -60,7 +60,7 @@ function getPreferences(
60
60
}
61
61
switch ( preferredEnding ) {
62
62
case ModuleSpecifierEnding . JsExtension : return [ ModuleSpecifierEnding . JsExtension , ModuleSpecifierEnding . Minimal , ModuleSpecifierEnding . Index ] ;
63
- case ModuleSpecifierEnding . TsExtension : return [ ModuleSpecifierEnding . TsExtension , ModuleSpecifierEnding . TsExtension , ModuleSpecifierEnding . Minimal , ModuleSpecifierEnding . Index ] ;
63
+ case ModuleSpecifierEnding . TsExtension : return [ ModuleSpecifierEnding . TsExtension , ModuleSpecifierEnding . Minimal , ModuleSpecifierEnding . JsExtension , ModuleSpecifierEnding . Index ] ;
64
64
case ModuleSpecifierEnding . Index : return [ ModuleSpecifierEnding . Index , ModuleSpecifierEnding . Minimal , ModuleSpecifierEnding . JsExtension ] ;
65
65
case ModuleSpecifierEnding . Minimal : return [ ModuleSpecifierEnding . Minimal , ModuleSpecifierEnding . Index , ModuleSpecifierEnding . JsExtension ] ;
66
66
default : Debug . assertNever ( preferredEnding ) ;
@@ -367,7 +367,7 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
367
367
return fromPaths ;
368
368
}
369
369
370
- const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? processEnding ( relativeToBaseUrl , ending , compilerOptions ) : fromPaths ;
370
+ const maybeNonRelative = fromPaths === undefined && baseUrl !== undefined ? processEnding ( relativeToBaseUrl , allowedEndings , compilerOptions ) : fromPaths ;
371
371
if ( ! maybeNonRelative ) {
372
372
return relativePath ;
373
373
}
@@ -655,7 +655,7 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
655
655
// sorted among the others for a particular value of `importModuleSpecifierEnding`.
656
656
const candidates : { ending : ModuleSpecifierEnding | undefined , value : string } [ ] = allowedEndings . map ( ending => ( {
657
657
ending,
658
- value : processEnding ( relativeToBaseUrl , ending , compilerOptions )
658
+ value : processEnding ( relativeToBaseUrl , [ ending ] , compilerOptions )
659
659
} ) ) ;
660
660
if ( tryGetExtensionFromPath ( pattern ) ) {
661
661
candidates . push ( { ending : undefined , value : relativeToBaseUrl } ) ;
@@ -692,7 +692,7 @@ function tryGetModuleNameFromPaths(relativeToBaseUrl: string, paths: MapLike<rea
692
692
// `ModuleSpecifierEnding.Index` result, which should already be in the list of candidates if `Minimal` was. (Note: the assumption here is
693
693
// that every module resolution mode that supports dropping extensions also supports dropping `/index`. Like literally
694
694
// everything else in this file, this logic needs to be updated if that's not true in some future module resolution mode.)
695
- return ending !== ModuleSpecifierEnding . Minimal || value === processEnding ( relativeToBaseUrl , ending , compilerOptions , host ) ;
695
+ return ending !== ModuleSpecifierEnding . Minimal || value === processEnding ( relativeToBaseUrl , [ ending ] , compilerOptions , host ) ;
696
696
}
697
697
}
698
698
@@ -767,7 +767,7 @@ function tryGetModuleNameFromExports(options: CompilerOptions, targetFilePath: s
767
767
return undefined ;
768
768
}
769
769
770
- function tryGetModuleNameFromRootDirs ( rootDirs : readonly string [ ] , moduleFileName : string , sourceDirectory : string , getCanonicalFileName : ( file : string ) => string , ending : ModuleSpecifierEnding , compilerOptions : CompilerOptions ) : string | undefined {
770
+ function tryGetModuleNameFromRootDirs ( rootDirs : readonly string [ ] , moduleFileName : string , sourceDirectory : string , getCanonicalFileName : ( file : string ) => string , allowedEndings : readonly ModuleSpecifierEnding [ ] , compilerOptions : CompilerOptions ) : string | undefined {
771
771
const normalizedTargetPaths = getPathsRelativeToRootDirs ( moduleFileName , rootDirs , getCanonicalFileName ) ;
772
772
if ( normalizedTargetPaths === undefined ) {
773
773
return undefined ;
@@ -781,7 +781,7 @@ function tryGetModuleNameFromRootDirs(rootDirs: readonly string[], moduleFileNam
781
781
if ( ! shortest ) {
782
782
return undefined ;
783
783
}
784
- return processEnding ( shortest , ending , compilerOptions ) ;
784
+ return processEnding ( shortest , allowedEndings , compilerOptions ) ;
785
785
}
786
786
787
787
function tryGetModuleNameAsNodeModule ( { path, isRedirect } : ModulePath , { getCanonicalFileName, sourceDirectory } : Info , importingSourceFile : SourceFile , host : ModuleSpecifierResolutionHost , options : CompilerOptions , userPreferences : UserPreferences , packageNameOnly ?: boolean , overrideMode ?: ResolutionMode ) : string | undefined {
@@ -823,7 +823,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
823
823
// try with next level of directory
824
824
packageRootIndex = path . indexOf ( directorySeparator , packageRootIndex + 1 ) ;
825
825
if ( packageRootIndex === - 1 ) {
826
- moduleSpecifier = processEnding ( moduleFileName , allowedEndings [ 0 ] , options , host ) ;
826
+ moduleSpecifier = processEnding ( moduleFileName , allowedEndings , options , host ) ;
827
827
break ;
828
828
}
829
829
}
@@ -936,7 +936,7 @@ function getPathsRelativeToRootDirs(path: string, rootDirs: readonly string[], g
936
936
} ) ;
937
937
}
938
938
939
- function processEnding ( fileName : string , ending : ModuleSpecifierEnding , options : CompilerOptions , host ?: ModuleSpecifierResolutionHost ) : string {
939
+ function processEnding ( fileName : string , allowedEndings : readonly ModuleSpecifierEnding [ ] , options : CompilerOptions , host ?: ModuleSpecifierResolutionHost ) : string {
940
940
if ( fileExtensionIsOneOf ( fileName , [ Extension . Json , Extension . Mjs , Extension . Cjs ] ) ) {
941
941
return fileName ;
942
942
}
@@ -950,7 +950,7 @@ function processEnding(fileName: string, ending: ModuleSpecifierEnding, options:
950
950
return noExtension + getJSExtensionForFile ( fileName , options ) ;
951
951
}
952
952
953
- switch ( ending ) {
953
+ switch ( allowedEndings [ 0 ] ) {
954
954
case ModuleSpecifierEnding . Minimal :
955
955
const withoutIndex = removeSuffix ( noExtension , "/index" ) ;
956
956
if ( host && withoutIndex !== noExtension && tryGetAnyFileFromPath ( host , withoutIndex ) ) {
@@ -965,10 +965,17 @@ function processEnding(fileName: string, ending: ModuleSpecifierEnding, options:
965
965
return noExtension + getJSExtensionForFile ( fileName , options ) ;
966
966
case ModuleSpecifierEnding . TsExtension :
967
967
// For now, we don't know if this import is going to be type-only, which means we don't
968
- // know if a .d.ts extension is valid, so use the .js extension.
969
- return isDeclarationFileName ( fileName ) ? noExtension + getJSExtensionForFile ( fileName , options ) : fileName ;
968
+ // know if a .d.ts extension is valid, so use no extension or a .js extension
969
+ if ( isDeclarationFileName ( fileName ) ) {
970
+ const extensionlessPriority = allowedEndings . findIndex ( e => e === ModuleSpecifierEnding . Minimal || e === ModuleSpecifierEnding . Index ) ;
971
+ const jsPriority = allowedEndings . indexOf ( ModuleSpecifierEnding . JsExtension ) ;
972
+ return extensionlessPriority !== - 1 && extensionlessPriority < jsPriority
973
+ ? noExtension
974
+ : noExtension + getJSExtensionForFile ( fileName , options ) ;
975
+ }
976
+ return fileName ;
970
977
default :
971
- return Debug . assertNever ( ending ) ;
978
+ return Debug . assertNever ( allowedEndings [ 0 ] ) ;
972
979
}
973
980
}
974
981
0 commit comments