@@ -786,9 +786,9 @@ namespace ts.codefix {
786
786
cb : ( module : Symbol ) => void ,
787
787
) {
788
788
let filteredCount = 0 ;
789
- const packageJson = filterByPackageJson && createAutoImportFilter ( from , program , host ) ;
789
+ const moduleSpecifierResolutionHost = createModuleSpecifierResolutionHost ( program , host ) ;
790
+ const packageJson = filterByPackageJson && createAutoImportFilter ( from , program , host , moduleSpecifierResolutionHost ) ;
790
791
const allSourceFiles = program . getSourceFiles ( ) ;
791
- const globalTypingsCache = host . getGlobalTypingsCacheLocation && host . getGlobalTypingsCacheLocation ( ) ;
792
792
forEachExternalModule ( program . getTypeChecker ( ) , allSourceFiles , ( module , sourceFile ) => {
793
793
if ( sourceFile === undefined ) {
794
794
if ( ! packageJson || packageJson . allowsImportingAmbientModule ( module , allSourceFiles ) ) {
@@ -800,7 +800,7 @@ namespace ts.codefix {
800
800
}
801
801
else if ( sourceFile &&
802
802
sourceFile !== from &&
803
- isImportablePath ( from . fileName , sourceFile . fileName , hostGetCanonicalFileName ( host ) , globalTypingsCache )
803
+ isImportableFile ( program , from , sourceFile , moduleSpecifierResolutionHost )
804
804
) {
805
805
if ( ! packageJson || packageJson . allowsImportingSourceFile ( sourceFile , allSourceFiles ) ) {
806
806
cb ( module ) ;
@@ -826,6 +826,32 @@ namespace ts.codefix {
826
826
}
827
827
}
828
828
829
+ function isImportableFile (
830
+ program : Program ,
831
+ from : SourceFile ,
832
+ to : SourceFile ,
833
+ moduleSpecifierResolutionHost : ModuleSpecifierResolutionHost
834
+ ) {
835
+ const getCanonicalFileName = hostGetCanonicalFileName ( moduleSpecifierResolutionHost ) ;
836
+ const globalTypingsCache = moduleSpecifierResolutionHost . getGlobalTypingsCacheLocation ?.( ) ;
837
+ return ! ! moduleSpecifiers . forEachFileNameOfModule (
838
+ program . getSourceFiles ( ) ,
839
+ from . fileName ,
840
+ to . fileName ,
841
+ hostGetCanonicalFileName ( moduleSpecifierResolutionHost ) ,
842
+ moduleSpecifierResolutionHost ,
843
+ program . redirectTargetsMap ,
844
+ /*preferSymlinks*/ false ,
845
+ toPath => {
846
+ const toFile = program . getSourceFile ( toPath ) ;
847
+ // Determine to import using toPath only if toPath is what we were looking at
848
+ // or there doesnt exist the file in the program by the symlink
849
+ return ( toFile === to || ! toFile ) &&
850
+ isImportablePath ( from . fileName , toPath , getCanonicalFileName , globalTypingsCache ) ;
851
+ }
852
+ ) ;
853
+ }
854
+
829
855
/**
830
856
* Don't include something from a `node_modules` that isn't actually reachable by a global import.
831
857
* A relative import to node_modules is usually a bad idea.
@@ -870,12 +896,10 @@ namespace ts.codefix {
870
896
return ! isStringANonContextualKeyword ( res ) ? res || "_" : `_${ res } ` ;
871
897
}
872
898
873
- function createAutoImportFilter ( fromFile : SourceFile , program : Program , host : LanguageServiceHost ) {
874
- const packageJsons = host . getPackageJsonsVisibleToFile && host . getPackageJsonsVisibleToFile ( fromFile . fileName ) || getPackageJsonsVisibleToFile ( fromFile . fileName , host ) ;
875
- const dependencyGroups = PackageJsonDependencyGroup . Dependencies | PackageJsonDependencyGroup . DevDependencies | PackageJsonDependencyGroup . OptionalDependencies ;
899
+ function createModuleSpecifierResolutionHost ( program : Program , host : LanguageServiceHost ) : ModuleSpecifierResolutionHost {
876
900
// Mix in `getProbablySymlinks` from Program when host doesn't have it
877
901
// in order for non-Project hosts to have a symlinks cache.
878
- const moduleSpecifierResolutionHost : ModuleSpecifierResolutionHost = {
902
+ return {
879
903
directoryExists : maybeBind ( host , host . directoryExists ) ,
880
904
fileExists : maybeBind ( host , host . fileExists ) ,
881
905
getCurrentDirectory : maybeBind ( host , host . getCurrentDirectory ) ,
@@ -884,9 +908,14 @@ namespace ts.codefix {
884
908
getProbableSymlinks : maybeBind ( host , host . getProbableSymlinks ) || program . getProbableSymlinks ,
885
909
getGlobalTypingsCacheLocation : maybeBind ( host , host . getGlobalTypingsCacheLocation ) ,
886
910
} ;
911
+ }
912
+
913
+ function createAutoImportFilter ( fromFile : SourceFile , program : Program , host : LanguageServiceHost , moduleSpecifierResolutionHost = createModuleSpecifierResolutionHost ( program , host ) ) {
914
+ const packageJsons = host . getPackageJsonsVisibleToFile && host . getPackageJsonsVisibleToFile ( fromFile . fileName ) || getPackageJsonsVisibleToFile ( fromFile . fileName , host ) ;
915
+ const dependencyGroups = PackageJsonDependencyGroup . Dependencies | PackageJsonDependencyGroup . DevDependencies | PackageJsonDependencyGroup . OptionalDependencies ;
887
916
888
917
let usesNodeCoreModules : boolean | undefined ;
889
- return { allowsImportingAmbientModule, allowsImportingSourceFile, allowsImportingSpecifier } ;
918
+ return { allowsImportingAmbientModule, allowsImportingSourceFile, allowsImportingSpecifier, moduleSpecifierResolutionHost } ;
890
919
891
920
function moduleSpecifierIsCoveredByPackageJson ( specifier : string ) {
892
921
const packageName = getNodeModuleRootSpecifier ( specifier ) ;
0 commit comments