-
Notifications
You must be signed in to change notification settings - Fork 91
/
ts2c.ts
22 lines (20 loc) · 1018 Bytes
/
ts2c.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import * as ts from 'typescript';
import { CProgram } from './src/program';
export function transpile(source: string): string {
var sourceFile = ts.createSourceFile('source.ts', source, ts.ScriptTarget.ES5, true);
var compilerHost: ts.CompilerHost = {
getSourceFile: (fileName, target) => 'source.ts' ? sourceFile : null,
writeFile: (name, text, writeByteOrderMark) => { },
getDefaultLibFileName: () => { return "lib.d.ts"; },
useCaseSensitiveFileNames: () => { return false; },
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getDirectories: () => [],
getNewLine: () => "\n",
fileExists: fileName => fileName == 'source.ts',
readFile: fileName => fileName == 'source.ts' ? source : null,
directoryExists: dirName => dirName == "",
};
var program = ts.createProgram(['source.ts'], { noLib: true }, compilerHost);
return new CProgram(program)["resolve"]();
};