@@ -795,41 +795,6 @@ namespace ts.server {
795795 }
796796 }
797797
798- /* @internal */
799- private extractUnresolvedImportsFromSourceFile ( file : SourceFile , ambientModules : string [ ] ) : ReadonlyArray < string > {
800- const cached = this . cachedUnresolvedImportsPerFile . get ( file . path ) ;
801- if ( cached ) {
802- // found cached result, return
803- return cached ;
804- }
805- let unresolvedImports : string [ ] | undefined ;
806- if ( file . resolvedModules ) {
807- file . resolvedModules . forEach ( ( resolvedModule , name ) => {
808- // pick unresolved non-relative names
809- if ( ! resolvedModule && ! isExternalModuleNameRelative ( name ) && ! isAmbientlyDeclaredModule ( name ) ) {
810- // for non-scoped names extract part up-to the first slash
811- // for scoped names - extract up to the second slash
812- let trimmed = name . trim ( ) ;
813- let i = trimmed . indexOf ( "/" ) ;
814- if ( i !== - 1 && trimmed . charCodeAt ( 0 ) === CharacterCodes . at ) {
815- i = trimmed . indexOf ( "/" , i + 1 ) ;
816- }
817- if ( i !== - 1 ) {
818- trimmed = trimmed . substr ( 0 , i ) ;
819- }
820- ( unresolvedImports || ( unresolvedImports = [ ] ) ) . push ( trimmed ) ;
821- }
822- } ) ;
823- }
824-
825- this . cachedUnresolvedImportsPerFile . set ( file . path , unresolvedImports || emptyArray ) ;
826- return unresolvedImports || emptyArray ;
827-
828- function isAmbientlyDeclaredModule ( name : string ) {
829- return ambientModules . some ( m => m === name ) ;
830- }
831- }
832-
833798 /* @internal */
834799 onFileAddedOrRemoved ( ) {
835800 this . hasAddedorRemovedFiles = true ;
@@ -863,15 +828,7 @@ namespace ts.server {
863828 // (can reuse cached imports for files that were not changed)
864829 // 4. compilation settings were changed in the way that might affect module resolution - drop all caches and collect all data from the scratch
865830 if ( hasNewProgram || changedFiles . length ) {
866- let result : string [ ] | undefined ;
867- const ambientModules = this . program . getTypeChecker ( ) . getAmbientModules ( ) . map ( mod => stripQuotes ( mod . getName ( ) ) ) ;
868- for ( const sourceFile of this . program . getSourceFiles ( ) ) {
869- const unResolved = this . extractUnresolvedImportsFromSourceFile ( sourceFile , ambientModules ) ;
870- if ( unResolved !== emptyArray ) {
871- ( result || ( result = [ ] ) ) . push ( ...unResolved ) ;
872- }
873- }
874- this . lastCachedUnresolvedImportsList = result ? sortAndDeduplicate ( result ) : emptyArray ;
831+ this . lastCachedUnresolvedImportsList = getUnresolvedImports ( this . program , this . cachedUnresolvedImportsPerFile ) ;
875832 }
876833
877834 this . projectService . typingsCache . enqueueInstallTypingsForProject ( this , this . lastCachedUnresolvedImportsList , hasAddedorRemovedFiles ) ;
@@ -1218,6 +1175,25 @@ namespace ts.server {
12181175 }
12191176 }
12201177
1178+ function getUnresolvedImports ( program : Program , cachedUnresolvedImportsPerFile : Map < ReadonlyArray < string > > ) : SortedReadonlyArray < string > {
1179+ const ambientModules = program . getTypeChecker ( ) . getAmbientModules ( ) . map ( mod => stripQuotes ( mod . getName ( ) ) ) ;
1180+ return sortAndDeduplicate ( flatMap ( program . getSourceFiles ( ) , sourceFile =>
1181+ extractUnresolvedImportsFromSourceFile ( sourceFile , ambientModules , cachedUnresolvedImportsPerFile ) ) ) ;
1182+ }
1183+ function extractUnresolvedImportsFromSourceFile ( file : SourceFile , ambientModules : ReadonlyArray < string > , cachedUnresolvedImportsPerFile : Map < ReadonlyArray < string > > ) : ReadonlyArray < string > {
1184+ return getOrUpdate ( cachedUnresolvedImportsPerFile , file . path , ( ) => {
1185+ if ( ! file . resolvedModules ) return emptyArray ;
1186+ let unresolvedImports : string [ ] | undefined ;
1187+ file . resolvedModules . forEach ( ( resolvedModule , name ) => {
1188+ // pick unresolved non-relative names
1189+ if ( ! resolvedModule && ! isExternalModuleNameRelative ( name ) && ! ambientModules . some ( m => m === name ) ) {
1190+ unresolvedImports = append ( unresolvedImports , parsePackageName ( name ) . packageName ) ;
1191+ }
1192+ } ) ;
1193+ return unresolvedImports || emptyArray ;
1194+ } ) ;
1195+ }
1196+
12211197 /**
12221198 * If a file is opened and no tsconfig (or jsconfig) is found,
12231199 * the file and its imports/references are put into an InferredProject.
0 commit comments