@@ -32,6 +32,7 @@ import {
32
32
flatten ,
33
33
forEach ,
34
34
forEachAncestorDirectory ,
35
+ FutureSourceFile ,
35
36
getBaseFileName ,
36
37
GetCanonicalFileName ,
37
38
getConditions ,
@@ -65,6 +66,7 @@ import {
65
66
isDeclarationFileName ,
66
67
isExternalModuleAugmentation ,
67
68
isExternalModuleNameRelative ,
69
+ isFullSourceFile ,
68
70
isMissingPackageJsonInfo ,
69
71
isModuleBlock ,
70
72
isModuleDeclaration ,
@@ -141,7 +143,7 @@ export interface ModuleSpecifierPreferences {
141
143
export function getModuleSpecifierPreferences (
142
144
{ importModuleSpecifierPreference, importModuleSpecifierEnding } : UserPreferences ,
143
145
compilerOptions : CompilerOptions ,
144
- importingSourceFile : SourceFile ,
146
+ importingSourceFile : Pick < SourceFile , "fileName" | "impliedNodeFormat" > ,
145
147
oldImportSpecifier ?: string ,
146
148
) : ModuleSpecifierPreferences {
147
149
const filePreferredEnding = getPreferredEnding ( ) ;
@@ -197,7 +199,7 @@ export function getModuleSpecifierPreferences(
197
199
importModuleSpecifierEnding ,
198
200
resolutionMode ?? importingSourceFile . impliedNodeFormat ,
199
201
compilerOptions ,
200
- importingSourceFile ,
202
+ isFullSourceFile ( importingSourceFile ) ? importingSourceFile : undefined ,
201
203
) ;
202
204
}
203
205
}
@@ -230,7 +232,7 @@ export function updateModuleSpecifier(
230
232
/** @internal */
231
233
export function getModuleSpecifier (
232
234
compilerOptions : CompilerOptions ,
233
- importingSourceFile : SourceFile ,
235
+ importingSourceFile : SourceFile | FutureSourceFile ,
234
236
importingSourceFileName : string ,
235
237
toFileName : string ,
236
238
host : ModuleSpecifierResolutionHost ,
@@ -242,7 +244,7 @@ export function getModuleSpecifier(
242
244
/** @internal */
243
245
export function getNodeModulesPackageName (
244
246
compilerOptions : CompilerOptions ,
245
- importingSourceFile : SourceFile ,
247
+ importingSourceFile : SourceFile | FutureSourceFile ,
246
248
nodeModulesFileName : string ,
247
249
host : ModuleSpecifierResolutionHost ,
248
250
preferences : UserPreferences ,
@@ -255,7 +257,7 @@ export function getNodeModulesPackageName(
255
257
256
258
function getModuleSpecifierWorker (
257
259
compilerOptions : CompilerOptions ,
258
- importingSourceFile : SourceFile ,
260
+ importingSourceFile : SourceFile | FutureSourceFile ,
259
261
importingSourceFileName : string ,
260
262
toFileName : string ,
261
263
host : ModuleSpecifierResolutionHost ,
@@ -272,7 +274,7 @@ function getModuleSpecifierWorker(
272
274
/** @internal */
273
275
export function tryGetModuleSpecifiersFromCache (
274
276
moduleSymbol : Symbol ,
275
- importingSourceFile : SourceFile ,
277
+ importingSourceFile : SourceFile | FutureSourceFile ,
276
278
host : ModuleSpecifierResolutionHost ,
277
279
userPreferences : UserPreferences ,
278
280
options : ModuleSpecifierOptions = { } ,
@@ -288,7 +290,7 @@ export function tryGetModuleSpecifiersFromCache(
288
290
289
291
function tryGetModuleSpecifiersFromCacheWorker (
290
292
moduleSymbol : Symbol ,
291
- importingSourceFile : SourceFile ,
293
+ importingSourceFile : SourceFile | FutureSourceFile ,
292
294
host : ModuleSpecifierResolutionHost ,
293
295
userPreferences : UserPreferences ,
294
296
options : ModuleSpecifierOptions = { } ,
@@ -334,7 +336,7 @@ export function getModuleSpecifiersWithCacheInfo(
334
336
moduleSymbol : Symbol ,
335
337
checker : TypeChecker ,
336
338
compilerOptions : CompilerOptions ,
337
- importingSourceFile : SourceFile ,
339
+ importingSourceFile : SourceFile | FutureSourceFile ,
338
340
host : ModuleSpecifierResolutionHost ,
339
341
userPreferences : UserPreferences ,
340
342
options : ModuleSpecifierOptions = { } ,
@@ -370,18 +372,38 @@ export function getModuleSpecifiersWithCacheInfo(
370
372
return { moduleSpecifiers : result , computedWithoutCache } ;
371
373
}
372
374
375
+ /** @internal */
376
+ export function getLocalModuleSpecifierBetweenFileNames (
377
+ importingFile : Pick < SourceFile , "fileName" | "impliedNodeFormat" > ,
378
+ targetFileName : string ,
379
+ compilerOptions : CompilerOptions ,
380
+ host : ModuleSpecifierResolutionHost ,
381
+ options : ModuleSpecifierOptions = { } ,
382
+ ) : string {
383
+ const info = getInfo ( importingFile . fileName , host ) ;
384
+ const importMode = options . overrideImportMode ?? importingFile . impliedNodeFormat ;
385
+ return getLocalModuleSpecifier (
386
+ targetFileName ,
387
+ info ,
388
+ compilerOptions ,
389
+ host ,
390
+ importMode ,
391
+ getModuleSpecifierPreferences ( { } , compilerOptions , importingFile ) ,
392
+ ) ;
393
+ }
394
+
373
395
function computeModuleSpecifiers (
374
396
modulePaths : readonly ModulePath [ ] ,
375
397
compilerOptions : CompilerOptions ,
376
- importingSourceFile : SourceFile ,
398
+ importingSourceFile : SourceFile | FutureSourceFile ,
377
399
host : ModuleSpecifierResolutionHost ,
378
400
userPreferences : UserPreferences ,
379
401
options : ModuleSpecifierOptions = { } ,
380
402
forAutoImport : boolean ,
381
403
) : readonly string [ ] {
382
404
const info = getInfo ( importingSourceFile . fileName , host ) ;
383
405
const preferences = getModuleSpecifierPreferences ( userPreferences , compilerOptions , importingSourceFile ) ;
384
- const existingSpecifier = forEach ( modulePaths , modulePath =>
406
+ const existingSpecifier = isFullSourceFile ( importingSourceFile ) && forEach ( modulePaths , modulePath =>
385
407
forEach (
386
408
host . getFileIncludeReasons ( ) . get ( toPath ( modulePath . path , host . getCurrentDirectory ( ) , info . getCanonicalFileName ) ) ,
387
409
reason => {
@@ -1014,7 +1036,7 @@ function tryGetModuleNameFromRootDirs(rootDirs: readonly string[], moduleFileNam
1014
1036
return processEnding ( shortest , allowedEndings , compilerOptions ) ;
1015
1037
}
1016
1038
1017
- function tryGetModuleNameAsNodeModule ( { path, isRedirect } : ModulePath , { getCanonicalFileName, canonicalSourceDirectory } : Info , importingSourceFile : SourceFile , host : ModuleSpecifierResolutionHost , options : CompilerOptions , userPreferences : UserPreferences , packageNameOnly ?: boolean , overrideMode ?: ResolutionMode ) : string | undefined {
1039
+ function tryGetModuleNameAsNodeModule ( { path, isRedirect } : ModulePath , { getCanonicalFileName, canonicalSourceDirectory } : Info , importingSourceFile : SourceFile | FutureSourceFile , host : ModuleSpecifierResolutionHost , options : CompilerOptions , userPreferences : UserPreferences , packageNameOnly ?: boolean , overrideMode ?: ResolutionMode ) : string | undefined {
1018
1040
if ( ! host . fileExists || ! host . readFile ) {
1019
1041
return undefined ;
1020
1042
}
0 commit comments