Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix getScriptSnapshot for file with empty content #3358

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/spicy-swans-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typescript/vfs": patch
---

Fix getScriptSnapshot for file with empty content
2 changes: 1 addition & 1 deletion packages/typescript-vfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions packages/typescript-vfs/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})