Skip to content

Commit

Permalink
Swap the keys for map
Browse files Browse the repository at this point in the history
  • Loading branch information
sheetalkamat committed Oct 31, 2022
1 parent 5f7bc05 commit 4bd8b9b
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ namespace ts {
const originalDirectoryExists = host.directoryExists;
const originalCreateDirectory = host.createDirectory;
const originalWriteFile = host.writeFile;
const readFileCache = new Map<string, string | false>();
const fileExistsCache = new Map<string, boolean>();
const directoryExistsCache = new Map<string, boolean>();
const sourceFileCache = new Map<string, ESMap<SourceFile["impliedNodeFormat"], SourceFile>>();
const readFileCache = new Map<Path, string | false>();
const fileExistsCache = new Map<Path, boolean>();
const directoryExistsCache = new Map<Path, boolean>();
const sourceFileCache = new Map<SourceFile["impliedNodeFormat"], ESMap<Path, SourceFile>>();

const readFileWithCache = (fileName: string): string | undefined => {
const key = toPath(fileName);
Expand Down Expand Up @@ -199,13 +199,13 @@ namespace ts {
const getSourceFileWithCache: CompilerHost["getSourceFile"] | undefined = getSourceFile ? (fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile) => {
const key = toPath(fileName);
const impliedNodeFormat: SourceFile["impliedNodeFormat"] = typeof languageVersionOrOptions === "object" ? languageVersionOrOptions.impliedNodeFormat : undefined;
const forPath = sourceFileCache.get(key);
const value = forPath?.get(impliedNodeFormat);
const forImpliedNodeFormat = sourceFileCache.get(impliedNodeFormat);
const value = forImpliedNodeFormat?.get(key);
if (value) return value;

const sourceFile = getSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile);
if (sourceFile && (isDeclarationFileName(fileName) || fileExtensionIs(fileName, Extension.Json))) {
sourceFileCache.set(key, (forPath || new Map()).set(impliedNodeFormat, sourceFile));
sourceFileCache.set(impliedNodeFormat, (forImpliedNodeFormat || new Map()).set(key, sourceFile));
}
return sourceFile;
} : undefined;
Expand All @@ -227,14 +227,15 @@ namespace ts {
const value = readFileCache.get(key);
if (value !== undefined && value !== data) {
readFileCache.delete(key);
sourceFileCache.delete(key);
sourceFileCache.forEach(map => map.delete(key));
}
else if (getSourceFileWithCache) {
const sourceFileMap = sourceFileCache.get(key);
const sourceFile = sourceFileMap && firstDefinedIterator(sourceFileMap.values(), identity);
if (sourceFile && sourceFile.text !== data) {
sourceFileCache.delete(key);
}
sourceFileCache.forEach(map => {
const sourceFile = map.get(key);
if (sourceFile && sourceFile.text !== data) {
map.delete(key);
}
});
}
originalWriteFile.call(host, fileName, data, ...rest);
};
Expand Down

0 comments on commit 4bd8b9b

Please sign in to comment.