MCPcopy Index your code
hub / github.com/TypeScriptToLua/TypeScriptToLua / createVirtualProgram

Function createVirtualProgram

src/transpilation/index.ts:45–87  ·  view source on GitHub ↗
(input: Record<string, string>, options: CompilerOptions = {})

Source from the content-addressed store, hash-verified

43
44/** @internal */
45export 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
89export interface TranspileVirtualProjectResult {
90 diagnostics: ts.Diagnostic[];

Callers 1

transpileVirtualProjectFunction · 0.85

Calls 4

normalizeSlashesFunction · 0.90
fileExistsMethod · 0.80
entriesMethod · 0.65
keysMethod · 0.65

Tested by

no test coverage detected