@@ -37,9 +37,9 @@ import {
3737 getBaseFileName ,
3838 GetCanonicalFileName ,
3939 getConditions ,
40- getDefaultResolutionModeForFileWorker ,
4140 getDirectoryPath ,
4241 getEmitModuleResolutionKind ,
42+ getModeForResolutionAtIndex ,
4343 getModuleNameStringLiteralAt ,
4444 getModuleSpecifierEndingPreference ,
4545 getNodeModulePathParts ,
@@ -143,13 +143,12 @@ export interface ModuleSpecifierPreferences {
143143 /**
144144 * @param syntaxImpliedNodeFormat Used when the import syntax implies ESM or CJS irrespective of the mode of the file.
145145 */
146- getAllowedEndingsInPreferredOrder ( syntaxImpliedNodeFormat ?: ResolutionMode ) : ModuleSpecifierEnding [ ] ;
146+ getAllowedEndingsInPreferredOrder ( syntaxImpliedNodeFormat ?: SourceFile [ "impliedNodeFormat" ] ) : ModuleSpecifierEnding [ ] ;
147147}
148148
149149/** @internal */
150150export function getModuleSpecifierPreferences (
151151 { importModuleSpecifierPreference, importModuleSpecifierEnding } : UserPreferences ,
152- host : Pick < ModuleSpecifierResolutionHost , "getDefaultResolutionModeForFile" > ,
153152 compilerOptions : CompilerOptions ,
154153 importingSourceFile : Pick < SourceFile , "fileName" | "impliedNodeFormat" > ,
155154 oldImportSpecifier ?: string ,
@@ -164,10 +163,8 @@ export function getModuleSpecifierPreferences(
164163 importModuleSpecifierPreference === "project-relative" ? RelativePreference . ExternalNonRelative :
165164 RelativePreference . Shortest ,
166165 getAllowedEndingsInPreferredOrder : syntaxImpliedNodeFormat => {
167- const impliedNodeFormat = getDefaultResolutionModeForFile ( importingSourceFile , host , compilerOptions ) ;
168- const preferredEnding = syntaxImpliedNodeFormat !== impliedNodeFormat ? getPreferredEnding ( syntaxImpliedNodeFormat ) : filePreferredEnding ;
169- const moduleResolution = getEmitModuleResolutionKind ( compilerOptions ) ;
170- if ( ( syntaxImpliedNodeFormat ?? impliedNodeFormat ) === ModuleKind . ESNext && ModuleResolutionKind . Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind . NodeNext ) {
166+ const preferredEnding = syntaxImpliedNodeFormat !== importingSourceFile . impliedNodeFormat ? getPreferredEnding ( syntaxImpliedNodeFormat ) : filePreferredEnding ;
167+ if ( ( syntaxImpliedNodeFormat ?? importingSourceFile . impliedNodeFormat ) === ModuleKind . ESNext ) {
171168 if ( shouldAllowImportingTsExtension ( compilerOptions , importingSourceFile . fileName ) ) {
172169 return [ ModuleSpecifierEnding . TsExtension , ModuleSpecifierEnding . JsExtension ] ;
173170 }
@@ -207,7 +204,7 @@ export function getModuleSpecifierPreferences(
207204 }
208205 return getModuleSpecifierEndingPreference (
209206 importModuleSpecifierEnding ,
210- resolutionMode ?? getDefaultResolutionModeForFile ( importingSourceFile , host , compilerOptions ) ,
207+ resolutionMode ?? importingSourceFile . impliedNodeFormat ,
211208 compilerOptions ,
212209 isFullSourceFile ( importingSourceFile ) ? importingSourceFile : undefined ,
213210 ) ;
@@ -228,7 +225,7 @@ export function updateModuleSpecifier(
228225 oldImportSpecifier : string ,
229226 options : ModuleSpecifierOptions = { } ,
230227) : string | undefined {
231- const res = getModuleSpecifierWorker ( compilerOptions , importingSourceFile , importingSourceFileName , toFileName , host , getModuleSpecifierPreferences ( { } , host , compilerOptions , importingSourceFile , oldImportSpecifier ) , { } , options ) ;
228+ const res = getModuleSpecifierWorker ( compilerOptions , importingSourceFile , importingSourceFileName , toFileName , host , getModuleSpecifierPreferences ( { } , compilerOptions , importingSourceFile , oldImportSpecifier ) , { } , options ) ;
232229 if ( res === oldImportSpecifier ) return undefined ;
233230 return res ;
234231}
@@ -248,7 +245,7 @@ export function getModuleSpecifier(
248245 host : ModuleSpecifierResolutionHost ,
249246 options : ModuleSpecifierOptions = { } ,
250247) : string {
251- return getModuleSpecifierWorker ( compilerOptions , importingSourceFile , importingSourceFileName , toFileName , host , getModuleSpecifierPreferences ( { } , host , compilerOptions , importingSourceFile ) , { } , options ) ;
248+ return getModuleSpecifierWorker ( compilerOptions , importingSourceFile , importingSourceFileName , toFileName , host , getModuleSpecifierPreferences ( { } , compilerOptions , importingSourceFile ) , { } , options ) ;
252249}
253250
254251/** @internal */
@@ -278,7 +275,7 @@ function getModuleSpecifierWorker(
278275 const info = getInfo ( importingSourceFileName , host ) ;
279276 const modulePaths = getAllModulePaths ( info , toFileName , host , userPreferences , compilerOptions , options ) ;
280277 return firstDefined ( modulePaths , modulePath => tryGetModuleNameAsNodeModule ( modulePath , info , importingSourceFile , host , compilerOptions , userPreferences , /*packageNameOnly*/ undefined , options . overrideImportMode ) ) ||
281- getLocalModuleSpecifier ( toFileName , info , compilerOptions , host , options . overrideImportMode || getDefaultResolutionModeForFile ( importingSourceFile , host , compilerOptions ) , preferences ) ;
278+ getLocalModuleSpecifier ( toFileName , info , compilerOptions , host , options . overrideImportMode || importingSourceFile . impliedNodeFormat , preferences ) ;
282279}
283280
284281/** @internal */
@@ -406,7 +403,7 @@ export function getLocalModuleSpecifierBetweenFileNames(
406403 compilerOptions ,
407404 host ,
408405 importMode ,
409- getModuleSpecifierPreferences ( { } , host , compilerOptions , importingFile ) ,
406+ getModuleSpecifierPreferences ( { } , compilerOptions , importingFile ) ,
410407 ) ;
411408}
412409
@@ -420,19 +417,15 @@ function computeModuleSpecifiers(
420417 forAutoImport : boolean ,
421418) : ModuleSpecifierResult {
422419 const info = getInfo ( importingSourceFile . fileName , host ) ;
423- const preferences = getModuleSpecifierPreferences ( userPreferences , host , compilerOptions , importingSourceFile ) ;
420+ const preferences = getModuleSpecifierPreferences ( userPreferences , compilerOptions , importingSourceFile ) ;
424421 const existingSpecifier = isFullSourceFile ( importingSourceFile ) && forEach ( modulePaths , modulePath =>
425422 forEach (
426423 host . getFileIncludeReasons ( ) . get ( toPath ( modulePath . path , host . getCurrentDirectory ( ) , info . getCanonicalFileName ) ) ,
427424 reason => {
428425 if ( reason . kind !== FileIncludeKind . Import || reason . file !== importingSourceFile . path ) return undefined ;
429426 // If the candidate import mode doesn't match the mode we're generating for, don't consider it
430427 // TODO: maybe useful to keep around as an alternative option for certain contexts where the mode is overridable
431- const existingMode = host . getModeForResolutionAtIndex ( importingSourceFile , reason . index ) ;
432- const targetMode = options . overrideImportMode ?? host . getDefaultResolutionModeForFile ( importingSourceFile ) ;
433- if ( existingMode !== targetMode && existingMode !== undefined && targetMode !== undefined ) {
434- return undefined ;
435- }
428+ if ( importingSourceFile . impliedNodeFormat && importingSourceFile . impliedNodeFormat !== getModeForResolutionAtIndex ( importingSourceFile , reason . index , compilerOptions ) ) return undefined ;
436429 const specifier = getModuleNameStringLiteralAt ( importingSourceFile , reason . index ) . text ;
437430 // If the preference is for non relative and the module specifier is relative, ignore it
438431 return preferences . relativePreference !== RelativePreference . NonRelative || ! pathIsRelative ( specifier ) ?
@@ -1100,7 +1093,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
11001093
11011094 // Simplify the full file path to something that can be resolved by Node.
11021095
1103- const preferences = getModuleSpecifierPreferences ( userPreferences , host , options , importingSourceFile ) ;
1096+ const preferences = getModuleSpecifierPreferences ( userPreferences , options , importingSourceFile ) ;
11041097 const allowedEndings = preferences . getAllowedEndingsInPreferredOrder ( ) ;
11051098 let moduleSpecifier = path ;
11061099 let isPackageRootPath = false ;
@@ -1160,7 +1153,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
11601153 const cachedPackageJson = host . getPackageJsonInfoCache ?.( ) ?. getPackageJsonInfo ( packageJsonPath ) ;
11611154 if ( isPackageJsonInfo ( cachedPackageJson ) || cachedPackageJson === undefined && host . fileExists ( packageJsonPath ) ) {
11621155 const packageJsonContent : Record < string , any > | undefined = cachedPackageJson ?. contents . packageJsonContent || tryParseJson ( host . readFile ! ( packageJsonPath ) ! ) ;
1163- const importMode = overrideMode || getDefaultResolutionModeForFile ( importingSourceFile , host , options ) ;
1156+ const importMode = overrideMode || importingSourceFile . impliedNodeFormat ;
11641157 if ( getResolvePackageJsonExports ( options ) ) {
11651158 // The package name that we found in node_modules could be different from the package
11661159 // name in the package.json content via url/filepath dependency specifiers. We need to
@@ -1355,7 +1348,3 @@ function getRelativePathIfInSameVolume(path: string, directoryPath: string, getC
13551348function isPathRelativeToParent ( path : string ) : boolean {
13561349 return startsWith ( path , ".." ) ;
13571350}
1358-
1359- function getDefaultResolutionModeForFile ( file : Pick < SourceFile , "fileName" | "impliedNodeFormat" | "packageJsonScope" > , host : Pick < ModuleSpecifierResolutionHost , "getDefaultResolutionModeForFile" > , compilerOptions : CompilerOptions ) {
1360- return isFullSourceFile ( file ) ? host . getDefaultResolutionModeForFile ( file ) : getDefaultResolutionModeForFileWorker ( file , compilerOptions ) ;
1361- }
0 commit comments