Skip to content

Commit 96d1cc3

Browse files
yoyo930021bradzacher
authored andcommitted
fix(typescript-estree): hash code to reduce update frequency (typescript-eslint#1179)
1 parent c8fe515 commit 96d1cc3

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

packages/typescript-estree/src/create-program/createWatchProgram.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const programFileListCache = new Map<CanonicalPath, Set<CanonicalPath>>();
4545
*/
4646
const tsconfigLastModifiedTimestampCache = new Map<CanonicalPath, number>();
4747

48-
const parsedFilesSeen = new Set<CanonicalPath>();
48+
const parsedFilesSeenHash = new Map<CanonicalPath, string>();
4949

5050
/**
5151
* Clear all of the parser caches.
@@ -55,7 +55,7 @@ function clearCaches(): void {
5555
knownWatchProgramMap.clear();
5656
fileWatchCallbackTrackingMap.clear();
5757
folderWatchCallbackTrackingMap.clear();
58-
parsedFilesSeen.clear();
58+
parsedFilesSeenHash.clear();
5959
programFileListCache.clear();
6060
tsconfigLastModifiedTimestampCache.clear();
6161
}
@@ -104,6 +104,19 @@ function diagnosticReporter(diagnostic: ts.Diagnostic): void {
104104
);
105105
}
106106

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+
107120
/**
108121
* Calculate project environments using options provided by consumer and paths from config
109122
* @param code The code being linted
@@ -125,10 +138,10 @@ function getProgramsForProjects(
125138
currentLintOperationState.filePath = filePath;
126139

127140
// Update file version if necessary
128-
// TODO: only update when necessary, currently marks as changed on every lint
129141
const fileWatchCallbacks = fileWatchCallbackTrackingMap.get(filePath);
142+
const codeHash = createHash(code);
130143
if (
131-
parsedFilesSeen.has(filePath) &&
144+
parsedFilesSeenHash.get(filePath) !== codeHash &&
132145
fileWatchCallbacks &&
133146
fileWatchCallbacks.size > 0
134147
) {
@@ -232,11 +245,15 @@ function createWatchProgram(
232245
const oldReadFile = watchCompilerHost.readFile;
233246
watchCompilerHost.readFile = (filePathIn, encoding): string | undefined => {
234247
const filePath = getCanonicalFileName(filePathIn);
235-
parsedFilesSeen.add(filePath);
236-
return path.normalize(filePath) ===
248+
const fileContent =
249+
path.normalize(filePath) ===
237250
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;
240257
};
241258

242259
// ensure process reports error on failure instead of exiting process immediately

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy