diff --git a/.changeset/breezy-gifts-fly.md b/.changeset/breezy-gifts-fly.md new file mode 100644 index 000000000000..2a16dd647b79 --- /dev/null +++ b/.changeset/breezy-gifts-fly.md @@ -0,0 +1,5 @@ +--- +"@typescript/vfs": patch +--- + +Fix the exception when file content is empty diff --git a/.changeset/gold-pears-flow.md b/.changeset/gold-pears-flow.md new file mode 100644 index 000000000000..637183811187 --- /dev/null +++ b/.changeset/gold-pears-flow.md @@ -0,0 +1,5 @@ +--- +"@typescript/vfs": patch +--- + +Fix `moduleDetection` compiler option is not working diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index b0a7569e901a..8040f29ce242 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -458,7 +458,7 @@ export function createSystem(files: Map): 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, @@ -574,14 +574,14 @@ export function createVirtualCompilerHost(sys: System, compilerOptions: Compiler // getDefaultLibLocation: () => '/', getDirectories: () => [], getNewLine: () => sys.newLine, - getSourceFile: fileName => { + getSourceFile: (fileName, languageVersionOrOptions) => { return ( sourceFiles.get(fileName) || save( ts.createSourceFile( fileName, sys.readFile(fileName)!, - compilerOptions.target || defaultCompilerOptions(ts).target!, + languageVersionOrOptions ?? compilerOptions.target ?? defaultCompilerOptions(ts).target!, false ) ) diff --git a/packages/typescript-vfs/test/index.test.ts b/packages/typescript-vfs/test/index.test.ts index e526272040c1..8e2022af543b 100644 --- a/packages/typescript-vfs/test/index.test.ts +++ b/packages/typescript-vfs/test/index.test.ts @@ -214,3 +214,34 @@ 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, + }) +}) + +it("moduleDetection options", async () => { + const options: ts.CompilerOptions = { + module: ts.ModuleKind.AMD, + moduleDetection: ts.ModuleDetectionKind.Force, + } + const fsMap = createDefaultMapFromNodeModules(options, ts) + fsMap.set("index.ts", "let foo = 'foo'") + const system = createSystem(fsMap) + const host = createVirtualCompilerHost(system, options, ts) + const program = ts.createProgram({ + rootNames: ["index.ts"], + options, + host: host.compilerHost, + }) + 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`) +})