@@ -102,7 +102,7 @@ namespace ts.codefix {
102
102
const exportInfos = getAllReExportingModules ( exportedSymbol , checker , allSourceFiles ) ;
103
103
Debug . assert ( exportInfos . some ( info => info . moduleSymbol === moduleSymbol ) ) ;
104
104
// We sort the best codefixes first, so taking `first` is best for completions.
105
- const moduleSpecifier = first ( getNewImportInfos ( program , sourceFile , exportInfos , compilerOptions , getCanonicalFileName , host ) ) . moduleSpecifier ;
105
+ const moduleSpecifier = first ( getNewImportInfos ( program , sourceFile , exportInfos , compilerOptions , getCanonicalFileName , host , options ) ) . moduleSpecifier ;
106
106
const ctx : ImportCodeFixContext = { host, program, checker, compilerOptions, sourceFile, formatContext, symbolName, getCanonicalFileName, symbolToken, options } ;
107
107
return { moduleSpecifier, codeAction : first ( getCodeActionsForImport ( exportInfos , ctx ) ) } ;
108
108
}
@@ -254,25 +254,26 @@ namespace ts.codefix {
254
254
program : Program ,
255
255
sourceFile : SourceFile ,
256
256
moduleSymbols : ReadonlyArray < SymbolExportInfo > ,
257
- options : CompilerOptions ,
257
+ compilerOptions : CompilerOptions ,
258
258
getCanonicalFileName : ( file : string ) => string ,
259
259
host : LanguageServiceHost ,
260
+ options : Options ,
260
261
) : ReadonlyArray < NewImportInfo > {
261
- const { baseUrl, paths, rootDirs } = options ;
262
+ const { baseUrl, paths, rootDirs } = compilerOptions ;
262
263
const addJsExtension = usesJsExtensionOnImports ( sourceFile ) ;
263
264
const choicesForEachExportingModule = flatMap < SymbolExportInfo , NewImportInfo [ ] > ( moduleSymbols , ( { moduleSymbol, importKind } ) => {
264
265
const modulePathsGroups = getAllModulePaths ( program , moduleSymbol . valueDeclaration . getSourceFile ( ) ) . map ( moduleFileName => {
265
266
const sourceDirectory = getDirectoryPath ( sourceFile . fileName ) ;
266
267
const global = tryGetModuleNameFromAmbientModule ( moduleSymbol )
267
- || tryGetModuleNameFromTypeRoots ( options , host , getCanonicalFileName , moduleFileName , addJsExtension )
268
- || tryGetModuleNameAsNodeModule ( options , moduleFileName , host , getCanonicalFileName , sourceDirectory )
268
+ || tryGetModuleNameFromTypeRoots ( compilerOptions , host , getCanonicalFileName , moduleFileName , addJsExtension )
269
+ || tryGetModuleNameAsNodeModule ( compilerOptions , moduleFileName , host , getCanonicalFileName , sourceDirectory )
269
270
|| rootDirs && tryGetModuleNameFromRootDirs ( rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ;
270
271
if ( global ) {
271
272
return [ global ] ;
272
273
}
273
274
274
- const relativePath = removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , options , addJsExtension ) ;
275
- if ( ! baseUrl ) {
275
+ const relativePath = removeExtensionAndIndexPostFix ( getRelativePath ( moduleFileName , sourceDirectory , getCanonicalFileName ) , compilerOptions , addJsExtension ) ;
276
+ if ( ! baseUrl || options . importModuleSpecifierPreference == "relative" ) {
276
277
return [ relativePath ] ;
277
278
}
278
279
@@ -281,14 +282,20 @@ namespace ts.codefix {
281
282
return [ relativePath ] ;
282
283
}
283
284
284
- const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , options , addJsExtension ) ;
285
+ const importRelativeToBaseUrl = removeExtensionAndIndexPostFix ( relativeToBaseUrl , compilerOptions , addJsExtension ) ;
285
286
if ( paths ) {
286
287
const fromPaths = tryGetModuleNameFromPaths ( removeFileExtension ( relativeToBaseUrl ) , importRelativeToBaseUrl , paths ) ;
287
288
if ( fromPaths ) {
288
289
return [ fromPaths ] ;
289
290
}
290
291
}
291
292
293
+ if ( options . importModuleSpecifierPreference === "baseUrl" ) {
294
+ return [ importRelativeToBaseUrl ] ;
295
+ }
296
+
297
+ if ( options . importModuleSpecifierPreference !== undefined ) Debug . assertNever ( options . importModuleSpecifierPreference ) ;
298
+
292
299
if ( isPathRelativeToParent ( relativeToBaseUrl ) ) {
293
300
return [ relativePath ] ;
294
301
}
@@ -584,7 +591,7 @@ namespace ts.codefix {
584
591
const existingDeclaration = firstDefined ( existingImports , newImportInfoFromExistingSpecifier ) ;
585
592
const newImportInfos = existingDeclaration
586
593
? [ existingDeclaration ]
587
- : getNewImportInfos ( ctx . program , ctx . sourceFile , exportInfos , ctx . compilerOptions , ctx . getCanonicalFileName , ctx . host ) ;
594
+ : getNewImportInfos ( ctx . program , ctx . sourceFile , exportInfos , ctx . compilerOptions , ctx . getCanonicalFileName , ctx . host , ctx . options ) ;
588
595
return newImportInfos . map ( info => getCodeActionForNewImport ( ctx , info ) ) ;
589
596
}
590
597
0 commit comments