File tree Expand file tree Collapse file tree 1 file changed +17
-14
lines changed Expand file tree Collapse file tree 1 file changed +17
-14
lines changed Original file line number Diff line number Diff line change @@ -423,21 +423,24 @@ namespace ts {
423
423
function getCommonPrefix ( directory : Path , resolution : string ) {
424
424
const resolutionDirectory = toPath ( getDirectoryPath ( resolution ) , currentDirectory , getCanonicalFileName ) ;
425
425
426
- let current = directory ;
427
- let parent = getDirectoryPath ( current ) ;
428
- while (
429
- // keep going until we find a matching prefix
430
- ! startsWith ( resolutionDirectory , current ) ||
431
- // keep going if the prefix is not a complete directory segment, e.g. '/dir' as prefix of '/directory'
432
- resolutionDirectory . length > current . length && resolutionDirectory [ current . length ] !== directorySeparator && current !== parent
433
- ) {
434
- if ( current === parent ) {
435
- return undefined ;
436
- }
437
- current = parent ;
438
- parent = getDirectoryPath ( current ) ;
426
+ // find first position where directory and resolution differs
427
+ let i = 0 ;
428
+ const limit = Math . min ( directory . length , resolutionDirectory . length ) ;
429
+ while ( i < limit && directory . charCodeAt ( i ) === resolutionDirectory . charCodeAt ( i ) ) {
430
+ i ++ ;
431
+ }
432
+ if ( i === directory . length && ( resolutionDirectory . length === i || resolutionDirectory [ i ] === directorySeparator ) ) {
433
+ return directory ;
434
+ }
435
+ const rootLength = getRootLength ( directory ) ;
436
+ if ( i < rootLength ) {
437
+ return undefined ;
438
+ }
439
+ const sep = directory . lastIndexOf ( directorySeparator , i - 1 ) ;
440
+ if ( sep === - 1 ) {
441
+ return undefined ;
439
442
}
440
- return current ;
443
+ return directory . substr ( 0 , Math . max ( sep , rootLength ) ) ;
441
444
}
442
445
}
443
446
}
You can’t perform that action at this time.
0 commit comments