@@ -45,7 +45,7 @@ const programFileListCache = new Map<CanonicalPath, Set<CanonicalPath>>();
45
45
*/
46
46
const tsconfigLastModifiedTimestampCache = new Map < CanonicalPath , number > ( ) ;
47
47
48
- const parsedFilesSeen = new Set < CanonicalPath > ( ) ;
48
+ const parsedFilesSeenHash = new Map < CanonicalPath , string > ( ) ;
49
49
50
50
/**
51
51
* Clear all of the parser caches.
@@ -55,7 +55,7 @@ function clearCaches(): void {
55
55
knownWatchProgramMap . clear ( ) ;
56
56
fileWatchCallbackTrackingMap . clear ( ) ;
57
57
folderWatchCallbackTrackingMap . clear ( ) ;
58
- parsedFilesSeen . clear ( ) ;
58
+ parsedFilesSeenHash . clear ( ) ;
59
59
programFileListCache . clear ( ) ;
60
60
tsconfigLastModifiedTimestampCache . clear ( ) ;
61
61
}
@@ -104,6 +104,19 @@ function diagnosticReporter(diagnostic: ts.Diagnostic): void {
104
104
) ;
105
105
}
106
106
107
+ /**
108
+ * Hash content for compare content.
109
+ * @param content hashed contend
110
+ * @returns hashed result
111
+ */
112
+ function createHash ( content : string ) : string {
113
+ // No ts.sys in browser environments.
114
+ if ( ts . sys && ts . sys . createHash ) {
115
+ return ts . sys . createHash ( content ) ;
116
+ }
117
+ return content ;
118
+ }
119
+
107
120
/**
108
121
* Calculate project environments using options provided by consumer and paths from config
109
122
* @param code The code being linted
@@ -125,10 +138,10 @@ function getProgramsForProjects(
125
138
currentLintOperationState . filePath = filePath ;
126
139
127
140
// Update file version if necessary
128
- // TODO: only update when necessary, currently marks as changed on every lint
129
141
const fileWatchCallbacks = fileWatchCallbackTrackingMap . get ( filePath ) ;
142
+ const codeHash = createHash ( code ) ;
130
143
if (
131
- parsedFilesSeen . has ( filePath ) &&
144
+ parsedFilesSeenHash . get ( filePath ) !== codeHash &&
132
145
fileWatchCallbacks &&
133
146
fileWatchCallbacks . size > 0
134
147
) {
@@ -232,11 +245,15 @@ function createWatchProgram(
232
245
const oldReadFile = watchCompilerHost . readFile ;
233
246
watchCompilerHost . readFile = ( filePathIn , encoding ) : string | undefined => {
234
247
const filePath = getCanonicalFileName ( filePathIn ) ;
235
- parsedFilesSeen . add ( filePath ) ;
236
- return path . normalize ( filePath ) ===
248
+ const fileContent =
249
+ path . normalize ( filePath ) ===
237
250
path . normalize ( currentLintOperationState . filePath )
238
- ? currentLintOperationState . code
239
- : oldReadFile ( filePath , encoding ) ;
251
+ ? currentLintOperationState . code
252
+ : oldReadFile ( filePath , encoding ) ;
253
+ if ( fileContent ) {
254
+ parsedFilesSeenHash . set ( filePath , createHash ( fileContent ) ) ;
255
+ }
256
+ return fileContent ;
240
257
} ;
241
258
242
259
// ensure process reports error on failure instead of exiting process immediately
0 commit comments