-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathconsole.ts
More file actions
66 lines (63 loc) · 3.32 KB
/
console.ts
File metadata and controls
66 lines (63 loc) · 3.32 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
import * as ts from "typescript";
import * as lua from "../../LuaAST";
import { TransformationContext } from "../context";
import { unsupportedProperty } from "../utils/diagnostics";
import { transformArguments } from "../visitors/call";
const isStringFormatTemplate = (node: ts.Expression) => ts.isStringLiteral(node) && node.text.includes("%");
export function transformConsoleCall(
context: TransformationContext,
node: ts.CallExpression,
calledMethod: ts.PropertyAccessExpression
): lua.Expression | undefined {
const methodName = calledMethod.name.text;
const signature = context.checker.getResolvedSignature(node);
const parameters = transformArguments(context, node.arguments, signature);
switch (methodName) {
case "error":
case "info":
case "log":
case "warn":
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) {
// print(string.format([arguments]))
const stringFormatCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")),
parameters
);
return lua.createCallExpression(lua.createIdentifier("print"), [stringFormatCall]);
}
// print([arguments])
return lua.createCallExpression(lua.createIdentifier("print"), parameters);
case "assert":
if (node.arguments.length > 1 && isStringFormatTemplate(node.arguments[1])) {
// assert([condition], string.format([arguments]))
const stringFormatCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")),
parameters.slice(1)
);
return lua.createCallExpression(lua.createIdentifier("assert"), [parameters[0], stringFormatCall]);
}
// assert()
return lua.createCallExpression(lua.createIdentifier("assert"), parameters);
case "trace":
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) {
// print(debug.traceback(string.format([arguments])))
const stringFormatCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")),
parameters
);
const debugTracebackCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("traceback")),
[stringFormatCall]
);
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
}
// print(debug.traceback([arguments])))
const debugTracebackCall = lua.createCallExpression(
lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("traceback")),
parameters
);
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
default:
context.diagnostics.push(unsupportedProperty(calledMethod.name, "console", methodName));
}
}