@@ -363,6 +363,10 @@ namespace ts.codefix {
363363 }
364364 }
365365
366+ if ( isPathRelativeToParent ( relativeToBaseUrl ) ) {
367+ return [ relativePath ] ;
368+ }
369+
366370 /*
367371 Prefer a relative import over a baseUrl import if it doesn't traverse up to baseUrl.
368372
@@ -424,24 +428,25 @@ namespace ts.codefix {
424428 }
425429 }
426430
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 {
428432 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 ) ) ;
430435 const indexOfStar = pattern . indexOf ( "*" ) ;
431436 if ( indexOfStar === 0 && pattern . length === 1 ) {
432437 continue ;
433438 }
434439 else if ( indexOfStar !== - 1 ) {
435440 const prefix = pattern . substr ( 0 , indexOfStar ) ;
436441 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 ) ;
442447 }
443448 }
444- else if ( pattern === relativeName || pattern === relativeNameWithIndex ) {
449+ else if ( pattern === relativeToBaseUrl || pattern === relativeToBaseUrlWithIndex ) {
445450 return key ;
446451 }
447452 }
@@ -601,7 +606,10 @@ namespace ts.codefix {
601606 }
602607
603608 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+ } ) ;
605613 }
606614
607615 function removeExtensionAndIndexPostFix ( fileName : string , options : CompilerOptions , addJsExtension : boolean ) : string {
@@ -615,7 +623,11 @@ namespace ts.codefix {
615623
616624 function getRelativePathIfInDirectory ( path : string , directoryPath : string , getCanonicalFileName : GetCanonicalFileName ) : string | undefined {
617625 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 , ".." ) ;
619631 }
620632
621633 function getRelativePath ( path : string , directoryPath : string , getCanonicalFileName : GetCanonicalFileName ) {
0 commit comments