@@ -9,7 +9,7 @@ namespace ts.moduleSpecifiers {
99 export function getModuleSpecifier (
1010 compilerOptions : CompilerOptions ,
1111 importingSourceFile : SourceFile ,
12- importingSourceFileName : string ,
12+ importingSourceFileName : Path ,
1313 toFileName : string ,
1414 host : ModuleSpecifierResolutionHost ,
1515 files : ReadonlyArray < SourceFile > ,
@@ -18,7 +18,6 @@ namespace ts.moduleSpecifiers {
1818 const info = getInfo ( compilerOptions , importingSourceFile , importingSourceFileName , host ) ;
1919 const modulePaths = getAllModulePaths ( files , toFileName , info . getCanonicalFileName , host ) ;
2020 return firstDefined ( modulePaths , moduleFileName => getGlobalModuleSpecifier ( moduleFileName , info , host , compilerOptions ) ) ||
21- getGlobalModuleSpecifier ( toFileName , info , host , compilerOptions ) ||
2221 first ( getLocalModuleSpecifiers ( toFileName , info , compilerOptions , preferences ) ) ;
2322 }
2423
@@ -49,10 +48,10 @@ namespace ts.moduleSpecifiers {
4948 readonly moduleResolutionKind : ModuleResolutionKind ;
5049 readonly addJsExtension : boolean ;
5150 readonly getCanonicalFileName : GetCanonicalFileName ;
52- readonly sourceDirectory : string ;
51+ readonly sourceDirectory : Path ;
5352 }
5453 // importingSourceFileName is separate because getEditsForFileRename may need to specify an updated path
55- function getInfo ( compilerOptions : CompilerOptions , importingSourceFile : SourceFile , importingSourceFileName : string , host : ModuleSpecifierResolutionHost ) : Info {
54+ function getInfo ( compilerOptions : CompilerOptions , importingSourceFile : SourceFile , importingSourceFileName : Path , host : ModuleSpecifierResolutionHost ) : Info {
5655 const moduleResolutionKind = getEmitModuleResolutionKind ( compilerOptions ) ;
5756 const addJsExtension = usesJsExtensionOnImports ( importingSourceFile ) ;
5857 const getCanonicalFileName = createGetCanonicalFileName ( host . useCaseSensitiveFileNames ? host . useCaseSensitiveFileNames ( ) : true ) ;
@@ -67,19 +66,19 @@ namespace ts.moduleSpecifiers {
6766 compilerOptions : CompilerOptions ,
6867 ) {
6968 return tryGetModuleNameFromTypeRoots ( compilerOptions , host , getCanonicalFileName , moduleFileName , addJsExtension )
70- || tryGetModuleNameAsNodeModule ( compilerOptions , moduleFileName , host , getCanonicalFileName , sourceDirectory )
71- || compilerOptions . rootDirs && tryGetModuleNameFromRootDirs ( compilerOptions . rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ;
69+ || tryGetModuleNameAsNodeModule ( compilerOptions , moduleFileName , host , getCanonicalFileName , sourceDirectory ) ;
7270 }
7371
7472 function getLocalModuleSpecifiers (
7573 moduleFileName : string ,
7674 { moduleResolutionKind, addJsExtension, getCanonicalFileName, sourceDirectory } : Info ,
7775 compilerOptions : CompilerOptions ,
7876 preferences : ModuleSpecifierPreferences ,
79- ) {
80- const { baseUrl, paths } = compilerOptions ;
77+ ) : ReadonlyArray < string > {
78+ const { baseUrl, paths, rootDirs } = compilerOptions ;
8179
82- const relativePath = removeExtensionAndIndexPostFix ( ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourceDirectory , moduleFileName , getCanonicalFileName ) ) , moduleResolutionKind , addJsExtension ) ;
80+ const relativePath = rootDirs && tryGetModuleNameFromRootDirs ( rootDirs , moduleFileName , sourceDirectory , getCanonicalFileName ) ||
81+ removeExtensionAndIndexPostFix ( ensurePathIsNonModuleName ( getRelativePathFromDirectory ( sourceDirectory , moduleFileName , getCanonicalFileName ) ) , moduleResolutionKind , addJsExtension ) ;
8382 if ( ! baseUrl || preferences . importModuleSpecifierPreference === "relative" ) {
8483 return [ relativePath ] ;
8584 }
@@ -272,7 +271,7 @@ namespace ts.moduleSpecifiers {
272271 moduleFileName : string ,
273272 host : ModuleSpecifierResolutionHost ,
274273 getCanonicalFileName : ( file : string ) => string ,
275- sourceDirectory : string ,
274+ sourceDirectory : Path ,
276275 ) : string | undefined {
277276 if ( getEmitModuleResolutionKind ( options ) !== ModuleResolutionKind . NodeJs ) {
278277 // nothing to do here
@@ -291,7 +290,7 @@ namespace ts.moduleSpecifiers {
291290 const moduleSpecifier = getDirectoryOrExtensionlessFileName ( moduleFileName ) ;
292291 // Get a path that's relative to node_modules or the importing file's path
293292 // if node_modules folder is in this folder or any of its parent folders, no need to keep it.
294- if ( ! startsWith ( sourceDirectory , moduleSpecifier . substring ( 0 , parts . topLevelNodeModulesIndex ) ) ) return undefined ;
293+ if ( ! startsWith ( sourceDirectory , getCanonicalFileName ( moduleSpecifier . substring ( 0 , parts . topLevelNodeModulesIndex ) ) ) ) return undefined ;
295294 // If the module was found in @types , get the actual Node package name
296295 return getPackageNameFromAtTypesDirectory ( moduleSpecifier . substring ( parts . topLevelPackageNameIndex + 1 ) ) ;
297296
0 commit comments