@@ -41,11 +41,9 @@ namespace ts.moduleSpecifiers {
4141 importingSourceFileName : Path ,
4242 toFileName : string ,
4343 host : ModuleSpecifierResolutionHost ,
44- files : readonly SourceFile [ ] ,
45- redirectTargetsMap : RedirectTargetsMap ,
4644 oldImportSpecifier : string ,
4745 ) : string | undefined {
48- const res = getModuleSpecifierWorker ( compilerOptions , importingSourceFileName , toFileName , host , files , redirectTargetsMap , getPreferencesForUpdate ( compilerOptions , oldImportSpecifier ) ) ;
46+ const res = getModuleSpecifierWorker ( compilerOptions , importingSourceFileName , toFileName , host , getPreferencesForUpdate ( compilerOptions , oldImportSpecifier ) ) ;
4947 if ( res === oldImportSpecifier ) return undefined ;
5048 return res ;
5149 }
@@ -57,23 +55,19 @@ namespace ts.moduleSpecifiers {
5755 importingSourceFileName : Path ,
5856 toFileName : string ,
5957 host : ModuleSpecifierResolutionHost ,
60- files : readonly SourceFile [ ] ,
6158 preferences : UserPreferences = { } ,
62- redirectTargetsMap : RedirectTargetsMap ,
6359 ) : string {
64- return getModuleSpecifierWorker ( compilerOptions , importingSourceFileName , toFileName , host , files , redirectTargetsMap , getPreferences ( preferences , compilerOptions , importingSourceFile ) ) ;
60+ return getModuleSpecifierWorker ( compilerOptions , importingSourceFileName , toFileName , host , getPreferences ( preferences , compilerOptions , importingSourceFile ) ) ;
6561 }
6662
6763 export function getNodeModulesPackageName (
6864 compilerOptions : CompilerOptions ,
6965 importingSourceFileName : Path ,
7066 nodeModulesFileName : string ,
7167 host : ModuleSpecifierResolutionHost ,
72- files : readonly SourceFile [ ] ,
73- redirectTargetsMap : RedirectTargetsMap ,
7468 ) : string | undefined {
7569 const info = getInfo ( importingSourceFileName , host ) ;
76- const modulePaths = getAllModulePaths ( files , importingSourceFileName , nodeModulesFileName , info . getCanonicalFileName , host , redirectTargetsMap ) ;
70+ const modulePaths = getAllModulePaths ( importingSourceFileName , nodeModulesFileName , host ) ;
7771 return firstDefined ( modulePaths ,
7872 moduleFileName => tryGetModuleNameAsNodeModule ( moduleFileName , info , host , compilerOptions , /*packageNameOnly*/ true ) ) ;
7973 }
@@ -83,12 +77,10 @@ namespace ts.moduleSpecifiers {
8377 importingSourceFileName : Path ,
8478 toFileName : string ,
8579 host : ModuleSpecifierResolutionHost ,
86- files : readonly SourceFile [ ] ,
87- redirectTargetsMap : RedirectTargetsMap ,
8880 preferences : Preferences
8981 ) : string {
9082 const info = getInfo ( importingSourceFileName , host ) ;
91- const modulePaths = getAllModulePaths ( files , importingSourceFileName , toFileName , info . getCanonicalFileName , host , redirectTargetsMap ) ;
83+ const modulePaths = getAllModulePaths ( importingSourceFileName , toFileName , host ) ;
9284 return firstDefined ( modulePaths , moduleFileName => tryGetModuleNameAsNodeModule ( moduleFileName , info , host , compilerOptions ) ) ||
9385 getLocalModuleSpecifier ( toFileName , info , compilerOptions , preferences ) ;
9486 }
@@ -99,16 +91,14 @@ namespace ts.moduleSpecifiers {
9991 compilerOptions : CompilerOptions ,
10092 importingSourceFile : SourceFile ,
10193 host : ModuleSpecifierResolutionHost ,
102- files : readonly SourceFile [ ] ,
10394 userPreferences : UserPreferences ,
104- redirectTargetsMap : RedirectTargetsMap ,
10595 ) : readonly string [ ] {
10696 const ambient = tryGetModuleNameFromAmbientModule ( moduleSymbol ) ;
10797 if ( ambient ) return [ ambient ] ;
10898
10999 const info = getInfo ( importingSourceFile . path , host ) ;
110100 const moduleSourceFile = getSourceFileOfNode ( moduleSymbol . valueDeclaration || getNonAugmentationDeclaration ( moduleSymbol ) ) ;
111- const modulePaths = getAllModulePaths ( files , importingSourceFile . path , moduleSourceFile . originalFileName , info . getCanonicalFileName , host , redirectTargetsMap ) ;
101+ const modulePaths = getAllModulePaths ( importingSourceFile . path , moduleSourceFile . originalFileName , host ) ;
112102
113103 const preferences = getPreferences ( userPreferences , compilerOptions , importingSourceFile ) ;
114104 const global = mapDefined ( modulePaths , moduleFileName => tryGetModuleNameAsNodeModule ( moduleFileName , info , host , compilerOptions ) ) ;
@@ -179,26 +169,24 @@ namespace ts.moduleSpecifiers {
179169 }
180170
181171 export function forEachFileNameOfModule < T > (
182- files : readonly SourceFile [ ] ,
183172 importingFileName : string ,
184173 importedFileName : string ,
185- getCanonicalFileName : GetCanonicalFileName ,
186174 host : ModuleSpecifierResolutionHost ,
187- redirectTargetsMap : RedirectTargetsMap ,
188175 preferSymlinks : boolean ,
189176 cb : ( fileName : string ) => T | undefined
190177 ) : T | undefined {
191- const redirects = redirectTargetsMap . get ( importedFileName ) ;
192- const importedFileNames = redirects ? [ ...redirects , importedFileName ] : [ importedFileName ] ;
193- const cwd = host . getCurrentDirectory ? host . getCurrentDirectory ( ) : "" ;
178+ const getCanonicalFileName = hostGetCanonicalFileName ( host ) ;
179+ const cwd = host . getCurrentDirectory ( ) ;
180+ const redirects = host . redirectTargetsMap . get ( toPath ( importedFileName , cwd , getCanonicalFileName ) ) || emptyArray ;
181+ const importedFileNames = [ importedFileName , ...redirects ] ;
194182 const targets = importedFileNames . map ( f => getNormalizedAbsolutePath ( f , cwd ) ) ;
195183 if ( ! preferSymlinks ) {
196184 const result = forEach ( targets , cb ) ;
197185 if ( result ) return result ;
198186 }
199187 const links = host . getProbableSymlinks
200- ? host . getProbableSymlinks ( files )
201- : discoverProbableSymlinks ( files , getCanonicalFileName , cwd ) ;
188+ ? host . getProbableSymlinks ( host . getSourceFiles ( ) )
189+ : discoverProbableSymlinks ( host . getSourceFiles ( ) , getCanonicalFileName , cwd ) ;
202190
203191 const compareStrings = ( ! host . useCaseSensitiveFileNames || host . useCaseSensitiveFileNames ( ) ) ? compareStringsCaseSensitive : compareStringsCaseInsensitive ;
204192 const result = forEachEntry ( links , ( resolved , path ) => {
@@ -224,20 +212,20 @@ namespace ts.moduleSpecifiers {
224212 * Looks for existing imports that use symlinks to this module.
225213 * Symlinks will be returned first so they are preferred over the real path.
226214 */
227- function getAllModulePaths ( files : readonly SourceFile [ ] , importingFileName : string , importedFileName : string , getCanonicalFileName : GetCanonicalFileName , host : ModuleSpecifierResolutionHost , redirectTargetsMap : RedirectTargetsMap ) : readonly string [ ] {
228- const cwd = host . getCurrentDirectory ? host . getCurrentDirectory ( ) : "" ;
215+ function getAllModulePaths ( importingFileName : string , importedFileName : string , host : ModuleSpecifierResolutionHost ) : readonly string [ ] {
216+ const cwd = host . getCurrentDirectory ( ) ;
217+ const getCanonicalFileName = hostGetCanonicalFileName ( host ) ;
229218 const allFileNames = createMap < string > ( ) ;
219+ let importedFileFromNodeModules = false ;
230220 forEachFileNameOfModule (
231- files ,
232221 importingFileName ,
233222 importedFileName ,
234- getCanonicalFileName ,
235223 host ,
236- redirectTargetsMap ,
237224 /*preferSymlinks*/ true ,
238225 path => {
239226 // dont return value, so we collect everything
240227 allFileNames . set ( path , getCanonicalFileName ( path ) ) ;
228+ importedFileFromNodeModules = importedFileFromNodeModules || pathContainsNodeModules ( path ) ;
241229 }
242230 ) ;
243231
@@ -251,7 +239,10 @@ namespace ts.moduleSpecifiers {
251239 let pathsInDirectory : string [ ] | undefined ;
252240 allFileNames . forEach ( ( canonicalFileName , fileName ) => {
253241 if ( startsWith ( canonicalFileName , directoryStart ) ) {
254- ( pathsInDirectory || ( pathsInDirectory = [ ] ) ) . push ( fileName ) ;
242+ // If the importedFile is from node modules, use only paths in node_modules folder as option
243+ if ( ! importedFileFromNodeModules || pathContainsNodeModules ( fileName ) ) {
244+ ( pathsInDirectory || ( pathsInDirectory = [ ] ) ) . push ( fileName ) ;
245+ }
255246 allFileNames . delete ( fileName ) ;
256247 }
257248 } ) ;
0 commit comments