diff --git a/.changeset/spicy-swans-work.md b/.changeset/spicy-swans-work.md new file mode 100644 index 000000000000..cdbed796b106 --- /dev/null +++ b/.changeset/spicy-swans-work.md @@ -0,0 +1,5 @@ +--- +"@typescript/vfs": patch +--- + +Fix getScriptSnapshot for file with empty content diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index be88ea840ac8..8b3e6c16f481 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -671,7 +671,7 @@ export function createVirtualLanguageServiceHost( getScriptFileNames: () => fileNames.slice(), getScriptSnapshot: fileName => { const contents = sys.readFile(fileName) - if (contents && typeof contents === "string") { + if (typeof contents === "string") { return ts.ScriptSnapshot.fromString(contents) } return diff --git a/packages/typescript-vfs/test/index.test.ts b/packages/typescript-vfs/test/index.test.ts index 8e2022af543b..35ae1d85abf2 100644 --- a/packages/typescript-vfs/test/index.test.ts +++ b/packages/typescript-vfs/test/index.test.ts @@ -245,3 +245,16 @@ it("moduleDetection options", async () => { program.emit() expect(fsMap.get("index.js")).toEqual(`define(["require", "exports"], function (require, exports) {\n "use strict";\n Object.defineProperty(exports, "__esModule", { value: true });\n var foo = 'foo';\n});\n`) }) + +it("update file content to empty string", () => { + const options: ts.CompilerOptions = { + target: ts.ScriptTarget.ES2020, + } + const fsMap = createDefaultMapFromNodeModules(options, ts) + fsMap.set("index.ts", "console.logx('')") + const system = createSystem(fsMap) + const env = createVirtualTypeScriptEnvironment(system, ["index.ts"], ts, options) + env.updateFile("index.ts", "") + const diagnostics = env.languageService.getSemanticDiagnostics("index.ts") + expect(diagnostics.length).toBe(0) +})