Skip to content

fix(typescript-estree): hash code to reduce update frequency #1179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const programFileListCache = new Map<CanonicalPath, Set<CanonicalPath>>();
*/
const tsconfigLastModifiedTimestampCache = new Map<CanonicalPath, number>();

const parsedFilesSeen = new Set<CanonicalPath>();
const parsedFilesSeenHash = new Map<CanonicalPath, string>();

/**
* Clear all of the parser caches.
Expand All @@ -55,7 +55,7 @@ function clearCaches(): void {
knownWatchProgramMap.clear();
fileWatchCallbackTrackingMap.clear();
folderWatchCallbackTrackingMap.clear();
parsedFilesSeen.clear();
parsedFilesSeenHash.clear();
programFileListCache.clear();
tsconfigLastModifiedTimestampCache.clear();
}
Expand Down Expand Up @@ -104,6 +104,19 @@ function diagnosticReporter(diagnostic: ts.Diagnostic): void {
);
}

/**
* Hash content for compare content.
* @param content hashed contend
* @returns hashed result
*/
function createHash(content: string): string {
// No ts.sys in browser environments.
if (ts.sys && ts.sys.createHash) {
return ts.sys.createHash(content);
}
return content;
}

/**
* Calculate project environments using options provided by consumer and paths from config
* @param code The code being linted
Expand All @@ -125,10 +138,10 @@ function getProgramsForProjects(
currentLintOperationState.filePath = filePath;

// Update file version if necessary
// TODO: only update when necessary, currently marks as changed on every lint
const fileWatchCallbacks = fileWatchCallbackTrackingMap.get(filePath);
const codeHash = createHash(code);
if (
parsedFilesSeen.has(filePath) &&
parsedFilesSeenHash.get(filePath) !== codeHash &&
fileWatchCallbacks &&
fileWatchCallbacks.size > 0
) {
Expand Down Expand Up @@ -232,11 +245,15 @@ function createWatchProgram(
const oldReadFile = watchCompilerHost.readFile;
watchCompilerHost.readFile = (filePathIn, encoding): string | undefined => {
const filePath = getCanonicalFileName(filePathIn);
parsedFilesSeen.add(filePath);
return path.normalize(filePath) ===
const fileContent =
path.normalize(filePath) ===
path.normalize(currentLintOperationState.filePath)
? currentLintOperationState.code
: oldReadFile(filePath, encoding);
? currentLintOperationState.code
: oldReadFile(filePath, encoding);
if (fileContent) {
parsedFilesSeenHash.set(filePath, createHash(fileContent));
}
return fileContent;
};

// ensure process reports error on failure instead of exiting process immediately
Expand Down