(input: Record<string, string>, options: CompilerOptions = {})
| 43 | |
| 44 | /** @internal */ |
| 45 | export function createVirtualProgram(input: Record<string, string>, options: CompilerOptions = {}): ts.Program { |
| 46 | const normalizedFiles: Record<string, string> = {}; |
| 47 | for (const [path, file] of Object.entries(input)) { |
| 48 | normalizedFiles[normalizeSlashes(path)] = file; |
| 49 | } |
| 50 | const compilerHost: ts.CompilerHost = { |
| 51 | fileExists: fileName => fileName in normalizedFiles || ts.sys.fileExists(fileName), |
| 52 | getCanonicalFileName: fileName => fileName, |
| 53 | getCurrentDirectory: () => "", |
| 54 | getDefaultLibFileName: ts.getDefaultLibFileName, |
| 55 | readFile: () => "", |
| 56 | getNewLine: () => "\n", |
| 57 | useCaseSensitiveFileNames: () => false, |
| 58 | writeFile() {}, |
| 59 | |
| 60 | getSourceFile(fileName) { |
| 61 | if (fileName in normalizedFiles) { |
| 62 | return ts.createSourceFile(fileName, normalizedFiles[fileName], ts.ScriptTarget.Latest, false); |
| 63 | } |
| 64 | |
| 65 | let filePath: string | undefined; |
| 66 | |
| 67 | if (fileName.startsWith("lib.")) { |
| 68 | const typeScriptDir = path.dirname(require.resolve("typescript")); |
| 69 | filePath = path.join(typeScriptDir, fileName); |
| 70 | } |
| 71 | |
| 72 | if (fileName.includes("language-extensions")) { |
| 73 | const dtsName = fileName.replace(/(\.d)?(\.ts)$/, ".d.ts"); |
| 74 | filePath = path.resolve(dtsName); |
| 75 | } |
| 76 | |
| 77 | if (filePath !== undefined) { |
| 78 | if (libCache[fileName]) return libCache[fileName]; |
| 79 | const content = fs.readFileSync(filePath, "utf8"); |
| 80 | libCache[fileName] = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, false); |
| 81 | return libCache[fileName]; |
| 82 | } |
| 83 | }, |
| 84 | }; |
| 85 | |
| 86 | return ts.createProgram(Object.keys(normalizedFiles), options, compilerHost); |
| 87 | } |
| 88 | |
| 89 | export interface TranspileVirtualProjectResult { |
| 90 | diagnostics: ts.Diagnostic[]; |
no test coverage detected