Skip to content

Commit 0ea84b5

Browse files
Fix two issues in the typescript/vfs module. (#3038)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 9f8dea2 commit 0ea84b5

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

.changeset/breezy-gifts-fly.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@typescript/vfs": patch
3+
---
4+
5+
Fix the exception when file content is empty

.changeset/gold-pears-flow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@typescript/vfs": patch
3+
---
4+
5+
Fix `moduleDetection` compiler option is not working

packages/typescript-vfs/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ export function createSystem(files: Map<string, string>): System {
458458
getDirectories: () => [],
459459
getExecutingFilePath: () => notImplemented("getExecutingFilePath"),
460460
readDirectory: audit("readDirectory", directory => (directory === "/" ? Array.from(files.keys()) : [])),
461-
readFile: audit("readFile", fileName => files.get(fileName) || files.get(libize(fileName))),
461+
readFile: audit("readFile", fileName => files.get(fileName) ?? files.get(libize(fileName))),
462462
resolvePath: path => path,
463463
newLine: "\n",
464464
useCaseSensitiveFileNames: true,
@@ -574,14 +574,14 @@ export function createVirtualCompilerHost(sys: System, compilerOptions: Compiler
574574
// getDefaultLibLocation: () => '/',
575575
getDirectories: () => [],
576576
getNewLine: () => sys.newLine,
577-
getSourceFile: fileName => {
577+
getSourceFile: (fileName, languageVersionOrOptions) => {
578578
return (
579579
sourceFiles.get(fileName) ||
580580
save(
581581
ts.createSourceFile(
582582
fileName,
583583
sys.readFile(fileName)!,
584-
compilerOptions.target || defaultCompilerOptions(ts).target!,
584+
languageVersionOrOptions ?? compilerOptions.target ?? defaultCompilerOptions(ts).target!,
585585
false
586586
)
587587
)

packages/typescript-vfs/test/index.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,34 @@ it("grabs lib dts files from node_modules", async () => {
214214
const fsMap = createDefaultMapFromNodeModules({})
215215
expect(fsMap.get("/lib.es2015.collection.d.ts")).toBeDefined()
216216
})
217+
218+
it("empty file content", async () => {
219+
const options = { target: ts.ScriptTarget.ES2020 }
220+
const fsMap = createDefaultMapFromNodeModules(options, ts)
221+
fsMap.set("index.ts", "")
222+
const system = createSystem(fsMap)
223+
const host = createVirtualCompilerHost(system, options, ts)
224+
ts.createProgram({
225+
rootNames: ["index.ts"],
226+
options,
227+
host: host.compilerHost,
228+
})
229+
})
230+
231+
it("moduleDetection options", async () => {
232+
const options: ts.CompilerOptions = {
233+
module: ts.ModuleKind.AMD,
234+
moduleDetection: ts.ModuleDetectionKind.Force,
235+
}
236+
const fsMap = createDefaultMapFromNodeModules(options, ts)
237+
fsMap.set("index.ts", "let foo = 'foo'")
238+
const system = createSystem(fsMap)
239+
const host = createVirtualCompilerHost(system, options, ts)
240+
const program = ts.createProgram({
241+
rootNames: ["index.ts"],
242+
options,
243+
host: host.compilerHost,
244+
})
245+
program.emit()
246+
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`)
247+
})

0 commit comments

Comments
 (0)