@@ -363,6 +363,10 @@ namespace ts.codefix {
363
363
}
364
364
}
365
365
366
+ if ( isPathRelativeToParent ( relativeToBaseUrl ) ) {
367
+ return [ relativePath ] ;
368
+ }
369
+
366
370
/*
367
371
Prefer a relative import over a baseUrl import if it doesn't traverse up to baseUrl.
368
372
@@ -424,24 +428,25 @@ namespace ts.codefix {
424
428
}
425
429
}
426
430
427
- function tryGetModuleNameFromPaths ( relativeNameWithIndex : string , relativeName : string , paths : MapLike < ReadonlyArray < string > > ) : string | undefined {
431
+ function tryGetModuleNameFromPaths ( relativeToBaseUrlWithIndex : string , relativeToBaseUrl : string , paths : MapLike < ReadonlyArray < string > > ) : string | undefined {
428
432
for ( const key in paths ) {
429
- for ( const pattern of paths [ key ] ) {
433
+ for ( const patternText of paths [ key ] ) {
434
+ const pattern = removeFileExtension ( normalizePath ( patternText ) ) ;
430
435
const indexOfStar = pattern . indexOf ( "*" ) ;
431
436
if ( indexOfStar === 0 && pattern . length === 1 ) {
432
437
continue ;
433
438
}
434
439
else if ( indexOfStar !== - 1 ) {
435
440
const prefix = pattern . substr ( 0 , indexOfStar ) ;
436
441
const suffix = pattern . substr ( indexOfStar + 1 ) ;
437
- if ( relativeName . length >= prefix . length + suffix . length &&
438
- startsWith ( relativeName , prefix ) &&
439
- endsWith ( relativeName , suffix ) ) {
440
- const matchedStar = relativeName . substr ( prefix . length , relativeName . length - suffix . length ) ;
441
- return key . replace ( "\ *" , matchedStar ) ;
442
+ if ( relativeToBaseUrl . length >= prefix . length + suffix . length &&
443
+ startsWith ( relativeToBaseUrl , prefix ) &&
444
+ endsWith ( relativeToBaseUrl , suffix ) ) {
445
+ const matchedStar = relativeToBaseUrl . substr ( prefix . length , relativeToBaseUrl . length - suffix . length ) ;
446
+ return key . replace ( "*" , matchedStar ) ;
442
447
}
443
448
}
444
- else if ( pattern === relativeName || pattern === relativeNameWithIndex ) {
449
+ else if ( pattern === relativeToBaseUrl || pattern === relativeToBaseUrlWithIndex ) {
445
450
return key ;
446
451
}
447
452
}
@@ -601,7 +606,10 @@ namespace ts.codefix {
601
606
}
602
607
603
608
function getPathRelativeToRootDirs ( path : string , rootDirs : ReadonlyArray < string > , getCanonicalFileName : GetCanonicalFileName ) : string | undefined {
604
- return firstDefined ( rootDirs , rootDir => getRelativePathIfInDirectory ( path , rootDir , getCanonicalFileName ) ) ;
609
+ return firstDefined ( rootDirs , rootDir => {
610
+ const relativePath = getRelativePathIfInDirectory ( path , rootDir , getCanonicalFileName ) ;
611
+ return isPathRelativeToParent ( relativePath ) ? undefined : relativePath ;
612
+ } ) ;
605
613
}
606
614
607
615
function removeExtensionAndIndexPostFix ( fileName : string , options : CompilerOptions , addJsExtension : boolean ) : string {
@@ -615,7 +623,11 @@ namespace ts.codefix {
615
623
616
624
function getRelativePathIfInDirectory ( path : string , directoryPath : string , getCanonicalFileName : GetCanonicalFileName ) : string | undefined {
617
625
const relativePath = getRelativePathToDirectoryOrUrl ( directoryPath , path , directoryPath , getCanonicalFileName , /*isAbsolutePathAnUrl*/ false ) ;
618
- return isRootedDiskPath ( relativePath ) || startsWith ( relativePath , ".." ) ? undefined : relativePath ;
626
+ return isRootedDiskPath ( relativePath ) ? undefined : relativePath ;
627
+ }
628
+
629
+ function isPathRelativeToParent ( path : string ) : boolean {
630
+ return startsWith ( path , ".." ) ;
619
631
}
620
632
621
633
function getRelativePath ( path : string , directoryPath : string , getCanonicalFileName : GetCanonicalFileName ) {
0 commit comments