@@ -96,15 +96,10 @@ namespace ts.server {
96
96
*/
97
97
cachedUnresolvedImportsPerFile = createMap < ReadonlyArray < string > > ( ) ;
98
98
99
- /**
100
- * This is the set that has entry to true if file doesnt contain any unresolved import
101
- */
102
- private filesWithNoUnresolvedImports = createMap < true > ( ) ;
103
-
104
99
/*@internal */
105
100
lastCachedUnresolvedImportsList : SortedReadonlyArray < string > ;
106
101
/*@internal */
107
- hasMoreOrLessScriptInfos = false ;
102
+ private hasMoreOrLessFiles = false ;
108
103
109
104
private lastFileExceededProgramSize : string | undefined ;
110
105
@@ -136,10 +131,10 @@ namespace ts.server {
136
131
*/
137
132
private lastReportedVersion = 0 ;
138
133
/**
139
- * Current project structure version.
134
+ * Current project's program version. (incremented everytime new program is created that is not complete reuse from the old one)
140
135
* This property is changed in 'updateGraph' based on the set of files in program
141
136
*/
142
- private projectStructureVersion = 0 ;
137
+ private projectProgramVersion = 0 ;
143
138
/**
144
139
* Current version of the project state. It is changed when:
145
140
* - new root file was added/removed
@@ -566,7 +561,6 @@ namespace ts.server {
566
561
this . resolutionCache . clear ( ) ;
567
562
this . resolutionCache = undefined ;
568
563
this . cachedUnresolvedImportsPerFile = undefined ;
569
- this . filesWithNoUnresolvedImports = undefined ;
570
564
this . directoryStructureHost = undefined ;
571
565
572
566
// Clean up file watchers waiting for missing files
@@ -727,7 +721,6 @@ namespace ts.server {
727
721
else {
728
722
this . resolutionCache . invalidateResolutionOfFile ( info . path ) ;
729
723
}
730
- this . filesWithNoUnresolvedImports . delete ( info . path ) ;
731
724
this . cachedUnresolvedImportsPerFile . delete ( info . path ) ;
732
725
733
726
if ( detachFromProject ) {
@@ -749,19 +742,11 @@ namespace ts.server {
749
742
}
750
743
751
744
/* @internal */
752
- private extractUnresolvedImportsFromSourceFile ( file : SourceFile , result : string [ ] | undefined , ambientModules : string [ ] ) : string [ ] | undefined {
753
- // No unresolve imports in this file
754
- if ( this . filesWithNoUnresolvedImports . has ( file . path ) ) {
755
- return result ;
756
- }
757
-
745
+ private extractUnresolvedImportsFromSourceFile ( file : SourceFile , ambientModules : string [ ] ) : ReadonlyArray < string > {
758
746
const cached = this . cachedUnresolvedImportsPerFile . get ( file . path ) ;
759
747
if ( cached ) {
760
- // found cached result - use it and return
761
- for ( const f of cached ) {
762
- ( result || ( result = [ ] ) ) . push ( f ) ;
763
- }
764
- return result ;
748
+ // found cached result, return
749
+ return cached ;
765
750
}
766
751
let unresolvedImports : string [ ] | undefined ;
767
752
if ( file . resolvedModules ) {
@@ -779,40 +764,39 @@ namespace ts.server {
779
764
trimmed = trimmed . substr ( 0 , i ) ;
780
765
}
781
766
( unresolvedImports || ( unresolvedImports = [ ] ) ) . push ( trimmed ) ;
782
- ( result || ( result = [ ] ) ) . push ( trimmed ) ;
783
767
}
784
768
} ) ;
785
769
}
786
- if ( unresolvedImports ) {
787
- this . cachedUnresolvedImportsPerFile . set ( file . path , unresolvedImports ) ;
788
- }
789
- else {
790
- this . filesWithNoUnresolvedImports . set ( file . path , true ) ;
791
- }
792
- return result ;
770
+
771
+ this . cachedUnresolvedImportsPerFile . set ( file . path , unresolvedImports || emptyArray ) ;
772
+ return unresolvedImports || emptyArray ;
793
773
794
774
function isAmbientlyDeclaredModule ( name : string ) {
795
775
return ambientModules . some ( m => m === name ) ;
796
776
}
797
777
}
798
778
779
+ /* @internal */
780
+ setHasMoreOrLessFiles ( ) {
781
+ this . hasMoreOrLessFiles = true ;
782
+ }
783
+
799
784
/**
800
785
* Updates set of files that contribute to this project
801
786
* @returns : true if set of files in the project stays the same and false - otherwise.
802
787
*/
803
788
updateGraph ( ) : boolean {
804
789
this . resolutionCache . startRecordingFilesWithChangedResolutions ( ) ;
805
790
806
- const hasChanges = this . updateGraphWorker ( ) ;
807
- const hasMoreOrLessScriptInfos = this . hasMoreOrLessScriptInfos ;
808
- this . hasMoreOrLessScriptInfos = false ;
791
+ const hasNewProgram = this . updateGraphWorker ( ) ;
792
+ const hasMoreOrLessFiles = this . hasMoreOrLessFiles ;
793
+ this . hasMoreOrLessFiles = false ;
809
794
810
795
const changedFiles : ReadonlyArray < Path > = this . resolutionCache . finishRecordingFilesWithChangedResolutions ( ) || emptyArray ;
811
796
812
797
for ( const file of changedFiles ) {
813
798
// delete cached information for changed files
814
799
this . cachedUnresolvedImportsPerFile . delete ( file ) ;
815
- this . filesWithNoUnresolvedImports . delete ( file ) ;
816
800
}
817
801
818
802
// update builder only if language service is enabled
@@ -824,25 +808,28 @@ namespace ts.server {
824
808
// 3. new files were added/removed, but compilation settings stays the same - collect unresolved imports for all new/modified files
825
809
// (can reuse cached imports for files that were not changed)
826
810
// 4. compilation settings were changed in the way that might affect module resolution - drop all caches and collect all data from the scratch
827
- if ( hasChanges || changedFiles . length ) {
811
+ if ( hasNewProgram || changedFiles . length ) {
828
812
let result : string [ ] | undefined ;
829
813
const ambientModules = this . program . getTypeChecker ( ) . getAmbientModules ( ) . map ( mod => stripQuotes ( mod . getName ( ) ) ) ;
830
814
for ( const sourceFile of this . program . getSourceFiles ( ) ) {
831
- result = this . extractUnresolvedImportsFromSourceFile ( sourceFile , result , ambientModules ) ;
815
+ const unResolved = this . extractUnresolvedImportsFromSourceFile ( sourceFile , ambientModules ) ;
816
+ if ( unResolved !== emptyArray ) {
817
+ ( result || ( result = [ ] ) ) . push ( ...unResolved ) ;
818
+ }
832
819
}
833
820
this . lastCachedUnresolvedImportsList = result ? toDeduplicatedSortedArray ( result ) : emptyArray ;
834
821
}
835
822
836
- this . projectService . typingsCache . enqueueInstallTypingsForProject ( this , this . lastCachedUnresolvedImportsList , hasMoreOrLessScriptInfos ) ;
823
+ this . projectService . typingsCache . enqueueInstallTypingsForProject ( this , this . lastCachedUnresolvedImportsList , hasMoreOrLessFiles ) ;
837
824
}
838
825
else {
839
826
this . lastCachedUnresolvedImportsList = undefined ;
840
827
}
841
828
842
- if ( hasChanges ) {
843
- this . projectStructureVersion ++ ;
829
+ if ( hasNewProgram ) {
830
+ this . projectProgramVersion ++ ;
844
831
}
845
- return ! hasChanges ;
832
+ return ! hasNewProgram ;
846
833
}
847
834
848
835
/*@internal */
@@ -878,9 +865,9 @@ namespace ts.server {
878
865
// bump up the version if
879
866
// - oldProgram is not set - this is a first time updateGraph is called
880
867
// - newProgram is different from the old program and structure of the old program was not reused.
881
- const hasChanges = this . program && ( ! oldProgram || ( this . program !== oldProgram && ! ( oldProgram . structureIsReused & StructureIsReused . Completely ) ) ) ;
868
+ const hasNewProgram = this . program && ( ! oldProgram || ( this . program !== oldProgram && ! ( oldProgram . structureIsReused & StructureIsReused . Completely ) ) ) ;
882
869
this . hasChangedAutomaticTypeDirectiveNames = false ;
883
- if ( hasChanges ) {
870
+ if ( hasNewProgram ) {
884
871
if ( oldProgram ) {
885
872
for ( const f of oldProgram . getSourceFiles ( ) ) {
886
873
if ( this . program . getSourceFileByPath ( f . path ) ) {
@@ -918,8 +905,8 @@ namespace ts.server {
918
905
removed => this . detachScriptInfoFromProject ( removed )
919
906
) ;
920
907
const elapsed = timestamp ( ) - start ;
921
- this . writeLog ( `Finishing updateGraphWorker: Project: ${ this . getProjectName ( ) } Version: ${ this . getProjectVersion ( ) } structureChanged: ${ hasChanges } Elapsed: ${ elapsed } ms` ) ;
922
- return hasChanges ;
908
+ this . writeLog ( `Finishing updateGraphWorker: Project: ${ this . getProjectName ( ) } Version: ${ this . getProjectVersion ( ) } structureChanged: ${ hasNewProgram } Elapsed: ${ elapsed } ms` ) ;
909
+ return hasNewProgram ;
923
910
}
924
911
925
912
private detachScriptInfoFromProject ( uncheckedFileName : string ) {
@@ -993,7 +980,6 @@ namespace ts.server {
993
980
if ( changesAffectModuleResolution ( oldOptions , compilerOptions ) ) {
994
981
// reset cached unresolved imports if changes in compiler options affected module resolution
995
982
this . cachedUnresolvedImportsPerFile . clear ( ) ;
996
- this . filesWithNoUnresolvedImports . clear ( ) ;
997
983
this . lastCachedUnresolvedImportsList = undefined ;
998
984
this . resolutionCache . clear ( ) ;
999
985
}
@@ -1007,7 +993,7 @@ namespace ts.server {
1007
993
1008
994
const info : protocol . ProjectVersionInfo = {
1009
995
projectName : this . getProjectName ( ) ,
1010
- version : this . projectStructureVersion ,
996
+ version : this . projectProgramVersion ,
1011
997
isInferred : this . projectKind === ProjectKind . Inferred ,
1012
998
options : this . getCompilationSettings ( ) ,
1013
999
languageServiceDisabled : ! this . languageServiceEnabled ,
@@ -1018,7 +1004,7 @@ namespace ts.server {
1018
1004
// check if requested version is the same that we have reported last time
1019
1005
if ( this . lastReportedFileNames && lastKnownVersion === this . lastReportedVersion ) {
1020
1006
// if current structure version is the same - return info without any changes
1021
- if ( this . projectStructureVersion === this . lastReportedVersion && ! updatedFileNames ) {
1007
+ if ( this . projectProgramVersion === this . lastReportedVersion && ! updatedFileNames ) {
1022
1008
return { info, projectErrors : this . getGlobalProjectErrors ( ) } ;
1023
1009
}
1024
1010
// compute and return the difference
@@ -1041,7 +1027,7 @@ namespace ts.server {
1041
1027
}
1042
1028
} ) ;
1043
1029
this . lastReportedFileNames = currentFiles ;
1044
- this . lastReportedVersion = this . projectStructureVersion ;
1030
+ this . lastReportedVersion = this . projectProgramVersion ;
1045
1031
return { info, changes : { added, removed, updated } , projectErrors : this . getGlobalProjectErrors ( ) } ;
1046
1032
}
1047
1033
else {
@@ -1050,7 +1036,7 @@ namespace ts.server {
1050
1036
const externalFiles = this . getExternalFiles ( ) . map ( f => toNormalizedPath ( f ) ) ;
1051
1037
const allFiles = projectFileNames . concat ( externalFiles ) ;
1052
1038
this . lastReportedFileNames = arrayToSet ( allFiles ) ;
1053
- this . lastReportedVersion = this . projectStructureVersion ;
1039
+ this . lastReportedVersion = this . projectProgramVersion ;
1054
1040
return { info, files : allFiles , projectErrors : this . getGlobalProjectErrors ( ) } ;
1055
1041
}
1056
1042
}
0 commit comments