@@ -186,6 +186,18 @@ namespace ts.moduleSpecifiers {
186
186
return result ;
187
187
}
188
188
189
+ function numberOfDirectorySeparators ( str : string ) {
190
+ const match = str . match ( / \/ / g) ;
191
+ return match ? match . length : 0 ;
192
+ }
193
+
194
+ function comparePathsByNumberOfDirectrorySeparators ( a : string , b : string ) {
195
+ return compareValues (
196
+ numberOfDirectorySeparators ( a ) ,
197
+ numberOfDirectorySeparators ( b )
198
+ ) ;
199
+ }
200
+
189
201
/**
190
202
* Looks for existing imports that use symlinks to this module.
191
203
* Symlinks will be returned first so they are preferred over the real path.
@@ -214,7 +226,32 @@ namespace ts.moduleSpecifiers {
214
226
}
215
227
} ) ;
216
228
result . push ( ...targets ) ;
217
- return result ;
229
+ if ( result . length < 2 ) return result ;
230
+
231
+ // Sort by paths closest to importing file Name directory
232
+ const allFileNames = arrayToMap ( result , identity , getCanonicalFileName ) ;
233
+ const sortedPaths : string [ ] = [ ] ;
234
+ for (
235
+ let directory = getDirectoryPath ( toPath ( importingFileName , cwd , getCanonicalFileName ) ) ;
236
+ allFileNames . size !== 0 ;
237
+ directory = getDirectoryPath ( directory )
238
+ ) {
239
+ const directoryStart = ensureTrailingDirectorySeparator ( directory ) ;
240
+ let pathsInDirectory : string [ ] | undefined ;
241
+ allFileNames . forEach ( ( canonicalFileName , fileName ) => {
242
+ if ( startsWith ( canonicalFileName , directoryStart ) ) {
243
+ ( pathsInDirectory || ( pathsInDirectory = [ ] ) ) . push ( fileName ) ;
244
+ allFileNames . delete ( fileName ) ;
245
+ }
246
+ } ) ;
247
+ if ( pathsInDirectory ) {
248
+ if ( pathsInDirectory . length > 1 ) {
249
+ pathsInDirectory . sort ( comparePathsByNumberOfDirectrorySeparators ) ;
250
+ }
251
+ sortedPaths . push ( ...pathsInDirectory ) ;
252
+ }
253
+ }
254
+ return sortedPaths ;
218
255
}
219
256
220
257
function tryGetModuleNameFromAmbientModule ( moduleSymbol : Symbol ) : string | undefined {
0 commit comments