Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix two issues in the typescript/vfs module. #3038

Merged
merged 5 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions 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 Expand Up @@ -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,
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
false
)
)
Expand Down
31 changes: 31 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,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`)
})
Loading