-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypecheck.ts
More file actions
177 lines (171 loc) · 10.1 KB
/
typecheck.ts
File metadata and controls
177 lines (171 loc) · 10.1 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as ts from "typescript";
import * as tstl from "@jackmacwindows/typescript-to-lua";
const EXPECT_PLUGIN = "system.expect";
const EXPECT_METHOD = "expect";
const EXPECT_FIELD_METHOD = "field";
const EXPECT_FUNCTION = "___TS_Expect";
const EXPECT_FIELD_FUNCTION = "___TS_Expect_Field";
function fillTypeList(obj: ts.Expression, args: ts.Expression[], fields: ts.Statement[], type: ts.TypeNode, context: tstl.TransformationContext, inUnion: boolean, source?: ts.SourceFile): boolean {
if (ts.isUnionTypeNode(type)) {
for (let t of type.types) if (!fillTypeList(obj, args, fields, t, context, true, source)) return false;
} else if (ts.isFunctionTypeNode(type)) {
args.push(ts.factory.createStringLiteral("function"));
} else if (ts.isConstructorTypeNode(type)) {
args.push(ts.factory.createStringLiteral("table"));
} else if (ts.isArrayTypeNode(type)) {
args.push(ts.factory.createStringLiteral("table"));
} else if (ts.isMappedTypeNode(type)) {
args.push(ts.factory.createStringLiteral("table"));
} else if (ts.isTypeLiteralNode(type)) {
const table = ts.factory.createStringLiteral("table");
args.push(table);
let newFields: ts.Statement[] = [];
for (let field of type.members) {
if (field.name !== undefined) {
if (ts.isPropertySignature(field) && field.type !== undefined && (ts.isIdentifier(field.name) || ts.isStringLiteral(field.name))) {
let args2: ts.Expression[] = [
obj,
ts.factory.createStringLiteral(field.name.text)
];
if (field.questionToken) args2.push(ts.factory.createStringLiteral("nil"));
if (fillTypeList(ts.factory.createPropertyAccessExpression(obj, field.name.text), args2, fields, field.type, context, inUnion, source)) {
context.program["__usesExpect"] = true;
newFields.push(ts.factory.createExpressionStatement(ts.factory.createCallExpression(
ts.factory.createIdentifier(EXPECT_FIELD_FUNCTION),
[
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createRestTypeNode(ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)))
], args2)));
}
} else if (ts.isMethodSignature(field) && (ts.isIdentifier(field.name) || ts.isStringLiteral(field.name))) {
let args2: ts.Expression[] = [
obj,
ts.factory.createStringLiteral(field.name.text),
ts.factory.createStringLiteral("function")
];
if (field.questionToken) args2.push(ts.factory.createStringLiteral("nil"));
context.program["__usesExpect"] = true;
newFields.push(ts.factory.createExpressionStatement(ts.factory.createCallExpression(
ts.factory.createIdentifier(EXPECT_FIELD_FUNCTION),
[
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
ts.factory.createRestTypeNode(ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)))
], args2)));
}
}
}
if (inUnion) fields.push(ts.factory.createIfStatement(ts.factory.createEquality(ts.factory.createTypeOfExpression(obj), table), ts.factory.createBlock(newFields)));
else fields.unshift(...newFields);
} else if (ts.isLiteralTypeNode(type)) {
if (type.literal.kind === ts.SyntaxKind.NullKeyword) args.push(ts.factory.createStringLiteral("nil"));
else if (type.literal.kind === ts.SyntaxKind.FalseKeyword || type.literal.kind === ts.SyntaxKind.TrueKeyword) args.push(ts.factory.createStringLiteral("boolean"));
else return false;
} else if (ts.isParenthesizedTypeNode(type)) {
return fillTypeList(obj, args, fields, type.type, context, inUnion, source);
} else if (ts.isOptionalTypeNode(type)) {
if (!fillTypeList(obj, args, fields, type.type, context, true, source)) return false;
args.push(ts.factory.createStringLiteral("nil"));
} else if (type.kind === ts.SyntaxKind.NullKeyword || type.kind == ts.SyntaxKind.UndefinedKeyword) {
args.push(ts.factory.createStringLiteral("nil"));
} else if (type.kind === ts.SyntaxKind.AnyKeyword) {
// do nothing
} else if (type.kind === ts.SyntaxKind.BooleanKeyword) {
args.push(ts.factory.createStringLiteral("boolean"));
} else if (type.kind === ts.SyntaxKind.NumberKeyword) {
args.push(ts.factory.createStringLiteral("number"));
} else if (type.kind === ts.SyntaxKind.StringKeyword) {
args.push(ts.factory.createStringLiteral("string"));
} else if (type.kind === ts.SyntaxKind.FunctionKeyword) {
args.push(ts.factory.createStringLiteral("function"));
} else if (type.kind === ts.SyntaxKind.ObjectKeyword) {
args.push(ts.factory.createStringLiteral("table"));
} else if (ts.isTypeReferenceNode(type)) {
const t = context.checker.getTypeFromTypeNode(type);
if (t.flags & ts.TypeFlags.EnumLike) {
args.push(ts.factory.createStringLiteral("number"));
} else if (t.isClass()) {
const t2 = context.checker.typeToTypeNode(t, type, ts.NodeBuilderFlags.InTypeAlias);
if (t2 && !ts.isTypeReferenceNode(t2)) return fillTypeList(obj, args, fields, t2, context, inUnion, (t.aliasSymbol?.declarations ?? [])[0]?.getSourceFile() ?? type.getSourceFile());
args.push(ts.factory.createStringLiteral(type.typeName.getText(source)));
} else {
args.push(ts.factory.createStringLiteral("table"));
}
} else {
context.diagnostics.push({
category: ts.DiagnosticCategory.Warning,
code: 0,
file: type.getSourceFile() ?? source,
start: type.pos,
length: type.end - type.pos,
messageText: `Could not construct type name for parameter (${type.kind}); no type check will be emitted.`
})
return false;
}
return true;
}
function addTypeChecks(m: ts.MethodDeclaration | ts.FunctionDeclaration, context: tstl.TransformationContext) {
if (m["jsDoc"]) {
let jsDoc = m["jsDoc"] as ts.JSDoc[];
if (jsDoc.length > 0 && jsDoc[0].tags?.find(v => v.tagName.escapedText === "typecheck")) {
let add: ts.Statement[] = [];
for (let a in m.parameters) {
let arg = m.parameters[a];
if (arg.type && !ts.isThisTypeNode(arg.type)) {
let obj = ts.factory.createIdentifier((context.transformNode(arg.name)[0] as tstl.Identifier).text);
let args: ts.Expression[] = [
ts.factory.createNumericLiteral(parseInt(a) + 1),
obj
];
let fields: ts.Statement[] = [];
if (fillTypeList(obj, args, fields, arg.type, context, false)) {
context.program["__usesExpect"] = true;
add.push(ts.factory.createExpressionStatement(ts.factory.createCallExpression(
ts.factory.createIdentifier(EXPECT_FUNCTION),
[
ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
ts.factory.createRestTypeNode(ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)))
], args)));
for (let expr of fields) add.push(expr);
}
}
}
m.body?.statements["unshift"](...add);
}
}
}
class TypeCheckPlugin implements tstl.Plugin {
public visitors = {
[ts.SyntaxKind.FunctionDeclaration]: (node: ts.FunctionDeclaration, context: tstl.TransformationContext): tstl.Statement[] => {
addTypeChecks(node, context);
return context.superTransformStatements(node);
},
[ts.SyntaxKind.ClassDeclaration]: (node: ts.ClassDeclaration, context: tstl.TransformationContext): tstl.Statement[] => {
for (let m of node.members) {
if (ts.isMethodDeclaration(m)) {
addTypeChecks(m, context);
}
}
let stat = context.superTransformStatements(node);
for (const idx in stat) {
const s = stat[idx]
if (tstl.isAssignmentStatement(s) && tstl.isStringLiteral(s.right[0]) && tstl.isTableIndexExpression(s.left[0]) && tstl.isStringLiteral(s.left[0].index) && s.left[0].index.value == "name") {
stat.splice(parseInt(idx), 0, tstl.createAssignmentStatement(tstl.createTableIndexExpression(tstl.createTableIndexExpression(s.left[0].table, tstl.createStringLiteral("prototype")), tstl.createStringLiteral("__name")), s.right[0]));
break;
}
}
return stat;
}
}
public beforeEmit(program: ts.Program, options: tstl.CompilerOptions, emitHost: tstl.EmitHost, result: tstl.EmitFile[]): void | ts.Diagnostic[] {
if (program["__usesExpect"]) {
for (const file of result) {
file.code = `local ___TS_Expect_Temp = require('${EXPECT_PLUGIN}')\nlocal function ${EXPECT_FUNCTION}(this, ...) return ___TS_Expect_Temp.${EXPECT_METHOD}(...) end\nlocal function ${EXPECT_FIELD_FUNCTION}(this, ...) return ___TS_Expect_Temp.${EXPECT_FIELD_METHOD}(...) end\n` + file.code;
}
}
}
}
const plugin = new TypeCheckPlugin();
export default plugin;