@@ -41,11 +41,9 @@ namespace ts.moduleSpecifiers {
41
41
importingSourceFileName : Path ,
42
42
toFileName : string ,
43
43
host : ModuleSpecifierResolutionHost ,
44
- files : readonly SourceFile [ ] ,
45
- redirectTargetsMap : RedirectTargetsMap ,
46
44
oldImportSpecifier : string ,
47
45
) : 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 ) ) ;
49
47
if ( res === oldImportSpecifier ) return undefined ;
50
48
return res ;
51
49
}
@@ -57,23 +55,19 @@ namespace ts.moduleSpecifiers {
57
55
importingSourceFileName : Path ,
58
56
toFileName : string ,
59
57
host : ModuleSpecifierResolutionHost ,
60
- files : readonly SourceFile [ ] ,
61
58
preferences : UserPreferences = { } ,
62
- redirectTargetsMap : RedirectTargetsMap ,
63
59
) : 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 ) ) ;
65
61
}
66
62
67
63
export function getNodeModulesPackageName (
68
64
compilerOptions : CompilerOptions ,
69
65
importingSourceFileName : Path ,
70
66
nodeModulesFileName : string ,
71
67
host : ModuleSpecifierResolutionHost ,
72
- files : readonly SourceFile [ ] ,
73
- redirectTargetsMap : RedirectTargetsMap ,
74
68
) : string | undefined {
75
69
const info = getInfo ( importingSourceFileName , host ) ;
76
- const modulePaths = getAllModulePaths ( files , importingSourceFileName , nodeModulesFileName , info . getCanonicalFileName , host , redirectTargetsMap ) ;
70
+ const modulePaths = getAllModulePaths ( importingSourceFileName , nodeModulesFileName , host ) ;
77
71
return firstDefined ( modulePaths ,
78
72
moduleFileName => tryGetModuleNameAsNodeModule ( moduleFileName , info , host , compilerOptions , /*packageNameOnly*/ true ) ) ;
79
73
}
@@ -83,12 +77,10 @@ namespace ts.moduleSpecifiers {
83
77
importingSourceFileName : Path ,
84
78
toFileName : string ,
85
79
host : ModuleSpecifierResolutionHost ,
86
- files : readonly SourceFile [ ] ,
87
- redirectTargetsMap : RedirectTargetsMap ,
88
80
preferences : Preferences
89
81
) : string {
90
82
const info = getInfo ( importingSourceFileName , host ) ;
91
- const modulePaths = getAllModulePaths ( files , importingSourceFileName , toFileName , info . getCanonicalFileName , host , redirectTargetsMap ) ;
83
+ const modulePaths = getAllModulePaths ( importingSourceFileName , toFileName , host ) ;
92
84
return firstDefined ( modulePaths , moduleFileName => tryGetModuleNameAsNodeModule ( moduleFileName , info , host , compilerOptions ) ) ||
93
85
getLocalModuleSpecifier ( toFileName , info , compilerOptions , preferences ) ;
94
86
}
@@ -99,16 +91,14 @@ namespace ts.moduleSpecifiers {
99
91
compilerOptions : CompilerOptions ,
100
92
importingSourceFile : SourceFile ,
101
93
host : ModuleSpecifierResolutionHost ,
102
- files : readonly SourceFile [ ] ,
103
94
userPreferences : UserPreferences ,
104
- redirectTargetsMap : RedirectTargetsMap ,
105
95
) : readonly string [ ] {
106
96
const ambient = tryGetModuleNameFromAmbientModule ( moduleSymbol ) ;
107
97
if ( ambient ) return [ ambient ] ;
108
98
109
99
const info = getInfo ( importingSourceFile . path , host ) ;
110
100
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 ) ;
112
102
113
103
const preferences = getPreferences ( userPreferences , compilerOptions , importingSourceFile ) ;
114
104
const global = mapDefined ( modulePaths , moduleFileName => tryGetModuleNameAsNodeModule ( moduleFileName , info , host , compilerOptions ) ) ;
@@ -179,26 +169,24 @@ namespace ts.moduleSpecifiers {
179
169
}
180
170
181
171
export function forEachFileNameOfModule < T > (
182
- files : readonly SourceFile [ ] ,
183
172
importingFileName : string ,
184
173
importedFileName : string ,
185
- getCanonicalFileName : GetCanonicalFileName ,
186
174
host : ModuleSpecifierResolutionHost ,
187
- redirectTargetsMap : RedirectTargetsMap ,
188
175
preferSymlinks : boolean ,
189
176
cb : ( fileName : string ) => T | undefined
190
177
) : 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 ] ;
194
182
const targets = importedFileNames . map ( f => getNormalizedAbsolutePath ( f , cwd ) ) ;
195
183
if ( ! preferSymlinks ) {
196
184
const result = forEach ( targets , cb ) ;
197
185
if ( result ) return result ;
198
186
}
199
187
const links = host . getProbableSymlinks
200
- ? host . getProbableSymlinks ( files )
201
- : discoverProbableSymlinks ( files , getCanonicalFileName , cwd ) ;
188
+ ? host . getProbableSymlinks ( host . getSourceFiles ( ) )
189
+ : discoverProbableSymlinks ( host . getSourceFiles ( ) , getCanonicalFileName , cwd ) ;
202
190
203
191
const compareStrings = ( ! host . useCaseSensitiveFileNames || host . useCaseSensitiveFileNames ( ) ) ? compareStringsCaseSensitive : compareStringsCaseInsensitive ;
204
192
const result = forEachEntry ( links , ( resolved , path ) => {
@@ -224,20 +212,20 @@ namespace ts.moduleSpecifiers {
224
212
* Looks for existing imports that use symlinks to this module.
225
213
* Symlinks will be returned first so they are preferred over the real path.
226
214
*/
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 ) ;
229
218
const allFileNames = createMap < string > ( ) ;
219
+ let importedFileFromNodeModules = false ;
230
220
forEachFileNameOfModule (
231
- files ,
232
221
importingFileName ,
233
222
importedFileName ,
234
- getCanonicalFileName ,
235
223
host ,
236
- redirectTargetsMap ,
237
224
/*preferSymlinks*/ true ,
238
225
path => {
239
226
// dont return value, so we collect everything
240
227
allFileNames . set ( path , getCanonicalFileName ( path ) ) ;
228
+ importedFileFromNodeModules = importedFileFromNodeModules || pathContainsNodeModules ( path ) ;
241
229
}
242
230
) ;
243
231
@@ -251,7 +239,10 @@ namespace ts.moduleSpecifiers {
251
239
let pathsInDirectory : string [ ] | undefined ;
252
240
allFileNames . forEach ( ( canonicalFileName , fileName ) => {
253
241
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
+ }
255
246
allFileNames . delete ( fileName ) ;
256
247
}
257
248
} ) ;
0 commit comments