@@ -416,8 +416,8 @@ namespace ts.codefix {
416
416
) ;
417
417
418
418
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 ;
421
421
const sourceDirectory = getDirectoryPath ( fileName ) ;
422
422
const options = context . program . getCompilerOptions ( ) ;
423
423
@@ -439,8 +439,7 @@ namespace ts.codefix {
439
439
return undefined ;
440
440
}
441
441
442
- const normalizedBaseUrl = toPath ( options . baseUrl , getDirectoryPath ( options . baseUrl ) , getCanonicalFileName ) ;
443
- let relativeName = tryRemoveParentDirectoryName ( moduleFileName , normalizedBaseUrl ) ;
442
+ let relativeName = getRelativePathIfInDirectory ( moduleFileName , options . baseUrl ) ;
444
443
if ( ! relativeName ) {
445
444
return undefined ;
446
445
}
@@ -477,9 +476,8 @@ namespace ts.codefix {
477
476
478
477
function tryGetModuleNameFromRootDirs ( ) {
479
478
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 ) ;
483
481
if ( normalizedTargetPath !== undefined ) {
484
482
const relativePath = normalizedSourcePath !== undefined ? getRelativePath ( normalizedTargetPath , normalizedSourcePath ) : normalizedTargetPath ;
485
483
return removeFileExtension ( relativePath ) ;
@@ -546,9 +544,9 @@ namespace ts.codefix {
546
544
}
547
545
}
548
546
549
- function getPathRelativeToRootDirs ( path : Path , rootDirs : Path [ ] ) {
547
+ function getPathRelativeToRootDirs ( path : string , rootDirs : string [ ] ) {
550
548
for ( const rootDir of rootDirs ) {
551
- const relativeName = tryRemoveParentDirectoryName ( path , rootDir ) ;
549
+ const relativeName = getRelativePathIfInDirectory ( path , rootDir ) ;
552
550
if ( relativeName !== undefined ) {
553
551
return relativeName ;
554
552
}
@@ -564,19 +562,14 @@ namespace ts.codefix {
564
562
return fileName ;
565
563
}
566
564
567
- function getRelativePath ( path : string , directoryPath : string ) {
565
+ function getRelativePathIfInDirectory ( path : string , directoryPath : string ) {
568
566
const relativePath = getRelativePathToDirectoryOrUrl ( directoryPath , path , directoryPath , getCanonicalFileName , false ) ;
569
- return moduleHasNonRelativeName ( relativePath ) ? "./" + relativePath : relativePath ;
567
+ return isRootedDiskPath ( relativePath ) || startsWith ( relativePath , ".." ) ? undefined : relativePath ;
570
568
}
571
569
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 ;
580
573
}
581
574
}
582
575
0 commit comments