typescript-to-lua 1.4.4 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CompilerOptions.d.ts +1 -0
- package/dist/cli/parse.js +5 -0
- package/dist/lualib/Await.lua +21 -34
- package/dist/lualib/lualib_bundle.lua +21 -34
- package/dist/transformation/builtins/array.d.ts +2 -3
- package/dist/transformation/builtins/array.js +8 -10
- package/dist/transformation/builtins/console.d.ts +2 -2
- package/dist/transformation/builtins/console.js +8 -9
- package/dist/transformation/builtins/function.d.ts +1 -2
- package/dist/transformation/builtins/function.js +5 -6
- package/dist/transformation/builtins/index.js +22 -23
- package/dist/transformation/builtins/math.d.ts +1 -2
- package/dist/transformation/builtins/math.js +3 -4
- package/dist/transformation/builtins/number.d.ts +3 -3
- package/dist/transformation/builtins/number.js +10 -12
- package/dist/transformation/builtins/object.d.ts +2 -3
- package/dist/transformation/builtins/object.js +16 -17
- package/dist/transformation/builtins/promise.d.ts +1 -2
- package/dist/transformation/builtins/promise.js +5 -6
- package/dist/transformation/builtins/string.d.ts +2 -3
- package/dist/transformation/builtins/string.js +7 -9
- package/dist/transformation/builtins/symbol.d.ts +2 -2
- package/dist/transformation/builtins/symbol.js +6 -7
- package/dist/transformation/utils/lua-ast.js +83 -1
- package/dist/transformation/visitors/async-await.d.ts +1 -1
- package/dist/transformation/visitors/async-await.js +7 -14
- package/dist/transformation/visitors/call.d.ts +1 -3
- package/dist/transformation/visitors/call.js +23 -14
- package/dist/transformation/visitors/errors.js +52 -11
- package/dist/transformation/visitors/function.js +1 -1
- package/dist/transformation/visitors/return.js +5 -4
- package/package.json +1 -1
package/dist/cli/parse.js
CHANGED
|
@@ -39,6 +39,11 @@ exports.optionDeclarations = [
|
|
|
39
39
|
type: "enum",
|
|
40
40
|
choices: Object.values(CompilerOptions_1.LuaTarget),
|
|
41
41
|
},
|
|
42
|
+
{
|
|
43
|
+
name: "noImplicitGlobalVariables",
|
|
44
|
+
description: 'Specify to prevent implicitly turning "normal" variants into global variables in the transpiled output.',
|
|
45
|
+
type: "boolean",
|
|
46
|
+
},
|
|
42
47
|
{
|
|
43
48
|
name: "noImplicitSelf",
|
|
44
49
|
description: 'If "this" is implicitly considered an any type, do not generate a self parameter.',
|
package/dist/lualib/Await.lua
CHANGED
|
@@ -2,7 +2,7 @@ local function __TS__AsyncAwaiter(generator)
|
|
|
2
2
|
return __TS__New(
|
|
3
3
|
__TS__Promise,
|
|
4
4
|
function(____, resolve, reject)
|
|
5
|
-
local adopt, fulfilled,
|
|
5
|
+
local adopt, fulfilled, step, resolved, asyncCoroutine
|
|
6
6
|
function adopt(self, value)
|
|
7
7
|
local ____temp_0
|
|
8
8
|
if __TS__InstanceOf(value, __TS__Promise) then
|
|
@@ -13,55 +13,42 @@ local function __TS__AsyncAwaiter(generator)
|
|
|
13
13
|
return ____temp_0
|
|
14
14
|
end
|
|
15
15
|
function fulfilled(self, value)
|
|
16
|
-
local success,
|
|
16
|
+
local success, resultOrError = coroutine.resume(asyncCoroutine, value)
|
|
17
17
|
if success then
|
|
18
|
-
step(nil, resultOrError
|
|
18
|
+
step(nil, resultOrError)
|
|
19
19
|
else
|
|
20
|
-
reject(nil,
|
|
20
|
+
reject(nil, resultOrError)
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
|
-
function
|
|
24
|
-
if
|
|
25
|
-
return
|
|
26
|
-
local success, hasReturnedOrError, returnedValue = pcall(handler, value)
|
|
27
|
-
if success then
|
|
28
|
-
if hasReturnedOrError then
|
|
29
|
-
resolve(nil, returnedValue)
|
|
30
|
-
else
|
|
31
|
-
step(nil, hasReturnedOrError, handler)
|
|
32
|
-
end
|
|
33
|
-
else
|
|
34
|
-
reject(nil, hasReturnedOrError)
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
else
|
|
38
|
-
return function(____, value)
|
|
39
|
-
reject(nil, value)
|
|
40
|
-
end
|
|
23
|
+
function step(self, result)
|
|
24
|
+
if resolved then
|
|
25
|
+
return
|
|
41
26
|
end
|
|
42
|
-
end
|
|
43
|
-
function step(self, result, errorHandler)
|
|
44
27
|
if coroutine.status(asyncCoroutine) == "dead" then
|
|
45
28
|
resolve(nil, result)
|
|
46
29
|
else
|
|
47
30
|
local ____self_1 = adopt(nil, result)
|
|
48
|
-
____self_1["then"](
|
|
49
|
-
____self_1,
|
|
50
|
-
fulfilled,
|
|
51
|
-
rejected(nil, errorHandler)
|
|
52
|
-
)
|
|
31
|
+
____self_1["then"](____self_1, fulfilled, reject)
|
|
53
32
|
end
|
|
54
33
|
end
|
|
34
|
+
resolved = false
|
|
55
35
|
asyncCoroutine = coroutine.create(generator)
|
|
56
|
-
local success,
|
|
36
|
+
local success, resultOrError = coroutine.resume(
|
|
37
|
+
asyncCoroutine,
|
|
38
|
+
function(____, v)
|
|
39
|
+
resolved = true
|
|
40
|
+
local ____self_2 = adopt(nil, v)
|
|
41
|
+
____self_2["then"](____self_2, resolve, reject)
|
|
42
|
+
end
|
|
43
|
+
)
|
|
57
44
|
if success then
|
|
58
|
-
step(nil, resultOrError
|
|
45
|
+
step(nil, resultOrError)
|
|
59
46
|
else
|
|
60
|
-
reject(nil,
|
|
47
|
+
reject(nil, resultOrError)
|
|
61
48
|
end
|
|
62
49
|
end
|
|
63
50
|
)
|
|
64
51
|
end
|
|
65
|
-
local function __TS__Await(
|
|
66
|
-
return coroutine.yield(
|
|
52
|
+
local function __TS__Await(thing)
|
|
53
|
+
return coroutine.yield(thing)
|
|
67
54
|
end
|
|
@@ -753,7 +753,7 @@ local function __TS__AsyncAwaiter(generator)
|
|
|
753
753
|
return __TS__New(
|
|
754
754
|
__TS__Promise,
|
|
755
755
|
function(____, resolve, reject)
|
|
756
|
-
local adopt, fulfilled,
|
|
756
|
+
local adopt, fulfilled, step, resolved, asyncCoroutine
|
|
757
757
|
function adopt(self, value)
|
|
758
758
|
local ____temp_0
|
|
759
759
|
if __TS__InstanceOf(value, __TS__Promise) then
|
|
@@ -764,57 +764,44 @@ local function __TS__AsyncAwaiter(generator)
|
|
|
764
764
|
return ____temp_0
|
|
765
765
|
end
|
|
766
766
|
function fulfilled(self, value)
|
|
767
|
-
local success,
|
|
767
|
+
local success, resultOrError = coroutine.resume(asyncCoroutine, value)
|
|
768
768
|
if success then
|
|
769
|
-
step(nil, resultOrError
|
|
769
|
+
step(nil, resultOrError)
|
|
770
770
|
else
|
|
771
|
-
reject(nil,
|
|
771
|
+
reject(nil, resultOrError)
|
|
772
772
|
end
|
|
773
773
|
end
|
|
774
|
-
function
|
|
775
|
-
if
|
|
776
|
-
return
|
|
777
|
-
local success, hasReturnedOrError, returnedValue = pcall(handler, value)
|
|
778
|
-
if success then
|
|
779
|
-
if hasReturnedOrError then
|
|
780
|
-
resolve(nil, returnedValue)
|
|
781
|
-
else
|
|
782
|
-
step(nil, hasReturnedOrError, handler)
|
|
783
|
-
end
|
|
784
|
-
else
|
|
785
|
-
reject(nil, hasReturnedOrError)
|
|
786
|
-
end
|
|
787
|
-
end
|
|
788
|
-
else
|
|
789
|
-
return function(____, value)
|
|
790
|
-
reject(nil, value)
|
|
791
|
-
end
|
|
774
|
+
function step(self, result)
|
|
775
|
+
if resolved then
|
|
776
|
+
return
|
|
792
777
|
end
|
|
793
|
-
end
|
|
794
|
-
function step(self, result, errorHandler)
|
|
795
778
|
if coroutine.status(asyncCoroutine) == "dead" then
|
|
796
779
|
resolve(nil, result)
|
|
797
780
|
else
|
|
798
781
|
local ____self_1 = adopt(nil, result)
|
|
799
|
-
____self_1["then"](
|
|
800
|
-
____self_1,
|
|
801
|
-
fulfilled,
|
|
802
|
-
rejected(nil, errorHandler)
|
|
803
|
-
)
|
|
782
|
+
____self_1["then"](____self_1, fulfilled, reject)
|
|
804
783
|
end
|
|
805
784
|
end
|
|
785
|
+
resolved = false
|
|
806
786
|
asyncCoroutine = coroutine.create(generator)
|
|
807
|
-
local success,
|
|
787
|
+
local success, resultOrError = coroutine.resume(
|
|
788
|
+
asyncCoroutine,
|
|
789
|
+
function(____, v)
|
|
790
|
+
resolved = true
|
|
791
|
+
local ____self_2 = adopt(nil, v)
|
|
792
|
+
____self_2["then"](____self_2, resolve, reject)
|
|
793
|
+
end
|
|
794
|
+
)
|
|
808
795
|
if success then
|
|
809
|
-
step(nil, resultOrError
|
|
796
|
+
step(nil, resultOrError)
|
|
810
797
|
else
|
|
811
|
-
reject(nil,
|
|
798
|
+
reject(nil, resultOrError)
|
|
812
799
|
end
|
|
813
800
|
end
|
|
814
801
|
)
|
|
815
802
|
end
|
|
816
|
-
local function __TS__Await(
|
|
817
|
-
return coroutine.yield(
|
|
803
|
+
local function __TS__Await(thing)
|
|
804
|
+
return coroutine.yield(thing)
|
|
818
805
|
end
|
|
819
806
|
|
|
820
807
|
local function __TS__ClassExtends(target, base)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function transformArrayPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined;
|
|
4
|
+
export declare function transformArrayConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
5
|
+
export declare function transformArrayPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
7
6
|
export declare function transformArrayProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.UnaryExpression | undefined;
|
|
@@ -9,11 +9,10 @@ const call_1 = require("../visitors/call");
|
|
|
9
9
|
const typescript_1 = require("../utils/typescript");
|
|
10
10
|
const expression_list_1 = require("../visitors/expression-list");
|
|
11
11
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
12
|
-
function transformArrayConstructorCall(context, node) {
|
|
13
|
-
const expression = node.expression;
|
|
12
|
+
function transformArrayConstructorCall(context, node, calledMethod) {
|
|
14
13
|
const signature = context.checker.getResolvedSignature(node);
|
|
15
14
|
const params = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
16
|
-
const expressionName =
|
|
15
|
+
const expressionName = calledMethod.name.text;
|
|
17
16
|
switch (expressionName) {
|
|
18
17
|
case "from":
|
|
19
18
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayFrom, node, ...params);
|
|
@@ -22,7 +21,7 @@ function transformArrayConstructorCall(context, node) {
|
|
|
22
21
|
case "of":
|
|
23
22
|
return (0, lua_ast_1.wrapInTable)(...params);
|
|
24
23
|
default:
|
|
25
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
24
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Array", expressionName));
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
exports.transformArrayConstructorCall = transformArrayConstructorCall;
|
|
@@ -45,11 +44,10 @@ function transformSingleElementArrayPush(context, node, caller, param) {
|
|
|
45
44
|
context.addPrecedingStatements(pushStatement);
|
|
46
45
|
return expressionIsUsed ? lengthExpression : lua.createNilLiteral();
|
|
47
46
|
}
|
|
48
|
-
function transformArrayPrototypeCall(context, node) {
|
|
49
|
-
const expression = node.expression;
|
|
47
|
+
function transformArrayPrototypeCall(context, node, calledMethod) {
|
|
50
48
|
const signature = context.checker.getResolvedSignature(node);
|
|
51
|
-
const [caller, params] = (0, call_1.transformCallAndArguments)(context,
|
|
52
|
-
const expressionName =
|
|
49
|
+
const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature);
|
|
50
|
+
const expressionName = calledMethod.name.text;
|
|
53
51
|
switch (expressionName) {
|
|
54
52
|
case "concat":
|
|
55
53
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayConcat, node, caller, ...params);
|
|
@@ -103,7 +101,7 @@ function transformArrayPrototypeCall(context, node) {
|
|
|
103
101
|
case "splice":
|
|
104
102
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArraySplice, node, caller, ...params);
|
|
105
103
|
case "join":
|
|
106
|
-
const callerType = context.checker.getTypeAtLocation(
|
|
104
|
+
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
|
|
107
105
|
const elementType = context.checker.getElementTypeOfArrayType(callerType);
|
|
108
106
|
if (elementType && ((0, typescript_1.isStringType)(context, elementType) || (0, typescript_1.isNumberType)(context, elementType))) {
|
|
109
107
|
const defaultSeparatorLiteral = lua.createStringLiteral(",");
|
|
@@ -124,7 +122,7 @@ function transformArrayPrototypeCall(context, node) {
|
|
|
124
122
|
case "flatMap":
|
|
125
123
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ArrayFlatMap, node, caller, ...params);
|
|
126
124
|
default:
|
|
127
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
125
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "array", expressionName));
|
|
128
126
|
}
|
|
129
127
|
}
|
|
130
128
|
exports.transformArrayPrototypeCall = transformArrayPrototypeCall;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
1
2
|
import * as lua from "../../LuaAST";
|
|
2
3
|
import { TransformationContext } from "../context";
|
|
3
|
-
|
|
4
|
-
export declare function transformConsoleCall(context: TransformationContext, expression: PropertyCallExpression): lua.Expression | undefined;
|
|
4
|
+
export declare function transformConsoleCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
@@ -6,17 +6,16 @@ const lua = require("../../LuaAST");
|
|
|
6
6
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
7
7
|
const call_1 = require("../visitors/call");
|
|
8
8
|
const isStringFormatTemplate = (node) => ts.isStringLiteral(node) && node.text.includes("%");
|
|
9
|
-
function transformConsoleCall(context,
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const parameters = (0, call_1.transformArguments)(context, expression.arguments, signature);
|
|
9
|
+
function transformConsoleCall(context, node, calledMethod) {
|
|
10
|
+
const methodName = calledMethod.name.text;
|
|
11
|
+
const signature = context.checker.getResolvedSignature(node);
|
|
12
|
+
const parameters = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
14
13
|
switch (methodName) {
|
|
15
14
|
case "error":
|
|
16
15
|
case "info":
|
|
17
16
|
case "log":
|
|
18
17
|
case "warn":
|
|
19
|
-
if (
|
|
18
|
+
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) {
|
|
20
19
|
// print(string.format([arguments]))
|
|
21
20
|
const stringFormatCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")), parameters);
|
|
22
21
|
return lua.createCallExpression(lua.createIdentifier("print"), [stringFormatCall]);
|
|
@@ -24,7 +23,7 @@ function transformConsoleCall(context, expression) {
|
|
|
24
23
|
// print([arguments])
|
|
25
24
|
return lua.createCallExpression(lua.createIdentifier("print"), parameters);
|
|
26
25
|
case "assert":
|
|
27
|
-
if (
|
|
26
|
+
if (node.arguments.length > 1 && isStringFormatTemplate(node.arguments[1])) {
|
|
28
27
|
// assert([condition], string.format([arguments]))
|
|
29
28
|
const stringFormatCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")), parameters.slice(1));
|
|
30
29
|
return lua.createCallExpression(lua.createIdentifier("assert"), [parameters[0], stringFormatCall]);
|
|
@@ -32,7 +31,7 @@ function transformConsoleCall(context, expression) {
|
|
|
32
31
|
// assert()
|
|
33
32
|
return lua.createCallExpression(lua.createIdentifier("assert"), parameters);
|
|
34
33
|
case "trace":
|
|
35
|
-
if (
|
|
34
|
+
if (node.arguments.length > 0 && isStringFormatTemplate(node.arguments[0])) {
|
|
36
35
|
// print(debug.traceback(string.format([arguments])))
|
|
37
36
|
const stringFormatCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("format")), parameters);
|
|
38
37
|
const debugTracebackCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("traceback")), [stringFormatCall]);
|
|
@@ -42,7 +41,7 @@ function transformConsoleCall(context, expression) {
|
|
|
42
41
|
const debugTracebackCall = lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("debug"), lua.createStringLiteral("traceback")), parameters);
|
|
43
42
|
return lua.createCallExpression(lua.createIdentifier("print"), [debugTracebackCall]);
|
|
44
43
|
default:
|
|
45
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
44
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "console", methodName));
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
47
|
exports.transformConsoleCall = transformConsoleCall;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
|
|
5
|
-
export declare function transformFunctionPrototypeCall(context: TransformationContext, node: PropertyCallExpression): lua.CallExpression | undefined;
|
|
4
|
+
export declare function transformFunctionPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.CallExpression | undefined;
|
|
6
5
|
export declare function transformFunctionProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
@@ -8,15 +8,14 @@ const function_context_1 = require("../utils/function-context");
|
|
|
8
8
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
9
9
|
const lualib_1 = require("../utils/lualib");
|
|
10
10
|
const call_1 = require("../visitors/call");
|
|
11
|
-
function transformFunctionPrototypeCall(context, node) {
|
|
12
|
-
const
|
|
13
|
-
const callerType = context.checker.getTypeAtLocation(expression.expression);
|
|
11
|
+
function transformFunctionPrototypeCall(context, node, calledMethod) {
|
|
12
|
+
const callerType = context.checker.getTypeAtLocation(calledMethod.expression);
|
|
14
13
|
if ((0, function_context_1.getFunctionContextType)(context, callerType) === function_context_1.ContextType.Void) {
|
|
15
14
|
context.diagnostics.push((0, diagnostics_1.unsupportedSelfFunctionConversion)(node));
|
|
16
15
|
}
|
|
17
16
|
const signature = context.checker.getResolvedSignature(node);
|
|
18
|
-
const [caller, params] = (0, call_1.transformCallAndArguments)(context,
|
|
19
|
-
const expressionName =
|
|
17
|
+
const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature);
|
|
18
|
+
const expressionName = calledMethod.name.text;
|
|
20
19
|
switch (expressionName) {
|
|
21
20
|
case "apply":
|
|
22
21
|
const nonContextArgs = params.length > 1 ? [(0, lua_ast_1.createUnpackCall)(context, params[1], node.arguments[1])] : [];
|
|
@@ -26,7 +25,7 @@ function transformFunctionPrototypeCall(context, node) {
|
|
|
26
25
|
case "call":
|
|
27
26
|
return lua.createCallExpression(caller, params, node);
|
|
28
27
|
case "toString":
|
|
29
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
28
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "function", expressionName));
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
exports.transformFunctionPrototypeCall = transformFunctionPrototypeCall;
|
|
@@ -3,11 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.checkForLuaLibType = exports.transformBuiltinIdentifierExpression = exports.transformBuiltinCallExpression = exports.transformBuiltinPropertyAccessExpression = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
|
-
const utils_1 = require("../../utils");
|
|
7
6
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
8
7
|
const lualib_1 = require("../utils/lualib");
|
|
9
8
|
const symbols_1 = require("../utils/symbols");
|
|
10
9
|
const typescript_1 = require("../utils/typescript");
|
|
10
|
+
const call_1 = require("../visitors/call");
|
|
11
11
|
const array_1 = require("./array");
|
|
12
12
|
const console_1 = require("./console");
|
|
13
13
|
const function_1 = require("./function");
|
|
@@ -55,80 +55,79 @@ function transformBuiltinCallExpression(context, node, isOptionalCall) {
|
|
|
55
55
|
return result;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
-
const
|
|
59
|
-
if (!ts.isPropertyAccessExpression(
|
|
58
|
+
const calledMethod = ts.getOriginalNode((0, call_1.getCalledExpression)(node));
|
|
59
|
+
if (!ts.isPropertyAccessExpression(calledMethod)) {
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
|
-
const isOptionalAccess =
|
|
63
|
-
(0, utils_1.assume)(node);
|
|
62
|
+
const isOptionalAccess = calledMethod.questionDotToken;
|
|
64
63
|
// If the function being called is of type owner.func, get the type of owner
|
|
65
|
-
const ownerType = context.checker.getTypeAtLocation(
|
|
64
|
+
const ownerType = context.checker.getTypeAtLocation(calledMethod.expression);
|
|
66
65
|
if ((0, typescript_1.isStandardLibraryType)(context, ownerType, undefined)) {
|
|
67
66
|
const symbol = ownerType.getSymbol();
|
|
68
67
|
switch (symbol === null || symbol === void 0 ? void 0 : symbol.name) {
|
|
69
68
|
case "ArrayConstructor":
|
|
70
69
|
if (isOptionalCall || isOptionalAccess)
|
|
71
70
|
return unsupportedOptionalCall();
|
|
72
|
-
return (0, array_1.transformArrayConstructorCall)(context, node);
|
|
71
|
+
return (0, array_1.transformArrayConstructorCall)(context, node, calledMethod);
|
|
73
72
|
case "Console":
|
|
74
73
|
if (isOptionalCall || isOptionalAccess)
|
|
75
74
|
return unsupportedOptionalCall();
|
|
76
|
-
return (0, console_1.transformConsoleCall)(context, node);
|
|
75
|
+
return (0, console_1.transformConsoleCall)(context, node, calledMethod);
|
|
77
76
|
case "Math":
|
|
78
77
|
if (isOptionalCall || isOptionalAccess)
|
|
79
78
|
return unsupportedOptionalCall();
|
|
80
|
-
return (0, math_1.transformMathCall)(context, node);
|
|
79
|
+
return (0, math_1.transformMathCall)(context, node, calledMethod);
|
|
81
80
|
case "StringConstructor":
|
|
82
81
|
if (isOptionalCall || isOptionalAccess)
|
|
83
82
|
return unsupportedOptionalCall();
|
|
84
|
-
return (0, string_1.transformStringConstructorCall)(context, node);
|
|
83
|
+
return (0, string_1.transformStringConstructorCall)(context, node, calledMethod);
|
|
85
84
|
case "ObjectConstructor":
|
|
86
85
|
if (isOptionalCall || isOptionalAccess)
|
|
87
86
|
return unsupportedOptionalCall();
|
|
88
|
-
return (0, object_1.transformObjectConstructorCall)(context, node);
|
|
87
|
+
return (0, object_1.transformObjectConstructorCall)(context, node, calledMethod);
|
|
89
88
|
case "SymbolConstructor":
|
|
90
89
|
if (isOptionalCall || isOptionalAccess)
|
|
91
90
|
return unsupportedOptionalCall();
|
|
92
|
-
return (0, symbol_1.transformSymbolConstructorCall)(context, node);
|
|
91
|
+
return (0, symbol_1.transformSymbolConstructorCall)(context, node, calledMethod);
|
|
93
92
|
case "NumberConstructor":
|
|
94
93
|
if (isOptionalCall || isOptionalAccess)
|
|
95
94
|
return unsupportedOptionalCall();
|
|
96
|
-
return (0, number_1.transformNumberConstructorCall)(context, node);
|
|
95
|
+
return (0, number_1.transformNumberConstructorCall)(context, node, calledMethod);
|
|
97
96
|
case "PromiseConstructor":
|
|
98
97
|
if (isOptionalCall || isOptionalAccess)
|
|
99
98
|
return unsupportedOptionalCall();
|
|
100
|
-
return (0, promise_1.transformPromiseConstructorCall)(context, node);
|
|
99
|
+
return (0, promise_1.transformPromiseConstructorCall)(context, node, calledMethod);
|
|
101
100
|
}
|
|
102
101
|
}
|
|
103
102
|
const isStringFunction = (0, typescript_1.isStringType)(context, ownerType) ||
|
|
104
|
-
(
|
|
103
|
+
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isStringType));
|
|
105
104
|
if (isStringFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
106
105
|
if (isOptionalCall)
|
|
107
106
|
return unsupportedOptionalCall();
|
|
108
|
-
return (0, string_1.transformStringPrototypeCall)(context, node);
|
|
107
|
+
return (0, string_1.transformStringPrototypeCall)(context, node, calledMethod);
|
|
109
108
|
}
|
|
110
109
|
const isNumberFunction = (0, typescript_1.isNumberType)(context, ownerType) ||
|
|
111
|
-
(
|
|
110
|
+
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isNumberType));
|
|
112
111
|
if (isNumberFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
113
112
|
if (isOptionalCall)
|
|
114
113
|
return unsupportedOptionalCall();
|
|
115
|
-
return (0, number_1.transformNumberPrototypeCall)(context, node);
|
|
114
|
+
return (0, number_1.transformNumberPrototypeCall)(context, node, calledMethod);
|
|
116
115
|
}
|
|
117
116
|
const isArrayFunction = (0, typescript_1.isArrayType)(context, ownerType) ||
|
|
118
|
-
(
|
|
117
|
+
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, typescript_1.isArrayType));
|
|
119
118
|
if (isArrayFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
120
119
|
if (isOptionalCall)
|
|
121
120
|
return unsupportedOptionalCall();
|
|
122
|
-
return (0, array_1.transformArrayPrototypeCall)(context, node);
|
|
121
|
+
return (0, array_1.transformArrayPrototypeCall)(context, node, calledMethod);
|
|
123
122
|
}
|
|
124
123
|
const isFunctionFunction = (0, typescript_1.isFunctionType)(ownerType) ||
|
|
125
|
-
(
|
|
124
|
+
(calledMethod.questionDotToken && (0, typescript_1.isNullableType)(context, ownerType, (_, t) => (0, typescript_1.isFunctionType)(t)));
|
|
126
125
|
if (isFunctionFunction && (0, typescript_1.hasStandardLibrarySignature)(context, node)) {
|
|
127
126
|
if (isOptionalCall)
|
|
128
127
|
return unsupportedOptionalCall();
|
|
129
|
-
return (0, function_1.transformFunctionPrototypeCall)(context, node);
|
|
128
|
+
return (0, function_1.transformFunctionPrototypeCall)(context, node, calledMethod);
|
|
130
129
|
}
|
|
131
|
-
const objectResult = (0, object_1.transformObjectPrototypeCall)(context, node,
|
|
130
|
+
const objectResult = (0, object_1.transformObjectPrototypeCall)(context, node, calledMethod);
|
|
132
131
|
if (objectResult) {
|
|
133
132
|
if (isOptionalCall)
|
|
134
133
|
return unsupportedOptionalCall();
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
import { PropertyCallExpression } from "../visitors/call";
|
|
5
4
|
export declare function transformMathProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
6
|
-
export declare function transformMathCall(context: TransformationContext, node:
|
|
5
|
+
export declare function transformMathCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
@@ -26,12 +26,11 @@ function transformMathProperty(context, node) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
exports.transformMathProperty = transformMathProperty;
|
|
29
|
-
function transformMathCall(context, node) {
|
|
30
|
-
const expression = node.expression;
|
|
29
|
+
function transformMathCall(context, node, calledMethod) {
|
|
31
30
|
const signature = context.checker.getResolvedSignature(node);
|
|
32
31
|
const params = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
33
32
|
const math = lua.createIdentifier("math");
|
|
34
|
-
const expressionName =
|
|
33
|
+
const expressionName = calledMethod.name.text;
|
|
35
34
|
switch (expressionName) {
|
|
36
35
|
// Lua 5.3: math.atan(y, x)
|
|
37
36
|
// Otherwise: math.atan2(y, x)
|
|
@@ -90,7 +89,7 @@ function transformMathCall(context, node) {
|
|
|
90
89
|
return lua.createCallExpression(lua.createTableIndexExpression(math, method), params, node);
|
|
91
90
|
}
|
|
92
91
|
default:
|
|
93
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
92
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Math", expressionName));
|
|
94
93
|
}
|
|
95
94
|
}
|
|
96
95
|
exports.transformMathCall = transformMathCall;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import ts = require("typescript");
|
|
1
2
|
import * as lua from "../../LuaAST";
|
|
2
3
|
import { TransformationContext } from "../context";
|
|
3
|
-
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function transformNumberConstructorCall(context: TransformationContext, expression: PropertyCallExpression): lua.CallExpression | undefined;
|
|
4
|
+
export declare function transformNumberPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
5
|
+
export declare function transformNumberConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.CallExpression | undefined;
|
|
@@ -5,33 +5,31 @@ const lua = require("../../LuaAST");
|
|
|
5
5
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
6
6
|
const lualib_1 = require("../utils/lualib");
|
|
7
7
|
const call_1 = require("../visitors/call");
|
|
8
|
-
function transformNumberPrototypeCall(context, node) {
|
|
9
|
-
const expression = node.expression;
|
|
8
|
+
function transformNumberPrototypeCall(context, node, calledMethod) {
|
|
10
9
|
const signature = context.checker.getResolvedSignature(node);
|
|
11
10
|
const params = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
12
|
-
const caller = context.transformExpression(
|
|
13
|
-
const expressionName =
|
|
11
|
+
const caller = context.transformExpression(calledMethod.expression);
|
|
12
|
+
const expressionName = calledMethod.name.text;
|
|
14
13
|
switch (expressionName) {
|
|
15
14
|
case "toString":
|
|
16
15
|
return params.length === 0
|
|
17
16
|
? lua.createCallExpression(lua.createIdentifier("tostring"), [caller], node)
|
|
18
17
|
: (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberToString, node, caller, ...params);
|
|
19
18
|
default:
|
|
20
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
19
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "number", expressionName));
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
exports.transformNumberPrototypeCall = transformNumberPrototypeCall;
|
|
24
|
-
function transformNumberConstructorCall(context,
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const methodName = method.name.text;
|
|
23
|
+
function transformNumberConstructorCall(context, node, calledMethod) {
|
|
24
|
+
const parameters = (0, call_1.transformArguments)(context, node.arguments);
|
|
25
|
+
const methodName = calledMethod.name.text;
|
|
28
26
|
switch (methodName) {
|
|
29
27
|
case "isNaN":
|
|
30
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsNaN,
|
|
28
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsNaN, node, ...parameters);
|
|
31
29
|
case "isFinite":
|
|
32
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsFinite,
|
|
30
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.NumberIsFinite, node, ...parameters);
|
|
33
31
|
default:
|
|
34
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
32
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Number", methodName));
|
|
35
33
|
}
|
|
36
34
|
}
|
|
37
35
|
exports.transformNumberConstructorCall = transformNumberConstructorCall;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as lua from "../../LuaAST";
|
|
2
2
|
import * as ts from "typescript";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function transformObjectPrototypeCall(context: TransformationContext, node: ts.CallExpression, expression: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
4
|
+
export declare function transformObjectConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
5
|
+
export declare function transformObjectPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
@@ -5,41 +5,40 @@ const lua = require("../../LuaAST");
|
|
|
5
5
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
6
6
|
const lualib_1 = require("../utils/lualib");
|
|
7
7
|
const call_1 = require("../visitors/call");
|
|
8
|
-
function transformObjectConstructorCall(context,
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const methodName = method.name.text;
|
|
8
|
+
function transformObjectConstructorCall(context, node, calledMethod) {
|
|
9
|
+
const args = (0, call_1.transformArguments)(context, node.arguments);
|
|
10
|
+
const methodName = calledMethod.name.text;
|
|
12
11
|
switch (methodName) {
|
|
13
12
|
case "assign":
|
|
14
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectAssign,
|
|
13
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectAssign, node, ...args);
|
|
15
14
|
case "defineProperty":
|
|
16
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectDefineProperty,
|
|
15
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectDefineProperty, node, ...args);
|
|
17
16
|
case "entries":
|
|
18
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectEntries,
|
|
17
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectEntries, node, ...args);
|
|
19
18
|
case "fromEntries":
|
|
20
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectFromEntries,
|
|
19
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectFromEntries, node, ...args);
|
|
21
20
|
case "getOwnPropertyDescriptor":
|
|
22
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptor,
|
|
21
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptor, node, ...args);
|
|
23
22
|
case "getOwnPropertyDescriptors":
|
|
24
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptors,
|
|
23
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectGetOwnPropertyDescriptors, node, ...args);
|
|
25
24
|
case "keys":
|
|
26
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectKeys,
|
|
25
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectKeys, node, ...args);
|
|
27
26
|
case "values":
|
|
28
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectValues,
|
|
27
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ObjectValues, node, ...args);
|
|
29
28
|
default:
|
|
30
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
29
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Object", methodName));
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
exports.transformObjectConstructorCall = transformObjectConstructorCall;
|
|
34
|
-
function transformObjectPrototypeCall(context, node,
|
|
33
|
+
function transformObjectPrototypeCall(context, node, calledMethod) {
|
|
35
34
|
const signature = context.checker.getResolvedSignature(node);
|
|
36
|
-
const name =
|
|
35
|
+
const name = calledMethod.name.text;
|
|
37
36
|
switch (name) {
|
|
38
37
|
case "toString":
|
|
39
38
|
const toStringIdentifier = lua.createIdentifier("tostring");
|
|
40
|
-
return lua.createCallExpression(toStringIdentifier, [context.transformExpression(
|
|
39
|
+
return lua.createCallExpression(toStringIdentifier, [context.transformExpression(calledMethod.expression)], node);
|
|
41
40
|
case "hasOwnProperty":
|
|
42
|
-
const expr = context.transformExpression(
|
|
41
|
+
const expr = context.transformExpression(calledMethod.expression);
|
|
43
42
|
const parameters = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
44
43
|
const rawGetIdentifier = lua.createIdentifier("rawget");
|
|
45
44
|
const rawGetCall = lua.createCallExpression(rawGetIdentifier, [expr, ...parameters]);
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
import { PropertyCallExpression } from "../visitors/call";
|
|
5
4
|
export declare function isPromiseClass(context: TransformationContext, node: ts.Identifier): boolean;
|
|
6
5
|
export declare function createPromiseIdentifier(original: ts.Node): lua.Identifier;
|
|
7
|
-
export declare function transformPromiseConstructorCall(context: TransformationContext, node:
|
|
6
|
+
export declare function transformPromiseConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
8
7
|
export declare function createStaticPromiseFunctionAccessor(functionName: string, node: ts.Node): lua.TableIndexExpression;
|
|
@@ -15,11 +15,10 @@ function createPromiseIdentifier(original) {
|
|
|
15
15
|
return lua.createIdentifier("__TS__Promise", original);
|
|
16
16
|
}
|
|
17
17
|
exports.createPromiseIdentifier = createPromiseIdentifier;
|
|
18
|
-
function transformPromiseConstructorCall(context, node) {
|
|
19
|
-
const expression = node.expression;
|
|
18
|
+
function transformPromiseConstructorCall(context, node, calledMethod) {
|
|
20
19
|
const signature = context.checker.getResolvedSignature(node);
|
|
21
20
|
const params = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
22
|
-
const expressionName =
|
|
21
|
+
const expressionName = calledMethod.name.text;
|
|
23
22
|
switch (expressionName) {
|
|
24
23
|
case "all":
|
|
25
24
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.PromiseAll, node, ...params);
|
|
@@ -31,12 +30,12 @@ function transformPromiseConstructorCall(context, node) {
|
|
|
31
30
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.PromiseRace, node, ...params);
|
|
32
31
|
case "resolve":
|
|
33
32
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Promise);
|
|
34
|
-
return lua.createCallExpression(createStaticPromiseFunctionAccessor("resolve",
|
|
33
|
+
return lua.createCallExpression(createStaticPromiseFunctionAccessor("resolve", calledMethod), params, node);
|
|
35
34
|
case "reject":
|
|
36
35
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Promise);
|
|
37
|
-
return lua.createCallExpression(createStaticPromiseFunctionAccessor("reject",
|
|
36
|
+
return lua.createCallExpression(createStaticPromiseFunctionAccessor("reject", calledMethod), params, node);
|
|
38
37
|
default:
|
|
39
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
38
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Promise", expressionName));
|
|
40
39
|
}
|
|
41
40
|
}
|
|
42
41
|
exports.transformPromiseConstructorCall = transformPromiseConstructorCall;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { TransformationContext } from "../context";
|
|
4
|
-
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function transformStringConstructorCall(context: TransformationContext, node: PropertyCallExpression): lua.Expression | undefined;
|
|
4
|
+
export declare function transformStringPrototypeCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
5
|
+
export declare function transformStringConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.Expression | undefined;
|
|
7
6
|
export declare function transformStringProperty(context: TransformationContext, node: ts.PropertyAccessExpression): lua.UnaryExpression | undefined;
|
|
@@ -10,11 +10,10 @@ function createStringCall(methodName, tsOriginal, ...params) {
|
|
|
10
10
|
const stringIdentifier = lua.createIdentifier("string");
|
|
11
11
|
return lua.createCallExpression(lua.createTableIndexExpression(stringIdentifier, lua.createStringLiteral(methodName)), params, tsOriginal);
|
|
12
12
|
}
|
|
13
|
-
function transformStringPrototypeCall(context, node) {
|
|
14
|
-
const expression = node.expression;
|
|
13
|
+
function transformStringPrototypeCall(context, node, calledMethod) {
|
|
15
14
|
const signature = context.checker.getResolvedSignature(node);
|
|
16
|
-
const [caller, params] = (0, call_1.transformCallAndArguments)(context,
|
|
17
|
-
const expressionName =
|
|
15
|
+
const [caller, params] = (0, call_1.transformCallAndArguments)(context, calledMethod.expression, node.arguments, signature);
|
|
16
|
+
const expressionName = calledMethod.name.text;
|
|
18
17
|
switch (expressionName) {
|
|
19
18
|
case "replace":
|
|
20
19
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringReplace, node, caller, ...params);
|
|
@@ -102,20 +101,19 @@ function transformStringPrototypeCall(context, node) {
|
|
|
102
101
|
case "padEnd":
|
|
103
102
|
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.StringPadEnd, node, caller, ...params);
|
|
104
103
|
default:
|
|
105
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
104
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "string", expressionName));
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
exports.transformStringPrototypeCall = transformStringPrototypeCall;
|
|
109
|
-
function transformStringConstructorCall(context, node) {
|
|
110
|
-
const expression = node.expression;
|
|
108
|
+
function transformStringConstructorCall(context, node, calledMethod) {
|
|
111
109
|
const signature = context.checker.getResolvedSignature(node);
|
|
112
110
|
const params = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
113
|
-
const expressionName =
|
|
111
|
+
const expressionName = calledMethod.name.text;
|
|
114
112
|
switch (expressionName) {
|
|
115
113
|
case "fromCharCode":
|
|
116
114
|
return lua.createCallExpression(lua.createTableIndexExpression(lua.createIdentifier("string"), lua.createStringLiteral("char")), params, node);
|
|
117
115
|
default:
|
|
118
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
116
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "String", expressionName));
|
|
119
117
|
}
|
|
120
118
|
}
|
|
121
119
|
exports.transformStringConstructorCall = transformStringConstructorCall;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
+
import ts = require("typescript");
|
|
1
2
|
import * as lua from "../../LuaAST";
|
|
2
3
|
import { TransformationContext } from "../context";
|
|
3
|
-
|
|
4
|
-
export declare function transformSymbolConstructorCall(context: TransformationContext, expression: PropertyCallExpression): lua.CallExpression | undefined;
|
|
4
|
+
export declare function transformSymbolConstructorCall(context: TransformationContext, node: ts.CallExpression, calledMethod: ts.PropertyAccessExpression): lua.CallExpression | undefined;
|
|
@@ -5,20 +5,19 @@ const lua = require("../../LuaAST");
|
|
|
5
5
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
6
6
|
const lualib_1 = require("../utils/lualib");
|
|
7
7
|
const call_1 = require("../visitors/call");
|
|
8
|
-
function transformSymbolConstructorCall(context,
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const methodName = method.name.text;
|
|
8
|
+
function transformSymbolConstructorCall(context, node, calledMethod) {
|
|
9
|
+
const signature = context.checker.getResolvedSignature(node);
|
|
10
|
+
const parameters = (0, call_1.transformArguments)(context, node.arguments, signature);
|
|
11
|
+
const methodName = calledMethod.name.text;
|
|
13
12
|
switch (methodName) {
|
|
14
13
|
case "for":
|
|
15
14
|
case "keyFor":
|
|
16
15
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.SymbolRegistry);
|
|
17
16
|
const upperMethodName = methodName[0].toUpperCase() + methodName.slice(1);
|
|
18
17
|
const functionIdentifier = lua.createIdentifier(`__TS__SymbolRegistry${upperMethodName}`);
|
|
19
|
-
return lua.createCallExpression(functionIdentifier, parameters,
|
|
18
|
+
return lua.createCallExpression(functionIdentifier, parameters, node);
|
|
20
19
|
default:
|
|
21
|
-
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(
|
|
20
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(calledMethod.name, "Symbol", methodName));
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
exports.transformSymbolConstructorCall = transformSymbolConstructorCall;
|
|
@@ -104,6 +104,7 @@ function hasMultipleReferences(scope, identifiers) {
|
|
|
104
104
|
function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal, overrideExportScope) {
|
|
105
105
|
let declaration;
|
|
106
106
|
let assignment;
|
|
107
|
+
const noImplicitGlobalVariables = context.options.noImplicitGlobalVariables === true;
|
|
107
108
|
const isFunctionDeclaration = tsOriginal !== undefined && ts.isFunctionDeclaration(tsOriginal);
|
|
108
109
|
const identifiers = (0, utils_1.castArray)(lhs);
|
|
109
110
|
if (identifiers.length === 0) {
|
|
@@ -122,7 +123,7 @@ function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal,
|
|
|
122
123
|
else {
|
|
123
124
|
const scope = (0, scope_1.peekScope)(context);
|
|
124
125
|
const isTopLevelVariable = scope.type === scope_1.ScopeType.File;
|
|
125
|
-
if (context.isModule || !isTopLevelVariable) {
|
|
126
|
+
if (context.isModule || !isTopLevelVariable || noImplicitGlobalVariables) {
|
|
126
127
|
const isLuaFunctionExpression = rhs && !Array.isArray(rhs) && lua.isFunctionExpression(rhs);
|
|
127
128
|
const isSafeRecursiveFunctionDeclaration = isFunctionDeclaration && isLuaFunctionExpression;
|
|
128
129
|
if (!isSafeRecursiveFunctionDeclaration && hasMultipleReferences(scope, lhs)) {
|
|
@@ -163,6 +164,7 @@ function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal,
|
|
|
163
164
|
}
|
|
164
165
|
}
|
|
165
166
|
}
|
|
167
|
+
setJSDocComments(context, tsOriginal, declaration, assignment);
|
|
166
168
|
if (declaration && assignment) {
|
|
167
169
|
return [declaration, assignment];
|
|
168
170
|
}
|
|
@@ -177,6 +179,86 @@ function createLocalOrExportedOrGlobalDeclaration(context, lhs, rhs, tsOriginal,
|
|
|
177
179
|
}
|
|
178
180
|
}
|
|
179
181
|
exports.createLocalOrExportedOrGlobalDeclaration = createLocalOrExportedOrGlobalDeclaration;
|
|
182
|
+
/**
|
|
183
|
+
* Apply JSDoc comments to the newly-created Lua statement, if present.
|
|
184
|
+
* https://stackoverflow.com/questions/47429792/is-it-possible-to-get-comments-as-nodes-in-the-ast-using-the-typescript-compiler
|
|
185
|
+
*/
|
|
186
|
+
function setJSDocComments(context, tsOriginal, declaration, assignment) {
|
|
187
|
+
// Respect the vanilla TypeScript option of "removeComments":
|
|
188
|
+
// https://www.typescriptlang.org/tsconfig#removeComments
|
|
189
|
+
if (context.options.removeComments) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const docCommentArray = getJSDocCommentFromTSNode(context, tsOriginal);
|
|
193
|
+
if (docCommentArray === undefined) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (declaration && assignment) {
|
|
197
|
+
declaration.leadingComments = docCommentArray;
|
|
198
|
+
}
|
|
199
|
+
else if (declaration) {
|
|
200
|
+
declaration.leadingComments = docCommentArray;
|
|
201
|
+
}
|
|
202
|
+
else if (assignment) {
|
|
203
|
+
assignment.leadingComments = docCommentArray;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function getJSDocCommentFromTSNode(context, tsOriginal) {
|
|
207
|
+
if (tsOriginal === undefined) {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
// The "name" property is only on a subset of node types; we want to be permissive and get the
|
|
211
|
+
// comments from as many nodes as possible.
|
|
212
|
+
const node = tsOriginal;
|
|
213
|
+
if (node.name === undefined) {
|
|
214
|
+
return undefined;
|
|
215
|
+
}
|
|
216
|
+
const symbol = context.checker.getSymbolAtLocation(node.name);
|
|
217
|
+
if (symbol === undefined) {
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
// The TypeScript compiler separates JSDoc comments into the "documentation comment" and the
|
|
221
|
+
// "tags". The former is conventionally at the top of the comment, and the bottom is
|
|
222
|
+
// conventionally at the bottom. We need to get both from the TypeScript API and then combine
|
|
223
|
+
// them into one block of text.
|
|
224
|
+
const docCommentArray = symbol.getDocumentationComment(context.checker);
|
|
225
|
+
const docCommentText = ts.displayPartsToString(docCommentArray).trim();
|
|
226
|
+
const jsDocTagInfoArray = symbol.getJsDocTags(context.checker);
|
|
227
|
+
const jsDocTagsTextLines = jsDocTagInfoArray.map(jsDocTagInfo => {
|
|
228
|
+
let text = "@" + jsDocTagInfo.name;
|
|
229
|
+
if (jsDocTagInfo.text !== undefined) {
|
|
230
|
+
const tagDescriptionTextArray = jsDocTagInfo.text
|
|
231
|
+
.filter(symbolDisplayPart => symbolDisplayPart.text.trim() !== "")
|
|
232
|
+
.map(symbolDisplayPart => symbolDisplayPart.text.trim());
|
|
233
|
+
const tagDescriptionText = tagDescriptionTextArray.join(" ");
|
|
234
|
+
text += " " + tagDescriptionText;
|
|
235
|
+
}
|
|
236
|
+
return text;
|
|
237
|
+
});
|
|
238
|
+
const jsDocTagsText = jsDocTagsTextLines.join("\n");
|
|
239
|
+
const combined = (docCommentText + "\n\n" + jsDocTagsText).trim();
|
|
240
|
+
if (combined === "") {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
// By default, TSTL will display comments immediately next to the "--" characters. We can make
|
|
244
|
+
// the comments look better if we separate them by a space (similar to what Prettier does in
|
|
245
|
+
// JavaScript/TypeScript).
|
|
246
|
+
const linesWithoutSpace = combined.split("\n");
|
|
247
|
+
const lines = linesWithoutSpace.map(line => ` ${line}`);
|
|
248
|
+
// We want to JSDoc comments to map on to LDoc comments:
|
|
249
|
+
// https://stevedonovan.github.io/ldoc/manual/doc.md.html
|
|
250
|
+
// LDoc comments require that the first line starts with three hyphens.
|
|
251
|
+
// Thus, need to add a hyphen to the first line.
|
|
252
|
+
const firstLine = lines[0];
|
|
253
|
+
if (firstLine.startsWith(" @")) {
|
|
254
|
+
lines.unshift("-");
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
lines.shift();
|
|
258
|
+
lines.unshift("-" + firstLine);
|
|
259
|
+
}
|
|
260
|
+
return lines;
|
|
261
|
+
}
|
|
180
262
|
const createNaN = (tsOriginal) => lua.createBinaryExpression(lua.createNumericLiteral(0), lua.createNumericLiteral(0), lua.SyntaxKind.DivisionOperator, tsOriginal);
|
|
181
263
|
exports.createNaN = createNaN;
|
|
182
264
|
//# sourceMappingURL=lua-ast.js.map
|
|
@@ -3,4 +3,4 @@ import * as lua from "../../LuaAST";
|
|
|
3
3
|
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
4
|
export declare const transformAwaitExpression: FunctionVisitor<ts.AwaitExpression>;
|
|
5
5
|
export declare function isAsyncFunction(declaration: ts.FunctionLikeDeclaration): boolean;
|
|
6
|
-
export declare function wrapInAsyncAwaiter(context: TransformationContext, statements: lua.Statement[]): lua.
|
|
6
|
+
export declare function wrapInAsyncAwaiter(context: TransformationContext, statements: lua.Statement[], includeResolveParameter?: boolean): lua.CallExpression;
|
|
@@ -7,16 +7,12 @@ const diagnostics_1 = require("../utils/diagnostics");
|
|
|
7
7
|
const lualib_1 = require("../utils/lualib");
|
|
8
8
|
const typescript_1 = require("../utils/typescript");
|
|
9
9
|
const transformAwaitExpression = (node, context) => {
|
|
10
|
-
var _a;
|
|
11
10
|
// Check if await is inside an async function, it is not allowed at top level or in non-async functions
|
|
12
|
-
|
|
13
|
-
if (containingFunction === undefined ||
|
|
14
|
-
!((_a = containingFunction.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword))) {
|
|
11
|
+
if (!(0, typescript_1.isInAsyncFunction)(node)) {
|
|
15
12
|
context.diagnostics.push((0, diagnostics_1.awaitMustBeInAsyncFunction)(node));
|
|
16
13
|
}
|
|
17
14
|
const expression = context.transformExpression(node.expression);
|
|
18
|
-
|
|
19
|
-
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Await, node, catchIdentifier, expression);
|
|
15
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Await, node, expression);
|
|
20
16
|
};
|
|
21
17
|
exports.transformAwaitExpression = transformAwaitExpression;
|
|
22
18
|
function isAsyncFunction(declaration) {
|
|
@@ -24,15 +20,12 @@ function isAsyncFunction(declaration) {
|
|
|
24
20
|
return (_b = (_a = declaration.modifiers) === null || _a === void 0 ? void 0 : _a.some(m => m.kind === ts.SyntaxKind.AsyncKeyword)) !== null && _b !== void 0 ? _b : false;
|
|
25
21
|
}
|
|
26
22
|
exports.isAsyncFunction = isAsyncFunction;
|
|
27
|
-
function wrapInAsyncAwaiter(context, statements) {
|
|
23
|
+
function wrapInAsyncAwaiter(context, statements, includeResolveParameter = true) {
|
|
28
24
|
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Await);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
]),
|
|
34
|
-
]),
|
|
35
|
-
];
|
|
25
|
+
const parameters = includeResolveParameter ? [lua.createIdentifier("____awaiter_resolve")] : [];
|
|
26
|
+
return lua.createCallExpression(lua.createIdentifier("__TS__AsyncAwaiter"), [
|
|
27
|
+
lua.createFunctionExpression(lua.createBlock(statements), parameters),
|
|
28
|
+
]);
|
|
36
29
|
}
|
|
37
30
|
exports.wrapInAsyncAwaiter = wrapInAsyncAwaiter;
|
|
38
31
|
//# sourceMappingURL=async-await.js.map
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
2
|
import * as lua from "../../LuaAST";
|
|
3
3
|
import { FunctionVisitor, TransformationContext } from "../context";
|
|
4
|
-
export declare type PropertyCallExpression = ts.CallExpression & {
|
|
5
|
-
expression: ts.PropertyAccessExpression;
|
|
6
|
-
};
|
|
7
4
|
export declare function validateArguments(context: TransformationContext, params: readonly ts.Expression[], signature?: ts.Signature): void;
|
|
8
5
|
export declare function transformArguments(context: TransformationContext, params: readonly ts.Expression[], signature?: ts.Signature, callContext?: ts.Expression): lua.Expression[];
|
|
9
6
|
export declare function transformCallAndArguments(context: TransformationContext, callExpression: ts.Expression, params: readonly ts.Expression[], signature?: ts.Signature, callContext?: ts.Expression): [lua.Expression, lua.Expression[]];
|
|
10
7
|
export declare function transformContextualCallExpression(context: TransformationContext, node: ts.CallExpression | ts.TaggedTemplateExpression, args: ts.Expression[] | ts.NodeArray<ts.Expression>, signature?: ts.Signature): lua.Expression;
|
|
11
8
|
export declare const transformCallExpression: FunctionVisitor<ts.CallExpression>;
|
|
9
|
+
export declare function getCalledExpression(node: ts.CallExpression): ts.Expression;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformCallExpression = exports.transformContextualCallExpression = exports.transformCallAndArguments = exports.transformArguments = exports.validateArguments = void 0;
|
|
3
|
+
exports.getCalledExpression = exports.transformCallExpression = exports.transformContextualCallExpression = exports.transformCallAndArguments = exports.transformArguments = exports.validateArguments = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const builtins_1 = require("../builtins");
|
|
@@ -82,7 +82,7 @@ function transformContextualCallExpression(context, node, args, signature) {
|
|
|
82
82
|
if (ts.isOptionalChain(node)) {
|
|
83
83
|
return (0, optional_chaining_1.transformOptionalChain)(context, node);
|
|
84
84
|
}
|
|
85
|
-
const left = ts.isCallExpression(node) ? node
|
|
85
|
+
const left = ts.isCallExpression(node) ? getCalledExpression(node) : node.tag;
|
|
86
86
|
let [argPrecedingStatements, transformedArguments] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => transformArguments(context, args, signature));
|
|
87
87
|
if (ts.isPropertyAccessExpression(left) &&
|
|
88
88
|
ts.isIdentifier(left.name) &&
|
|
@@ -113,9 +113,9 @@ function transformContextualCallExpression(context, node, args, signature) {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
exports.transformContextualCallExpression = transformContextualCallExpression;
|
|
116
|
-
function transformPropertyCall(context, node) {
|
|
116
|
+
function transformPropertyCall(context, node, calledMethod) {
|
|
117
117
|
const signature = context.checker.getResolvedSignature(node);
|
|
118
|
-
if (
|
|
118
|
+
if (calledMethod.expression.kind === ts.SyntaxKind.SuperKeyword) {
|
|
119
119
|
// Super calls take the format of super.call(self,...)
|
|
120
120
|
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
|
|
121
121
|
return lua.createCallExpression(context.transformExpression(node.expression), parameters);
|
|
@@ -146,14 +146,15 @@ function transformElementCall(context, node) {
|
|
|
146
146
|
}
|
|
147
147
|
const transformCallExpression = (node, context) => {
|
|
148
148
|
var _a;
|
|
149
|
-
|
|
149
|
+
const calledExpression = getCalledExpression(node);
|
|
150
|
+
if (calledExpression.kind === ts.SyntaxKind.ImportKeyword) {
|
|
150
151
|
return (0, import_1.transformImportExpression)(node, context);
|
|
151
152
|
}
|
|
152
153
|
if (ts.isOptionalChain(node)) {
|
|
153
154
|
return (0, optional_chaining_1.transformOptionalChain)(context, node);
|
|
154
155
|
}
|
|
155
|
-
const optionalContinuation = ts.isIdentifier(
|
|
156
|
-
? (0, optional_chaining_1.getOptionalContinuationData)(
|
|
156
|
+
const optionalContinuation = ts.isIdentifier(calledExpression)
|
|
157
|
+
? (0, optional_chaining_1.getOptionalContinuationData)(calledExpression)
|
|
157
158
|
: undefined;
|
|
158
159
|
const wrapResultInTable = (0, multi_1.isMultiReturnCall)(context, node) && (0, multi_1.shouldMultiReturnCallBeWrapped)(context, node);
|
|
159
160
|
if ((0, annotations_1.isTupleReturnCall)(context, node)) {
|
|
@@ -164,22 +165,22 @@ const transformCallExpression = (node, context) => {
|
|
|
164
165
|
// unsupportedOptionalCall diagnostic already present
|
|
165
166
|
return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(builtinOrExtensionResult) : builtinOrExtensionResult;
|
|
166
167
|
}
|
|
167
|
-
if (ts.isPropertyAccessExpression(
|
|
168
|
-
const ownerType = context.checker.getTypeAtLocation(
|
|
168
|
+
if (ts.isPropertyAccessExpression(calledExpression)) {
|
|
169
|
+
const ownerType = context.checker.getTypeAtLocation(calledExpression.expression);
|
|
169
170
|
const annotations = (0, annotations_1.getTypeAnnotations)(ownerType);
|
|
170
171
|
if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
|
|
171
172
|
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
|
|
172
173
|
}
|
|
173
|
-
const result = transformPropertyCall(context, node);
|
|
174
|
+
const result = transformPropertyCall(context, node, calledExpression);
|
|
174
175
|
return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(result) : result;
|
|
175
176
|
}
|
|
176
|
-
if (ts.isElementAccessExpression(
|
|
177
|
+
if (ts.isElementAccessExpression(calledExpression)) {
|
|
177
178
|
const result = transformElementCall(context, node);
|
|
178
179
|
return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(result) : result;
|
|
179
180
|
}
|
|
180
181
|
const signature = context.checker.getResolvedSignature(node);
|
|
181
182
|
// Handle super calls properly
|
|
182
|
-
if (
|
|
183
|
+
if (calledExpression.kind === ts.SyntaxKind.SuperKeyword) {
|
|
183
184
|
const parameters = transformArguments(context, node.arguments, signature, ts.factory.createThis());
|
|
184
185
|
return lua.createCallExpression(lua.createTableIndexExpression(context.transformExpression(ts.factory.createSuper()), lua.createStringLiteral("____constructor")), parameters);
|
|
185
186
|
}
|
|
@@ -188,13 +189,13 @@ const transformCallExpression = (node, context) => {
|
|
|
188
189
|
let parameters;
|
|
189
190
|
const isContextualCall = !signatureDeclaration || (0, function_context_1.getDeclarationContextType)(context, signatureDeclaration) !== function_context_1.ContextType.Void;
|
|
190
191
|
if (!isContextualCall) {
|
|
191
|
-
[callPath, parameters] = transformCallAndArguments(context,
|
|
192
|
+
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature);
|
|
192
193
|
}
|
|
193
194
|
else {
|
|
194
195
|
// if is optionalContinuation, context will be handled by transformOptionalChain.
|
|
195
196
|
const useGlobalContext = !context.isStrict && optionalContinuation === undefined;
|
|
196
197
|
const callContext = useGlobalContext ? ts.factory.createIdentifier("_G") : ts.factory.createNull();
|
|
197
|
-
[callPath, parameters] = transformCallAndArguments(context,
|
|
198
|
+
[callPath, parameters] = transformCallAndArguments(context, calledExpression, node.arguments, signature, callContext);
|
|
198
199
|
}
|
|
199
200
|
const callExpression = lua.createCallExpression(callPath, parameters, node);
|
|
200
201
|
if (optionalContinuation && isContextualCall) {
|
|
@@ -203,4 +204,12 @@ const transformCallExpression = (node, context) => {
|
|
|
203
204
|
return wrapResultInTable ? (0, lua_ast_1.wrapInTable)(callExpression) : callExpression;
|
|
204
205
|
};
|
|
205
206
|
exports.transformCallExpression = transformCallExpression;
|
|
207
|
+
function getCalledExpression(node) {
|
|
208
|
+
function unwrapExpression(expression) {
|
|
209
|
+
expression = ts.skipOuterExpressions(expression);
|
|
210
|
+
return ts.isNonNullExpression(expression) ? unwrapExpression(expression.expression) : expression;
|
|
211
|
+
}
|
|
212
|
+
return unwrapExpression(node.expression);
|
|
213
|
+
}
|
|
214
|
+
exports.getCalledExpression = getCalledExpression;
|
|
206
215
|
//# sourceMappingURL=call.js.map
|
|
@@ -5,21 +5,58 @@ const __1 = require("../..");
|
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
6
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
7
7
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
8
|
+
const lualib_1 = require("../utils/lualib");
|
|
8
9
|
const scope_1 = require("../utils/scope");
|
|
9
10
|
const typescript_1 = require("../utils/typescript");
|
|
11
|
+
const async_await_1 = require("./async-await");
|
|
10
12
|
const block_1 = require("./block");
|
|
11
13
|
const identifier_1 = require("./identifier");
|
|
12
14
|
const multi_1 = require("./language-extensions/multi");
|
|
13
15
|
const return_1 = require("./return");
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (context.options.luaTarget === __1.LuaTarget.Lua51 &&
|
|
18
|
-
(0, typescript_1.isInAsyncFunction)(statement) &&
|
|
19
|
-
!context.options.lua51AllowTryCatchInAsyncAwait) {
|
|
16
|
+
const transformAsyncTry = (statement, context) => {
|
|
17
|
+
const [tryBlock] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try);
|
|
18
|
+
if (context.options.luaTarget === __1.LuaTarget.Lua51 && !context.options.lua51AllowTryCatchInAsyncAwait) {
|
|
20
19
|
context.diagnostics.push((0, diagnostics_1.unsupportedForTargetButOverrideAvailable)(statement, "try/catch inside async functions", __1.LuaTarget.Lua51, "lua51AllowTryCatchInAsyncAwait"));
|
|
21
20
|
return tryBlock.statements;
|
|
22
21
|
}
|
|
22
|
+
// __TS__AsyncAwaiter(<catch block>)
|
|
23
|
+
const awaiter = (0, async_await_1.wrapInAsyncAwaiter)(context, tryBlock.statements, false);
|
|
24
|
+
const awaiterIdentifier = lua.createIdentifier("____try");
|
|
25
|
+
const awaiterDefinition = lua.createVariableDeclarationStatement(awaiterIdentifier, awaiter);
|
|
26
|
+
// local ____try = __TS__AsyncAwaiter(<catch block>)
|
|
27
|
+
const result = [awaiterDefinition];
|
|
28
|
+
if (statement.finallyBlock) {
|
|
29
|
+
const awaiterFinally = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("finally"));
|
|
30
|
+
const finallyFunction = lua.createFunctionExpression(lua.createBlock(context.transformStatements(statement.finallyBlock.statements)));
|
|
31
|
+
const finallyCall = lua.createCallExpression(awaiterFinally, [awaiterIdentifier, finallyFunction], statement.finallyBlock);
|
|
32
|
+
// ____try.finally(<finally function>)
|
|
33
|
+
result.push(lua.createExpressionStatement(finallyCall));
|
|
34
|
+
}
|
|
35
|
+
if (statement.catchClause) {
|
|
36
|
+
// ____try.catch(<catch function>)
|
|
37
|
+
const [catchFunction] = transformCatchClause(context, statement.catchClause);
|
|
38
|
+
if (catchFunction.params) {
|
|
39
|
+
catchFunction.params.unshift(lua.createAnonymousIdentifier());
|
|
40
|
+
}
|
|
41
|
+
const awaiterCatch = lua.createTableIndexExpression(awaiterIdentifier, lua.createStringLiteral("catch"));
|
|
42
|
+
const catchCall = lua.createCallExpression(awaiterCatch, [awaiterIdentifier, catchFunction]);
|
|
43
|
+
// await ____try.catch(<catch function>)
|
|
44
|
+
const promiseAwait = (0, lualib_1.transformLuaLibFunction)(context, __1.LuaLibFeature.Await, statement, catchCall);
|
|
45
|
+
result.push(lua.createExpressionStatement(promiseAwait, statement));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// await ____try
|
|
49
|
+
const promiseAwait = (0, lualib_1.transformLuaLibFunction)(context, __1.LuaLibFeature.Await, statement, awaiterIdentifier);
|
|
50
|
+
result.push(lua.createExpressionStatement(promiseAwait, statement));
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
54
|
+
const transformTryStatement = (statement, context) => {
|
|
55
|
+
var _a;
|
|
56
|
+
if ((0, typescript_1.isInAsyncFunction)(statement)) {
|
|
57
|
+
return transformAsyncTry(statement, context);
|
|
58
|
+
}
|
|
59
|
+
const [tryBlock, tryScope] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try);
|
|
23
60
|
if (context.options.luaTarget === __1.LuaTarget.Lua51 && (0, typescript_1.isInGeneratorFunction)(statement)) {
|
|
24
61
|
context.diagnostics.push((0, diagnostics_1.unsupportedForTarget)(statement, "try/catch inside generator functions", __1.LuaTarget.Lua51));
|
|
25
62
|
return tryBlock.statements;
|
|
@@ -33,11 +70,7 @@ const transformTryStatement = (statement, context) => {
|
|
|
33
70
|
const tryCall = lua.createCallExpression(pCall, [lua.createFunctionExpression(tryBlock)]);
|
|
34
71
|
if (statement.catchClause && statement.catchClause.block.statements.length > 0) {
|
|
35
72
|
// try with catch
|
|
36
|
-
const [
|
|
37
|
-
const catchParameter = statement.catchClause.variableDeclaration
|
|
38
|
-
? (0, identifier_1.transformIdentifier)(context, statement.catchClause.variableDeclaration.name)
|
|
39
|
-
: undefined;
|
|
40
|
-
const catchFunction = lua.createFunctionExpression(catchBlock, catchParameter ? [lua.cloneIdentifier(catchParameter)] : []);
|
|
73
|
+
const [catchFunction, catchScope] = transformCatchClause(context, statement.catchClause);
|
|
41
74
|
const catchIdentifier = lua.createIdentifier("____catch");
|
|
42
75
|
result.push(lua.createVariableDeclarationStatement(catchIdentifier, catchFunction));
|
|
43
76
|
const hasReturn = (_a = tryScope.functionReturned) !== null && _a !== void 0 ? _a : catchScope.functionReturned;
|
|
@@ -96,4 +129,12 @@ const transformThrowStatement = (statement, context) => {
|
|
|
96
129
|
return lua.createExpressionStatement(lua.createCallExpression(lua.createIdentifier("error"), parameters), statement);
|
|
97
130
|
};
|
|
98
131
|
exports.transformThrowStatement = transformThrowStatement;
|
|
132
|
+
function transformCatchClause(context, catchClause) {
|
|
133
|
+
const [catchBlock, catchScope] = (0, block_1.transformScopeBlock)(context, catchClause.block, scope_1.ScopeType.Catch);
|
|
134
|
+
const catchParameter = catchClause.variableDeclaration
|
|
135
|
+
? (0, identifier_1.transformIdentifier)(context, catchClause.variableDeclaration.name)
|
|
136
|
+
: undefined;
|
|
137
|
+
const catchFunction = lua.createFunctionExpression(catchBlock, catchParameter ? [lua.cloneIdentifier(catchParameter)] : []);
|
|
138
|
+
return [catchFunction, catchScope];
|
|
139
|
+
}
|
|
99
140
|
//# sourceMappingURL=errors.js.map
|
|
@@ -114,7 +114,7 @@ function transformFunctionBody(context, parameters, body, spreadIdentifier, node
|
|
|
114
114
|
scope.node = node;
|
|
115
115
|
let bodyStatements = transformFunctionBodyContent(context, body);
|
|
116
116
|
if (node && (0, async_await_1.isAsyncFunction)(node)) {
|
|
117
|
-
bodyStatements = (0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements);
|
|
117
|
+
bodyStatements = [lua.createReturnStatement([(0, async_await_1.wrapInAsyncAwaiter)(context, bodyStatements)])];
|
|
118
118
|
}
|
|
119
119
|
const headerStatements = transformFunctionBodyHeader(context, scope, parameters, spreadIdentifier);
|
|
120
120
|
(0, scope_1.popScope)(context);
|
|
@@ -61,14 +61,15 @@ const transformReturnStatement = (statement, context) => {
|
|
|
61
61
|
exports.transformReturnStatement = transformReturnStatement;
|
|
62
62
|
function createReturnStatement(context, values, node) {
|
|
63
63
|
const results = [...values];
|
|
64
|
+
if ((0, typescript_1.isInAsyncFunction)(node)) {
|
|
65
|
+
return lua.createReturnStatement([
|
|
66
|
+
lua.createCallExpression(lua.createIdentifier("____awaiter_resolve"), [lua.createNilLiteral(), ...values]),
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
64
69
|
if (isInTryCatch(context)) {
|
|
65
70
|
// Bubble up explicit return flag and check if we're inside a try/catch block
|
|
66
71
|
results.unshift(lua.createBooleanLiteral(true));
|
|
67
72
|
}
|
|
68
|
-
else if ((0, typescript_1.isInAsyncFunction)(node)) {
|
|
69
|
-
// Add nil error handler in async function and not in try
|
|
70
|
-
results.unshift(lua.createNilLiteral());
|
|
71
|
-
}
|
|
72
73
|
return lua.createReturnStatement(results, node);
|
|
73
74
|
}
|
|
74
75
|
exports.createReturnStatement = createReturnStatement;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-to-lua",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "A generic TypeScript to Lua transpiler. Write your code in TypeScript and publish Lua!",
|
|
5
5
|
"repository": "https://github.com/TypeScriptToLua/TypeScriptToLua",
|
|
6
6
|
"homepage": "https://typescripttolua.github.io/",
|