Skip to content

Commit

Permalink
[@typescript/vfs] Fix the exception when file content is empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiyao committed Mar 2, 2024
1 parent 2ca478b commit 2fa00c5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/typescript-vfs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export function createSystem(files: Map<string, string>): System {
getDirectories: () => [],
getExecutingFilePath: () => notImplemented("getExecutingFilePath"),
readDirectory: audit("readDirectory", directory => (directory === "/" ? Array.from(files.keys()) : [])),
readFile: audit("readFile", fileName => files.get(fileName) || files.get(libize(fileName))),
readFile: audit("readFile", fileName => files.get(fileName) ?? files.get(libize(fileName))),
resolvePath: path => path,
newLine: "\n",
useCaseSensitiveFileNames: true,
Expand Down
14 changes: 14 additions & 0 deletions packages/typescript-vfs/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,17 @@ it("grabs lib dts files from node_modules", async () => {
const fsMap = createDefaultMapFromNodeModules({})
expect(fsMap.get("/lib.es2015.collection.d.ts")).toBeDefined()
})

it("empty file content", async () => {
const options = { target: ts.ScriptTarget.ES2020 }
const fsMap = createDefaultMapFromNodeModules(options, ts)
fsMap.set("index.ts", "")
const system = createSystem(fsMap)
const host = createVirtualCompilerHost(system, options, ts)
ts.createProgram({
rootNames: ["index.ts"],
options,
host: host.compilerHost,
})
})

0 comments on commit 2fa00c5

Please sign in to comment.