forked from TheLartians/TypeScript2Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
21 lines (17 loc) 路 662 Bytes
/
Copy pathutils.ts
File metadata and controls
21 lines (17 loc) 路 662 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { typeScriptToPython } from "../typeScriptToPython";
import { createProject, ts } from "@ts-morph/bootstrap";
export const transpileString = async (code: string) => {
const project = await createProject();
const fileName = "test.ts";
const sourceFile = project.createSourceFile(fileName, code);
const program = project.createProgram();
const diagnostics = ts.getPreEmitDiagnostics(program);
if (diagnostics.length > 0) {
throw new Error(
`code compiled with errors: ${project.formatDiagnosticsWithColorAndContext(
diagnostics,
)}`,
);
}
return typeScriptToPython(program.getTypeChecker(), [sourceFile]);
};