From 9e84d720e6e881cecc34f5e0d87fbe291b9eba17 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 20 Nov 2021 20:09:22 -0500 Subject: [PATCH] fix: align `getScriptSnapshot` compiler host libFileMap behaviour with `readFile` --- deno/common/ts_morph_common.js | 16 +++++++--------- packages/common/src/compiler/createHosts.ts | 18 ++++++++---------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/deno/common/ts_morph_common.js b/deno/common/ts_morph_common.js index 582b87aad..298cfe417 100644 --- a/deno/common/ts_morph_common.js +++ b/deno/common/ts_morph_common.js @@ -1034,18 +1034,16 @@ function createHosts(options) { }, getScriptSnapshot: fileName => { const filePath = transactionalFileSystem.getStandardizedAbsolutePath(fileName); - if (!fileExistsSync(filePath)) { - if (libFileMap != null) { - const libFileText = libFileMap.get(filePath); - if (libFileText != null) - return ts.ScriptSnapshot.fromString(libFileText); - } - return undefined; + if (libFileMap != null) { + const libFileText = libFileMap.get(filePath); + if (libFileText != null) + return ts.ScriptSnapshot.fromString(libFileText); } - return ts.ScriptSnapshot.fromString(sourceFileContainer.addOrGetSourceFileFromFilePathSync(filePath, { + const sourceFile = sourceFileContainer.addOrGetSourceFileFromFilePathSync(filePath, { markInProject: false, scriptKind: undefined, - }).getFullText()); + }); + return sourceFile ? ts.ScriptSnapshot.fromString(sourceFile.getFullText()) : undefined; }, getCurrentDirectory: () => transactionalFileSystem.getCurrentDirectory(), getDefaultLibFileName: options => { diff --git a/packages/common/src/compiler/createHosts.ts b/packages/common/src/compiler/createHosts.ts index 8a43e37bb..84d46d525 100644 --- a/packages/common/src/compiler/createHosts.ts +++ b/packages/common/src/compiler/createHosts.ts @@ -65,19 +65,17 @@ export function createHosts(options: CreateHostsOptions) { }, getScriptSnapshot: fileName => { const filePath = transactionalFileSystem.getStandardizedAbsolutePath(fileName); - if (!fileExistsSync(filePath)) { - if (libFileMap != null) { - const libFileText = libFileMap.get(filePath); - if (libFileText != null) - return ts.ScriptSnapshot.fromString(libFileText); - } - - return undefined; + if (libFileMap != null) { + const libFileText = libFileMap.get(filePath); + if (libFileText != null) + return ts.ScriptSnapshot.fromString(libFileText); } - return ts.ScriptSnapshot.fromString(sourceFileContainer.addOrGetSourceFileFromFilePathSync(filePath, { + + const sourceFile = sourceFileContainer.addOrGetSourceFileFromFilePathSync(filePath, { markInProject: false, scriptKind: undefined, - })!.getFullText()); + }); + return sourceFile ? ts.ScriptSnapshot.fromString(sourceFile.getFullText()) : undefined; }, getCurrentDirectory: () => transactionalFileSystem.getCurrentDirectory(), getDefaultLibFileName: options => {