@@ -416,8 +416,8 @@ namespace ts.codefix {
416416 ) ;
417417
418418 function getModuleSpecifierForNewImport ( ) {
419- const fileName = sourceFile . path ;
420- const moduleFileName = moduleSymbol . valueDeclaration . getSourceFile ( ) . path ;
419+ const fileName = sourceFile . fileName ;
420+ const moduleFileName = moduleSymbol . valueDeclaration . getSourceFile ( ) . fileName ;
421421 const sourceDirectory = getDirectoryPath ( fileName ) ;
422422 const options = context . program . getCompilerOptions ( ) ;
423423
@@ -439,8 +439,7 @@ namespace ts.codefix {
439439 return undefined ;
440440 }
441441
442- const normalizedBaseUrl = toPath ( options . baseUrl , getDirectoryPath ( options . baseUrl ) , getCanonicalFileName ) ;
443- let relativeName = tryRemoveParentDirectoryName ( moduleFileName , normalizedBaseUrl ) ;
442+ let relativeName = getRelativePathIfInDirectory ( moduleFileName , options . baseUrl ) ;
444443 if ( ! relativeName ) {
445444 return undefined ;
446445 }
@@ -477,9 +476,8 @@ namespace ts.codefix {
477476
478477 function tryGetModuleNameFromRootDirs ( ) {
479478 if ( options . rootDirs ) {
480- const normalizedRootDirs = map ( options . rootDirs , rootDir => toPath ( rootDir , /*basePath*/ undefined , getCanonicalFileName ) ) ;
481- const normalizedTargetPath = getPathRelativeToRootDirs ( moduleFileName , normalizedRootDirs ) ;
482- const normalizedSourcePath = getPathRelativeToRootDirs ( sourceDirectory , normalizedRootDirs ) ;
479+ const normalizedTargetPath = getPathRelativeToRootDirs ( moduleFileName , options . rootDirs ) ;
480+ const normalizedSourcePath = getPathRelativeToRootDirs ( sourceDirectory , options . rootDirs ) ;
483481 if ( normalizedTargetPath !== undefined ) {
484482 const relativePath = normalizedSourcePath !== undefined ? getRelativePath ( normalizedTargetPath , normalizedSourcePath ) : normalizedTargetPath ;
485483 return removeFileExtension ( relativePath ) ;
@@ -546,9 +544,9 @@ namespace ts.codefix {
546544 }
547545 }
548546
549- function getPathRelativeToRootDirs ( path : Path , rootDirs : Path [ ] ) {
547+ function getPathRelativeToRootDirs ( path : string , rootDirs : string [ ] ) {
550548 for ( const rootDir of rootDirs ) {
551- const relativeName = tryRemoveParentDirectoryName ( path , rootDir ) ;
549+ const relativeName = getRelativePathIfInDirectory ( path , rootDir ) ;
552550 if ( relativeName !== undefined ) {
553551 return relativeName ;
554552 }
@@ -564,19 +562,14 @@ namespace ts.codefix {
564562 return fileName ;
565563 }
566564
567- function getRelativePath ( path : string , directoryPath : string ) {
565+ function getRelativePathIfInDirectory ( path : string , directoryPath : string ) {
568566 const relativePath = getRelativePathToDirectoryOrUrl ( directoryPath , path , directoryPath , getCanonicalFileName , false ) ;
569- return moduleHasNonRelativeName ( relativePath ) ? "./" + relativePath : relativePath ;
567+ return isRootedDiskPath ( relativePath ) || startsWith ( relativePath , ".." ) ? undefined : relativePath ;
570568 }
571569
572- function tryRemoveParentDirectoryName ( path : Path , parentDirectory : Path ) {
573- const index = path . indexOf ( parentDirectory ) ;
574- if ( index === 0 ) {
575- return endsWith ( parentDirectory , directorySeparator )
576- ? path . substring ( parentDirectory . length )
577- : path . substring ( parentDirectory . length + 1 ) ;
578- }
579- return undefined ;
570+ function getRelativePath ( path : string , directoryPath : string ) {
571+ const relativePath = getRelativePathToDirectoryOrUrl ( directoryPath , path , directoryPath , getCanonicalFileName , false ) ;
572+ return moduleHasNonRelativeName ( relativePath ) ? "./" + relativePath : relativePath ;
580573 }
581574 }
582575
0 commit comments