forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
42 lines (36 loc) · 1.71 KB
/
Copy pathtypes.ts
File metadata and controls
42 lines (36 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as ts from "typescript";
import * as tstl from "../../../src";
import { visitAndReplace } from "./utils";
export const program = (program: ts.Program, options: { value: any }): ts.TransformerFactory<ts.SourceFile> =>
checker(program.getTypeChecker(), options);
export const config = ({ value }: { value: any }): ts.TransformerFactory<ts.SourceFile> => context => file =>
visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node) || node.expression) return;
return ts.updateReturn(node, ts.createLiteral(value));
});
export const checker = (
checker: ts.TypeChecker,
{ value }: { value: any }
): ts.TransformerFactory<ts.SourceFile> => context => file =>
visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node) || !node.expression) return;
const type = checker.getTypeAtLocation(node.expression);
if ((type.flags & ts.TypeFlags.BooleanLiteral) === 0) return;
return ts.updateReturn(node, ts.createLiteral(value));
});
export const raw: ts.TransformerFactory<ts.SourceFile> = context => file =>
visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node) || node.expression) return;
return ts.updateReturn(node, ts.createLiteral(true));
});
export const compilerOptions = ({
luaTarget,
}: tstl.CompilerOptions): ts.TransformerFactory<ts.SourceFile> => context => file => {
if (luaTarget !== tstl.LuaTarget.LuaJIT) {
throw new Error("Transformer supports only LuaJIT target");
}
return visitAndReplace(context, file, node => {
if (!ts.isReturnStatement(node) || node.expression) return;
return ts.updateReturn(node, ts.createLiteral(true));
});
};