From 2fa00c539f94f8fb613acdd669486582ab136409 Mon Sep 17 00:00:00 2001 From: xiaoxiyao Date: Sat, 2 Mar 2024 21:27:22 +0800 Subject: [PATCH 1/4] [`@typescript/vfs`] Fix the exception when file content is empty. --- packages/typescript-vfs/src/index.ts | 2 +- packages/typescript-vfs/test/index.test.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index e89ba2a57857..d4a422dadf84 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -440,7 +440,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, diff --git a/packages/typescript-vfs/test/index.test.ts b/packages/typescript-vfs/test/index.test.ts index e526272040c1..de56fb761cb7 100644 --- a/packages/typescript-vfs/test/index.test.ts +++ b/packages/typescript-vfs/test/index.test.ts @@ -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, + }) +}) + From 3458b37e7ff419c337bd92cb160aced73ca58058 Mon Sep 17 00:00:00 2001 From: xiaoxiyao Date: Sat, 2 Mar 2024 21:44:05 +0800 Subject: [PATCH 2/4] [`@typescript/vfs`] Fix `moduleDetection` compiler option is not working. --- packages/typescript-vfs/src/index.ts | 4 ++-- packages/typescript-vfs/test/index.test.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index d4a422dadf84..552781e9a7c0 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -555,14 +555,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, false ) ) diff --git a/packages/typescript-vfs/test/index.test.ts b/packages/typescript-vfs/test/index.test.ts index de56fb761cb7..8e2022af543b 100644 --- a/packages/typescript-vfs/test/index.test.ts +++ b/packages/typescript-vfs/test/index.test.ts @@ -228,3 +228,20 @@ it("empty file content", async () => { }) }) +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`) +}) From d24420542dd684fe12c43dce74a147f27ad4c958 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:55:45 -0700 Subject: [PATCH 3/4] Update packages/typescript-vfs/src/index.ts --- packages/typescript-vfs/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/typescript-vfs/src/index.ts b/packages/typescript-vfs/src/index.ts index 552781e9a7c0..c1e33761fb4d 100755 --- a/packages/typescript-vfs/src/index.ts +++ b/packages/typescript-vfs/src/index.ts @@ -562,7 +562,7 @@ export function createVirtualCompilerHost(sys: System, compilerOptions: Compiler ts.createSourceFile( fileName, sys.readFile(fileName)!, - languageVersionOrOptions, + languageVersionOrOptions ?? compilerOptions.target ?? defaultCompilerOptions(ts).target!, false ) ) From ade3b6466f13a49f9eb782a7e085c8d3ed0e84d2 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 5 Jun 2024 09:57:25 -0700 Subject: [PATCH 4/4] Add changeset --- .changeset/breezy-gifts-fly.md | 5 +++++ .changeset/gold-pears-flow.md | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 .changeset/breezy-gifts-fly.md create mode 100644 .changeset/gold-pears-flow.md 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