@@ -37,9 +37,9 @@ import {
37
37
getBaseFileName ,
38
38
GetCanonicalFileName ,
39
39
getConditions ,
40
- getDefaultResolutionModeForFileWorker ,
41
40
getDirectoryPath ,
42
41
getEmitModuleResolutionKind ,
42
+ getModeForResolutionAtIndex ,
43
43
getModuleNameStringLiteralAt ,
44
44
getModuleSpecifierEndingPreference ,
45
45
getNodeModulePathParts ,
@@ -143,13 +143,12 @@ export interface ModuleSpecifierPreferences {
143
143
/**
144
144
* @param syntaxImpliedNodeFormat Used when the import syntax implies ESM or CJS irrespective of the mode of the file.
145
145
*/
146
- getAllowedEndingsInPreferredOrder ( syntaxImpliedNodeFormat ?: ResolutionMode ) : ModuleSpecifierEnding [ ] ;
146
+ getAllowedEndingsInPreferredOrder ( syntaxImpliedNodeFormat ?: SourceFile [ "impliedNodeFormat" ] ) : ModuleSpecifierEnding [ ] ;
147
147
}
148
148
149
149
/** @internal */
150
150
export function getModuleSpecifierPreferences (
151
151
{ importModuleSpecifierPreference, importModuleSpecifierEnding } : UserPreferences ,
152
- host : Pick < ModuleSpecifierResolutionHost , "getDefaultResolutionModeForFile" > ,
153
152
compilerOptions : CompilerOptions ,
154
153
importingSourceFile : Pick < SourceFile , "fileName" | "impliedNodeFormat" > ,
155
154
oldImportSpecifier ?: string ,
@@ -164,10 +163,8 @@ export function getModuleSpecifierPreferences(
164
163
importModuleSpecifierPreference === "project-relative" ? RelativePreference . ExternalNonRelative :
165
164
RelativePreference . Shortest ,
166
165
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 ) {
171
168
if ( shouldAllowImportingTsExtension ( compilerOptions , importingSourceFile . fileName ) ) {
172
169
return [ ModuleSpecifierEnding . TsExtension , ModuleSpecifierEnding . JsExtension ] ;
173
170
}
@@ -207,7 +204,7 @@ export function getModuleSpecifierPreferences(
207
204
}
208
205
return getModuleSpecifierEndingPreference (
209
206
importModuleSpecifierEnding ,
210
- resolutionMode ?? getDefaultResolutionModeForFile ( importingSourceFile , host , compilerOptions ) ,
207
+ resolutionMode ?? importingSourceFile . impliedNodeFormat ,
211
208
compilerOptions ,
212
209
isFullSourceFile ( importingSourceFile ) ? importingSourceFile : undefined ,
213
210
) ;
@@ -228,7 +225,7 @@ export function updateModuleSpecifier(
228
225
oldImportSpecifier : string ,
229
226
options : ModuleSpecifierOptions = { } ,
230
227
) : 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 ) ;
232
229
if ( res === oldImportSpecifier ) return undefined ;
233
230
return res ;
234
231
}
@@ -248,7 +245,7 @@ export function getModuleSpecifier(
248
245
host : ModuleSpecifierResolutionHost ,
249
246
options : ModuleSpecifierOptions = { } ,
250
247
) : 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 ) ;
252
249
}
253
250
254
251
/** @internal */
@@ -278,7 +275,7 @@ function getModuleSpecifierWorker(
278
275
const info = getInfo ( importingSourceFileName , host ) ;
279
276
const modulePaths = getAllModulePaths ( info , toFileName , host , userPreferences , compilerOptions , options ) ;
280
277
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 ) ;
282
279
}
283
280
284
281
/** @internal */
@@ -406,7 +403,7 @@ export function getLocalModuleSpecifierBetweenFileNames(
406
403
compilerOptions ,
407
404
host ,
408
405
importMode ,
409
- getModuleSpecifierPreferences ( { } , host , compilerOptions , importingFile ) ,
406
+ getModuleSpecifierPreferences ( { } , compilerOptions , importingFile ) ,
410
407
) ;
411
408
}
412
409
@@ -420,19 +417,15 @@ function computeModuleSpecifiers(
420
417
forAutoImport : boolean ,
421
418
) : ModuleSpecifierResult {
422
419
const info = getInfo ( importingSourceFile . fileName , host ) ;
423
- const preferences = getModuleSpecifierPreferences ( userPreferences , host , compilerOptions , importingSourceFile ) ;
420
+ const preferences = getModuleSpecifierPreferences ( userPreferences , compilerOptions , importingSourceFile ) ;
424
421
const existingSpecifier = isFullSourceFile ( importingSourceFile ) && forEach ( modulePaths , modulePath =>
425
422
forEach (
426
423
host . getFileIncludeReasons ( ) . get ( toPath ( modulePath . path , host . getCurrentDirectory ( ) , info . getCanonicalFileName ) ) ,
427
424
reason => {
428
425
if ( reason . kind !== FileIncludeKind . Import || reason . file !== importingSourceFile . path ) return undefined ;
429
426
// If the candidate import mode doesn't match the mode we're generating for, don't consider it
430
427
// 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 ;
436
429
const specifier = getModuleNameStringLiteralAt ( importingSourceFile , reason . index ) . text ;
437
430
// If the preference is for non relative and the module specifier is relative, ignore it
438
431
return preferences . relativePreference !== RelativePreference . NonRelative || ! pathIsRelative ( specifier ) ?
@@ -1100,7 +1093,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
1100
1093
1101
1094
// Simplify the full file path to something that can be resolved by Node.
1102
1095
1103
- const preferences = getModuleSpecifierPreferences ( userPreferences , host , options , importingSourceFile ) ;
1096
+ const preferences = getModuleSpecifierPreferences ( userPreferences , options , importingSourceFile ) ;
1104
1097
const allowedEndings = preferences . getAllowedEndingsInPreferredOrder ( ) ;
1105
1098
let moduleSpecifier = path ;
1106
1099
let isPackageRootPath = false ;
@@ -1160,7 +1153,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
1160
1153
const cachedPackageJson = host . getPackageJsonInfoCache ?.( ) ?. getPackageJsonInfo ( packageJsonPath ) ;
1161
1154
if ( isPackageJsonInfo ( cachedPackageJson ) || cachedPackageJson === undefined && host . fileExists ( packageJsonPath ) ) {
1162
1155
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 ;
1164
1157
if ( getResolvePackageJsonExports ( options ) ) {
1165
1158
// The package name that we found in node_modules could be different from the package
1166
1159
// 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
1355
1348
function isPathRelativeToParent ( path : string ) : boolean {
1356
1349
return startsWith ( path , ".." ) ;
1357
1350
}
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