Skip to content

Commit b6659e5

Browse files
committed
Inline function to tidy up control flow
1 parent 4ed80b6 commit b6659e5

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

src/compiler/program.ts

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace ts {
9696
if (existingDirectories.has(directoryPath)) {
9797
return true;
9898
}
99-
if (system.directoryExists(directoryPath)) {
99+
if ((compilerHost.directoryExists || system.directoryExists)(directoryPath)) {
100100
existingDirectories.set(directoryPath, true);
101101
return true;
102102
}
@@ -107,45 +107,8 @@ namespace ts {
107107
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
108108
const parentDirectory = getDirectoryPath(directoryPath);
109109
ensureDirectoriesExist(parentDirectory);
110-
if (compilerHost.createDirectory) {
111-
compilerHost.createDirectory(directoryPath);
112-
}
113-
else {
114-
system.createDirectory(directoryPath);
115-
}
116-
}
117-
}
118-
119-
let outputFingerprints: Map<OutputFingerprint>;
120-
121-
function writeFileIfUpdated(fileName: string, data: string, writeByteOrderMark: boolean): void {
122-
if (!outputFingerprints) {
123-
outputFingerprints = createMap<OutputFingerprint>();
124-
}
125-
126-
const hash = system.createHash!(data); // TODO: GH#18217
127-
const mtimeBefore = system.getModifiedTime!(fileName); // TODO: GH#18217
128-
129-
if (mtimeBefore) {
130-
const fingerprint = outputFingerprints.get(fileName);
131-
// If output has not been changed, and the file has no external modification
132-
if (fingerprint &&
133-
fingerprint.byteOrderMark === writeByteOrderMark &&
134-
fingerprint.hash === hash &&
135-
fingerprint.mtime.getTime() === mtimeBefore.getTime()) {
136-
return;
137-
}
110+
(compilerHost.createDirectory || system.createDirectory)(directoryPath);
138111
}
139-
140-
system.writeFile(fileName, data, writeByteOrderMark);
141-
142-
const mtimeAfter = system.getModifiedTime!(fileName) || missingFileModifiedTime; // TODO: GH#18217
143-
144-
outputFingerprints.set(fileName, {
145-
hash,
146-
byteOrderMark: writeByteOrderMark,
147-
mtime: mtimeAfter
148-
});
149112
}
150113

151114
function writeFile(fileName: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
@@ -155,6 +118,9 @@ namespace ts {
155118
// PERF: Checking for directory existence is expensive.
156119
// Instead, assume the directory exists and fall back
157120
// to creating it if the file write fails.
121+
// NOTE: If patchWriteFileEnsuringDirectory has been called,
122+
// the file write will do its own directory creation and
123+
// the ensureDirectoriesExist call will always be redundant.
158124
try {
159125
writeFileWorker(fileName, data, writeByteOrderMark);
160126
}
@@ -173,13 +139,40 @@ namespace ts {
173139
}
174140
}
175141

142+
let outputFingerprints: Map<OutputFingerprint>;
176143
function writeFileWorker(fileName: string, data: string, writeByteOrderMark: boolean) {
177-
if (isWatchSet(options) && system.createHash && system.getModifiedTime) {
178-
writeFileIfUpdated(fileName, data, writeByteOrderMark);
179-
}
180-
else {
144+
if (!isWatchSet(options) || !system.createHash || !system.getModifiedTime) {
181145
system.writeFile(fileName, data, writeByteOrderMark);
146+
return;
147+
}
148+
149+
if (!outputFingerprints) {
150+
outputFingerprints = createMap<OutputFingerprint>();
151+
}
152+
153+
const hash = system.createHash(data);
154+
const mtimeBefore = system.getModifiedTime(fileName);
155+
156+
if (mtimeBefore) {
157+
const fingerprint = outputFingerprints.get(fileName);
158+
// If output has not been changed, and the file has no external modification
159+
if (fingerprint &&
160+
fingerprint.byteOrderMark === writeByteOrderMark &&
161+
fingerprint.hash === hash &&
162+
fingerprint.mtime.getTime() === mtimeBefore.getTime()) {
163+
return;
164+
}
182165
}
166+
167+
system.writeFile(fileName, data, writeByteOrderMark);
168+
169+
const mtimeAfter = system.getModifiedTime(fileName) || missingFileModifiedTime;
170+
171+
outputFingerprints.set(fileName, {
172+
hash,
173+
byteOrderMark: writeByteOrderMark,
174+
mtime: mtimeAfter
175+
});
183176
}
184177

185178
function getDefaultLibLocation(): string {

0 commit comments

Comments
 (0)