typescript-to-lua 1.0.0 → 1.2.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/CHANGELOG.md +34 -0
- package/dist/CompilerOptions.d.ts +4 -3
- package/dist/LuaAST.d.ts +1 -0
- package/dist/LuaAST.js +14 -6
- package/dist/LuaLib.d.ts +4 -3
- package/dist/LuaLib.js +5 -4
- package/dist/LuaPrinter.d.ts +15 -4
- package/dist/LuaPrinter.js +79 -27
- package/dist/cli/diagnostics.js +2 -2
- package/dist/cli/parse.d.ts +1 -1
- package/dist/cli/parse.js +14 -0
- package/dist/cli/report.js +1 -1
- package/dist/cli/tsconfig.js +4 -4
- package/dist/lualib/ArrayFlat.lua +1 -1
- package/dist/lualib/ArrayFlatMap.lua +1 -1
- package/dist/lualib/ArrayIsArray.lua +1 -1
- package/dist/lualib/ArrayReduce.lua +7 -1
- package/dist/lualib/ArrayReduceRight.lua +7 -1
- package/dist/lualib/ArraySetLength.lua +1 -1
- package/dist/lualib/ArraySplice.lua +5 -5
- package/dist/lualib/Await.lua +39 -19
- package/dist/lualib/CloneDescriptor.lua +8 -8
- package/dist/lualib/Decorate.lua +1 -1
- package/dist/lualib/DelegatedYield.lua +1 -3
- package/dist/lualib/Delete.lua +6 -6
- package/dist/lualib/Error.lua +47 -44
- package/dist/lualib/Generator.lua +1 -5
- package/dist/lualib/InstanceOf.lua +1 -1
- package/dist/lualib/InstanceOfObject.lua +1 -1
- package/dist/lualib/Map.lua +130 -134
- package/dist/lualib/Number.lua +1 -1
- package/dist/lualib/NumberIsFinite.lua +1 -1
- package/dist/lualib/NumberToString.lua +3 -5
- package/dist/lualib/ObjectDefineProperty.lua +40 -8
- package/dist/lualib/ParseFloat.lua +9 -5
- package/dist/lualib/ParseInt.lua +15 -3
- package/dist/lualib/Promise.lua +36 -26
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +19 -9
- package/dist/lualib/SparseArrayNew.lua +5 -0
- package/dist/lualib/SparseArrayPush.lua +9 -0
- package/dist/lualib/SparseArraySpread.lua +4 -0
- package/dist/lualib/StringAccess.lua +1 -1
- package/dist/lualib/StringCharCodeAt.lua +1 -1
- package/dist/lualib/StringEndsWith.lua +2 -2
- package/dist/lualib/StringPadEnd.lua +2 -2
- package/dist/lualib/StringPadStart.lua +2 -2
- package/dist/lualib/StringReplace.lua +12 -11
- package/dist/lualib/StringReplaceAll.lua +35 -0
- package/dist/lualib/StringSlice.lua +2 -2
- package/dist/lualib/StringSplit.lua +3 -3
- package/dist/lualib/StringStartsWith.lua +1 -1
- package/dist/lualib/StringSubstr.lua +1 -1
- package/dist/lualib/StringSubstring.lua +2 -2
- package/dist/lualib/Symbol.lua +3 -5
- package/dist/lualib/WeakMap.lua +40 -42
- package/dist/lualib/WeakSet.lua +36 -38
- package/dist/lualib/lualib_bundle.lua +705 -586
- package/dist/transformation/builtins/array.js +29 -30
- package/dist/transformation/builtins/console.js +2 -2
- package/dist/transformation/builtins/function.js +9 -10
- package/dist/transformation/builtins/global.js +8 -8
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +69 -36
- package/dist/transformation/builtins/math.js +4 -4
- package/dist/transformation/builtins/number.js +7 -7
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +12 -13
- package/dist/transformation/builtins/promise.js +9 -9
- package/dist/transformation/builtins/string.js +32 -31
- package/dist/transformation/builtins/symbol.js +3 -3
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +105 -6
- package/dist/transformation/index.js +1 -1
- package/dist/transformation/utils/annotations.d.ts +1 -3
- package/dist/transformation/utils/annotations.js +0 -2
- package/dist/transformation/utils/assignment-validation.js +8 -7
- package/dist/transformation/utils/diagnostics.d.ts +6 -0
- package/dist/transformation/utils/diagnostics.js +7 -5
- package/dist/transformation/utils/export.d.ts +3 -2
- package/dist/transformation/utils/export.js +14 -7
- package/dist/transformation/utils/function-context.js +8 -8
- package/dist/transformation/utils/lua-ast.d.ts +0 -1
- package/dist/transformation/utils/lua-ast.js +25 -35
- package/dist/transformation/utils/lualib.js +1 -1
- package/dist/transformation/utils/preceding-statements.d.ts +3 -0
- package/dist/transformation/utils/preceding-statements.js +11 -0
- package/dist/transformation/utils/safe-names.js +5 -5
- package/dist/transformation/utils/scope.d.ts +7 -0
- package/dist/transformation/utils/scope.js +77 -40
- package/dist/transformation/utils/symbols.js +6 -6
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/utils/typescript/nodes.d.ts +2 -0
- package/dist/transformation/utils/typescript/nodes.js +21 -1
- package/dist/transformation/visitors/access.d.ts +3 -0
- package/dist/transformation/visitors/access.js +74 -47
- package/dist/transformation/visitors/async-await.js +5 -4
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +86 -77
- package/dist/transformation/visitors/binary-expression/bit.js +4 -4
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +69 -71
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +65 -36
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +84 -40
- package/dist/transformation/visitors/block.js +6 -6
- package/dist/transformation/visitors/break-continue.js +4 -9
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +130 -143
- package/dist/transformation/visitors/class/decorators.js +3 -3
- package/dist/transformation/visitors/class/index.js +43 -39
- package/dist/transformation/visitors/class/members/accessors.js +6 -6
- package/dist/transformation/visitors/class/members/constructor.js +7 -7
- package/dist/transformation/visitors/class/members/fields.js +6 -6
- package/dist/transformation/visitors/class/members/method.js +6 -6
- package/dist/transformation/visitors/class/new.js +14 -16
- package/dist/transformation/visitors/class/setup.js +11 -11
- package/dist/transformation/visitors/class/utils.js +2 -2
- package/dist/transformation/visitors/conditional.js +34 -20
- package/dist/transformation/visitors/delete.js +8 -4
- package/dist/transformation/visitors/enum.js +8 -8
- package/dist/transformation/visitors/errors.js +30 -30
- package/dist/transformation/visitors/expression-list.d.ts +7 -0
- package/dist/transformation/visitors/expression-list.js +120 -0
- package/dist/transformation/visitors/expression-statement.js +10 -6
- package/dist/transformation/visitors/function.js +30 -32
- package/dist/transformation/visitors/identifier.js +26 -22
- package/dist/transformation/visitors/index.js +2 -2
- package/dist/transformation/visitors/language-extensions/iterable.js +7 -7
- package/dist/transformation/visitors/language-extensions/multi.d.ts +1 -1
- package/dist/transformation/visitors/language-extensions/multi.js +3 -3
- package/dist/transformation/visitors/language-extensions/operators.js +6 -6
- package/dist/transformation/visitors/language-extensions/range.js +6 -6
- package/dist/transformation/visitors/language-extensions/table.js +5 -5
- package/dist/transformation/visitors/literal.d.ts +1 -2
- package/dist/transformation/visitors/literal.js +87 -57
- package/dist/transformation/visitors/loops/do-while.js +35 -9
- package/dist/transformation/visitors/loops/for-in.js +4 -4
- package/dist/transformation/visitors/loops/for-of.js +13 -13
- package/dist/transformation/visitors/loops/for.js +28 -8
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +21 -10
- package/dist/transformation/visitors/modules/export.js +14 -15
- package/dist/transformation/visitors/modules/import.js +10 -10
- package/dist/transformation/visitors/namespace.js +13 -13
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/return.d.ts +1 -0
- package/dist/transformation/visitors/return.js +40 -26
- package/dist/transformation/visitors/sourceFile.js +12 -9
- package/dist/transformation/visitors/spread.js +11 -11
- package/dist/transformation/visitors/switch.js +152 -30
- package/dist/transformation/visitors/template.js +10 -7
- package/dist/transformation/visitors/typeof.js +4 -2
- package/dist/transformation/visitors/typescript.js +1 -1
- package/dist/transformation/visitors/unary-expression.js +9 -9
- package/dist/transformation/visitors/variable-declaration.js +46 -32
- package/dist/transformation/visitors/void.d.ts +6 -0
- package/dist/transformation/visitors/void.js +20 -0
- package/dist/transpilation/bundle.d.ts +3 -0
- package/dist/transpilation/bundle.js +56 -13
- package/dist/transpilation/diagnostics.js +3 -3
- package/dist/transpilation/index.js +2 -2
- package/dist/transpilation/output-collector.js +2 -2
- package/dist/transpilation/plugins.js +1 -1
- package/dist/transpilation/resolve.js +26 -14
- package/dist/transpilation/transformers.js +45 -25
- package/dist/transpilation/transpile.js +6 -6
- package/dist/transpilation/transpiler.js +8 -8
- package/dist/transpilation/utils.js +5 -3
- package/dist/tstl.js +8 -8
- package/dist/utils.js +2 -2
- package/package.json +15 -12
- package/dist/lualib/OptionalChainAccess.lua +0 -6
- package/dist/lualib/OptionalFunctionCall.lua +0 -6
- package/dist/lualib/OptionalMethodCall.lua +0 -11
- package/dist/transformation/utils/transform.d.ts +0 -8
- package/dist/transformation/utils/transform.js +0 -14
- package/dist/transformation/visitors/jsx/jsx.d.ts +0 -9
- package/dist/transformation/visitors/jsx/jsx.js +0 -237
- package/dist/transformation/visitors/jsx/xhtml.d.ts +0 -3
- package/dist/transformation/visitors/jsx/xhtml.js +0 -260
|
@@ -22,14 +22,14 @@ function transformConstructorDeclaration(context, statement, className, instance
|
|
|
22
22
|
return undefined;
|
|
23
23
|
}
|
|
24
24
|
// Transform body
|
|
25
|
-
const scope = scope_1.pushScope(context, scope_1.ScopeType.Function);
|
|
26
|
-
const body = function_1.transformFunctionBodyContent(context, statement.body);
|
|
27
|
-
const [params, dotsLiteral, restParamName] = function_1.transformParameters(context, statement.parameters, lua_ast_1.createSelfIdentifier());
|
|
25
|
+
const scope = (0, scope_1.pushScope)(context, scope_1.ScopeType.Function);
|
|
26
|
+
const body = (0, function_1.transformFunctionBodyContent)(context, statement.body);
|
|
27
|
+
const [params, dotsLiteral, restParamName] = (0, function_1.transformParameters)(context, statement.parameters, (0, lua_ast_1.createSelfIdentifier)());
|
|
28
28
|
// Make sure default parameters are assigned before fields are initialized
|
|
29
|
-
const bodyWithFieldInitializers = function_1.transformFunctionBodyHeader(context, scope, statement.parameters, restParamName);
|
|
29
|
+
const bodyWithFieldInitializers = (0, function_1.transformFunctionBodyHeader)(context, scope, statement.parameters, restParamName);
|
|
30
30
|
// Check for field declarations in constructor
|
|
31
31
|
const constructorFieldsDeclarations = statement.parameters.filter(p => p.modifiers !== undefined);
|
|
32
|
-
const classInstanceFields = fields_1.transformClassInstanceFields(context, instanceFields);
|
|
32
|
+
const classInstanceFields = (0, fields_1.transformClassInstanceFields)(context, instanceFields);
|
|
33
33
|
// If there are field initializers and the first statement is a super call,
|
|
34
34
|
// move super call between default assignments and initializers
|
|
35
35
|
if ((constructorFieldsDeclarations.length > 0 || classInstanceFields.length > 0) &&
|
|
@@ -49,7 +49,7 @@ function transformConstructorDeclaration(context, statement, className, instance
|
|
|
49
49
|
for (const declaration of constructorFieldsDeclarations) {
|
|
50
50
|
if (ts.isIdentifier(declaration.name)) {
|
|
51
51
|
// self.declarationName = declarationName
|
|
52
|
-
const assignment = lua.createAssignmentStatement(lua.createTableIndexExpression(lua_ast_1.createSelfIdentifier(), lua.createStringLiteral(declaration.name.text)), identifier_1.transformIdentifier(context, declaration.name));
|
|
52
|
+
const assignment = lua.createAssignmentStatement(lua.createTableIndexExpression((0, lua_ast_1.createSelfIdentifier)(), lua.createStringLiteral(declaration.name.text)), (0, identifier_1.transformIdentifier)(context, declaration.name));
|
|
53
53
|
bodyWithFieldInitializers.push(assignment);
|
|
54
54
|
}
|
|
55
55
|
// else { TypeScript error: A parameter property may not be declared using a binding pattern }
|
|
@@ -58,7 +58,7 @@ function transformConstructorDeclaration(context, statement, className, instance
|
|
|
58
58
|
bodyWithFieldInitializers.push(...body);
|
|
59
59
|
const block = lua.createBlock(bodyWithFieldInitializers);
|
|
60
60
|
const constructorWasGenerated = statement.pos === -1;
|
|
61
|
-
scope_1.popScope(context);
|
|
61
|
+
(0, scope_1.popScope)(context);
|
|
62
62
|
return lua.createAssignmentStatement(createConstructorName(className), lua.createFunctionExpression(block, params, dotsLiteral, lua.FunctionExpressionFlags.Declaration), constructorWasGenerated ? classDeclaration : statement);
|
|
63
63
|
}
|
|
64
64
|
exports.transformConstructorDeclaration = transformConstructorDeclaration;
|
|
@@ -9,19 +9,19 @@ const method_1 = require("./method");
|
|
|
9
9
|
function createPropertyDecoratingExpression(context, node, className) {
|
|
10
10
|
if (!node.decorators)
|
|
11
11
|
return;
|
|
12
|
-
const propertyName = literal_1.transformPropertyName(context, node.name);
|
|
13
|
-
const propertyOwnerTable = method_1.transformMemberExpressionOwnerName(node, className);
|
|
14
|
-
return decorators_1.createDecoratingExpression(context, node.kind, node.decorators.map(d => decorators_1.transformDecoratorExpression(context, d)), propertyOwnerTable, propertyName);
|
|
12
|
+
const propertyName = (0, literal_1.transformPropertyName)(context, node.name);
|
|
13
|
+
const propertyOwnerTable = (0, method_1.transformMemberExpressionOwnerName)(node, className);
|
|
14
|
+
return (0, decorators_1.createDecoratingExpression)(context, node.kind, node.decorators.map(d => (0, decorators_1.transformDecoratorExpression)(context, d)), propertyOwnerTable, propertyName);
|
|
15
15
|
}
|
|
16
16
|
exports.createPropertyDecoratingExpression = createPropertyDecoratingExpression;
|
|
17
17
|
function transformClassInstanceFields(context, instanceFields) {
|
|
18
18
|
const statements = [];
|
|
19
19
|
for (const f of instanceFields) {
|
|
20
20
|
// Get identifier
|
|
21
|
-
const fieldName = literal_1.transformPropertyName(context, f.name);
|
|
21
|
+
const fieldName = (0, literal_1.transformPropertyName)(context, f.name);
|
|
22
22
|
const value = f.initializer ? context.transformExpression(f.initializer) : undefined;
|
|
23
23
|
// self[fieldName]
|
|
24
|
-
const selfIndex = lua.createTableIndexExpression(lua_ast_1.createSelfIdentifier(), fieldName);
|
|
24
|
+
const selfIndex = lua.createTableIndexExpression((0, lua_ast_1.createSelfIdentifier)(), fieldName);
|
|
25
25
|
// self[fieldName] = value
|
|
26
26
|
const assignClassField = lua.createAssignmentStatement(selfIndex, value, f);
|
|
27
27
|
statements.push(assignClassField);
|
|
@@ -32,7 +32,7 @@ exports.transformClassInstanceFields = transformClassInstanceFields;
|
|
|
32
32
|
function transformStaticPropertyDeclaration(context, field, className) {
|
|
33
33
|
if (!field.initializer)
|
|
34
34
|
return;
|
|
35
|
-
const fieldName = literal_1.transformPropertyName(context, field.name);
|
|
35
|
+
const fieldName = (0, literal_1.transformPropertyName)(context, field.name);
|
|
36
36
|
const value = context.transformExpression(field.initializer);
|
|
37
37
|
const classField = lua.createTableIndexExpression(lua.cloneIdentifier(className), fieldName);
|
|
38
38
|
return lua.createAssignmentStatement(classField, value);
|
|
@@ -10,11 +10,11 @@ const constructor_1 = require("./constructor");
|
|
|
10
10
|
const lualib_1 = require("../../../utils/lualib");
|
|
11
11
|
const utils_2 = require("../../../../utils");
|
|
12
12
|
function transformMemberExpressionOwnerName(node, className) {
|
|
13
|
-
return utils_1.isStaticNode(node) ? lua.cloneIdentifier(className) : constructor_1.createPrototypeName(className);
|
|
13
|
+
return (0, utils_1.isStaticNode)(node) ? lua.cloneIdentifier(className) : (0, constructor_1.createPrototypeName)(className);
|
|
14
14
|
}
|
|
15
15
|
exports.transformMemberExpressionOwnerName = transformMemberExpressionOwnerName;
|
|
16
16
|
function transformMethodName(context, node) {
|
|
17
|
-
const methodName = literal_1.transformPropertyName(context, node.name);
|
|
17
|
+
const methodName = (0, literal_1.transformPropertyName)(context, node.name);
|
|
18
18
|
if (lua.isStringLiteral(methodName) && methodName.value === "toString") {
|
|
19
19
|
return lua.createStringLiteral("__tostring", node.name);
|
|
20
20
|
}
|
|
@@ -27,7 +27,7 @@ function transformMethodDeclaration(context, node, className) {
|
|
|
27
27
|
return;
|
|
28
28
|
const methodTable = transformMemberExpressionOwnerName(node, className);
|
|
29
29
|
const methodName = transformMethodName(context, node);
|
|
30
|
-
const [functionExpression] = function_1.transformFunctionToExpression(context, node);
|
|
30
|
+
const [functionExpression] = (0, function_1.transformFunctionToExpression)(context, node);
|
|
31
31
|
return lua.createAssignmentStatement(lua.createTableIndexExpression(methodTable, methodName), functionExpression, node);
|
|
32
32
|
}
|
|
33
33
|
exports.transformMethodDeclaration = transformMethodDeclaration;
|
|
@@ -38,12 +38,12 @@ function createMethodDecoratingExpression(context, node, className) {
|
|
|
38
38
|
const parameterDecorators = node.parameters
|
|
39
39
|
.flatMap((parameter, index) => {
|
|
40
40
|
var _a;
|
|
41
|
-
return (_a = parameter.decorators) === null || _a === void 0 ? void 0 : _a.map(decorator => lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.DecorateParam, node, lua.createNumericLiteral(index), decorators_1.transformDecoratorExpression(context, decorator)));
|
|
41
|
+
return (_a = parameter.decorators) === null || _a === void 0 ? void 0 : _a.map(decorator => (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.DecorateParam, node, lua.createNumericLiteral(index), (0, decorators_1.transformDecoratorExpression)(context, decorator)));
|
|
42
42
|
})
|
|
43
43
|
.filter(utils_2.isNonNull);
|
|
44
|
-
const methodDecorators = (_b = (_a = node.decorators) === null || _a === void 0 ? void 0 : _a.map(d => decorators_1.transformDecoratorExpression(context, d))) !== null && _b !== void 0 ? _b : [];
|
|
44
|
+
const methodDecorators = (_b = (_a = node.decorators) === null || _a === void 0 ? void 0 : _a.map(d => (0, decorators_1.transformDecoratorExpression)(context, d))) !== null && _b !== void 0 ? _b : [];
|
|
45
45
|
if (methodDecorators.length > 0 || parameterDecorators.length > 0) {
|
|
46
|
-
const decorateMethod = decorators_1.createDecoratingExpression(context, node.kind, [...methodDecorators, ...parameterDecorators], methodTable, methodName);
|
|
46
|
+
const decorateMethod = (0, decorators_1.createDecoratingExpression)(context, node.kind, [...methodDecorators, ...parameterDecorators], methodTable, methodName);
|
|
47
47
|
return lua.createExpressionStatement(decorateMethod);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.transformNewExpression = exports.checkForLuaLibType = void 0;
|
|
4
|
+
const ts = require("typescript");
|
|
4
5
|
const lua = require("../../../LuaAST");
|
|
5
6
|
const annotations_1 = require("../../utils/annotations");
|
|
6
7
|
const diagnostics_1 = require("../../utils/diagnostics");
|
|
@@ -28,49 +29,46 @@ function checkForLuaLibType(context, type) {
|
|
|
28
29
|
const name = context.checker.getFullyQualifiedName(type.symbol);
|
|
29
30
|
switch (name) {
|
|
30
31
|
case "Map":
|
|
31
|
-
lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.Map);
|
|
32
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Map);
|
|
32
33
|
return;
|
|
33
34
|
case "Set":
|
|
34
|
-
lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.Set);
|
|
35
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Set);
|
|
35
36
|
return;
|
|
36
37
|
case "WeakMap":
|
|
37
|
-
lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.WeakMap);
|
|
38
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.WeakMap);
|
|
38
39
|
return;
|
|
39
40
|
case "WeakSet":
|
|
40
|
-
lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.WeakSet);
|
|
41
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.WeakSet);
|
|
41
42
|
return;
|
|
42
43
|
}
|
|
43
44
|
if (builtinErrorTypeNames.has(name)) {
|
|
44
|
-
lualib_1.importLuaLibFeature(context, lualib_1.LuaLibFeature.Error);
|
|
45
|
+
(0, lualib_1.importLuaLibFeature)(context, lualib_1.LuaLibFeature.Error);
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
exports.checkForLuaLibType = checkForLuaLibType;
|
|
48
49
|
const transformNewExpression = (node, context) => {
|
|
49
|
-
var _a;
|
|
50
|
+
var _a, _b;
|
|
50
51
|
const type = context.checker.getTypeAtLocation(node);
|
|
51
|
-
const annotations = annotations_1.getTypeAnnotations(type);
|
|
52
|
+
const annotations = (0, annotations_1.getTypeAnnotations)(type);
|
|
52
53
|
if (annotations.has(annotations_1.AnnotationKind.LuaTable)) {
|
|
53
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(node, annotations_1.AnnotationKind.LuaTable));
|
|
54
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(node, annotations_1.AnnotationKind.LuaTable));
|
|
54
55
|
}
|
|
55
|
-
if (table_1.isTableNewCall(context, node)) {
|
|
56
|
+
if ((0, table_1.isTableNewCall)(context, node)) {
|
|
56
57
|
return lua.createTableExpression(undefined, node);
|
|
57
58
|
}
|
|
58
59
|
const signature = context.checker.getResolvedSignature(node);
|
|
59
|
-
const params = node.arguments
|
|
60
|
-
? call_1.transformArguments(context, node.arguments, signature)
|
|
61
|
-
: [lua.createBooleanLiteral(true)];
|
|
60
|
+
const [name, params] = (0, call_1.transformCallAndArguments)(context, node.expression, (_a = node.arguments) !== null && _a !== void 0 ? _a : [ts.factory.createTrue()], signature);
|
|
62
61
|
checkForLuaLibType(context, type);
|
|
63
|
-
const name = context.transformExpression(node.expression);
|
|
64
62
|
const customConstructorAnnotation = annotations.get(annotations_1.AnnotationKind.CustomConstructor);
|
|
65
63
|
if (customConstructorAnnotation) {
|
|
66
64
|
if (customConstructorAnnotation.args.length === 1) {
|
|
67
|
-
return lua.createCallExpression(lua.createIdentifier(customConstructorAnnotation.args[0]), call_1.transformArguments(context, (
|
|
65
|
+
return lua.createCallExpression(lua.createIdentifier(customConstructorAnnotation.args[0]), (0, call_1.transformArguments)(context, (_b = node.arguments) !== null && _b !== void 0 ? _b : []), node);
|
|
68
66
|
}
|
|
69
67
|
else {
|
|
70
|
-
context.diagnostics.push(diagnostics_1.annotationInvalidArgumentCount(node, annotations_1.AnnotationKind.CustomConstructor, customConstructorAnnotation.args.length, 1));
|
|
68
|
+
context.diagnostics.push((0, diagnostics_1.annotationInvalidArgumentCount)(node, annotations_1.AnnotationKind.CustomConstructor, customConstructorAnnotation.args.length, 1));
|
|
71
69
|
}
|
|
72
70
|
}
|
|
73
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.New, node, name, ...params);
|
|
71
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.New, node, name, ...params);
|
|
74
72
|
};
|
|
75
73
|
exports.transformNewExpression = transformNewExpression;
|
|
76
74
|
//# sourceMappingURL=new.js.map
|
|
@@ -11,34 +11,34 @@ const utils_2 = require("./utils");
|
|
|
11
11
|
function createClassSetup(context, statement, className, localClassName, extendsType) {
|
|
12
12
|
const result = [];
|
|
13
13
|
// __TS__Class()
|
|
14
|
-
const classInitializer = lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.Class, statement);
|
|
15
|
-
const defaultExportLeftHandSide = export_1.hasDefaultExportModifier(statement)
|
|
16
|
-
? lua.createTableIndexExpression(lua_ast_1.createExportsIdentifier(), export_1.createDefaultExportStringLiteral(statement))
|
|
14
|
+
const classInitializer = (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Class, statement);
|
|
15
|
+
const defaultExportLeftHandSide = (0, export_1.hasDefaultExportModifier)(statement)
|
|
16
|
+
? lua.createTableIndexExpression((0, lua_ast_1.createExportsIdentifier)(), (0, export_1.createDefaultExportStringLiteral)(statement))
|
|
17
17
|
: undefined;
|
|
18
18
|
// [____exports.]className = __TS__Class()
|
|
19
19
|
if (defaultExportLeftHandSide) {
|
|
20
20
|
result.push(lua.createAssignmentStatement(defaultExportLeftHandSide, classInitializer, statement));
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
|
-
result.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, className, classInitializer, statement));
|
|
23
|
+
result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, className, classInitializer, statement));
|
|
24
24
|
}
|
|
25
25
|
if (defaultExportLeftHandSide) {
|
|
26
26
|
// local localClassName = ____exports.default
|
|
27
27
|
result.push(lua.createVariableDeclarationStatement(localClassName, defaultExportLeftHandSide));
|
|
28
28
|
}
|
|
29
29
|
else {
|
|
30
|
-
const exportScope = export_1.getIdentifierExportScope(context, className);
|
|
30
|
+
const exportScope = (0, export_1.getIdentifierExportScope)(context, className);
|
|
31
31
|
if (exportScope) {
|
|
32
32
|
// local localClassName = ____exports.className
|
|
33
|
-
result.push(lua.createVariableDeclarationStatement(localClassName, export_1.createExportedIdentifier(context, lua.cloneIdentifier(className), exportScope)));
|
|
33
|
+
result.push(lua.createVariableDeclarationStatement(localClassName, (0, export_1.createExportedIdentifier)(context, lua.cloneIdentifier(className), exportScope)));
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
// localClassName.name = className
|
|
37
37
|
result.push(lua.createAssignmentStatement(lua.createTableIndexExpression(lua.cloneIdentifier(localClassName), lua.createStringLiteral("name")), getReflectionClassName(context, statement, className), statement));
|
|
38
38
|
if (extendsType) {
|
|
39
|
-
const extendedNode = utils_2.getExtendedNode(context, statement);
|
|
40
|
-
utils_1.assert(extendedNode);
|
|
41
|
-
result.push(lua.createExpressionStatement(lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.ClassExtends, utils_2.getExtendsClause(statement), lua.cloneIdentifier(localClassName), context.transformExpression(extendedNode.expression))));
|
|
39
|
+
const extendedNode = (0, utils_2.getExtendedNode)(context, statement);
|
|
40
|
+
(0, utils_1.assert)(extendedNode);
|
|
41
|
+
result.push(lua.createExpressionStatement((0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.ClassExtends, (0, utils_2.getExtendsClause)(statement), lua.cloneIdentifier(localClassName), context.transformExpression(extendedNode.expression))));
|
|
42
42
|
}
|
|
43
43
|
return result;
|
|
44
44
|
}
|
|
@@ -50,10 +50,10 @@ function getReflectionClassName(context, declaration, className) {
|
|
|
50
50
|
else if (ts.isVariableDeclaration(declaration.parent) && ts.isIdentifier(declaration.parent.name)) {
|
|
51
51
|
return lua.createStringLiteral(declaration.parent.name.text);
|
|
52
52
|
}
|
|
53
|
-
else if (export_1.hasDefaultExportModifier(declaration)) {
|
|
53
|
+
else if ((0, export_1.hasDefaultExportModifier)(declaration)) {
|
|
54
54
|
return lua.createStringLiteral("default");
|
|
55
55
|
}
|
|
56
|
-
if (utils_2.getExtendedNode(context, declaration)) {
|
|
56
|
+
if ((0, utils_2.getExtendedNode)(context, declaration)) {
|
|
57
57
|
return lua.createTableIndexExpression(className, lua.createStringLiteral("name"));
|
|
58
58
|
}
|
|
59
59
|
return lua.createStringLiteral("");
|
|
@@ -19,9 +19,9 @@ function getExtendedNode(context, node) {
|
|
|
19
19
|
if (!extendsClause)
|
|
20
20
|
return;
|
|
21
21
|
const superType = context.checker.getTypeAtLocation(extendsClause.types[0]);
|
|
22
|
-
const annotations = annotations_1.getTypeAnnotations(superType);
|
|
22
|
+
const annotations = (0, annotations_1.getTypeAnnotations)(superType);
|
|
23
23
|
if (annotations.has(annotations_1.AnnotationKind.PureAbstract)) {
|
|
24
|
-
context.diagnostics.push(diagnostics_1.annotationRemoved(extendsClause, annotations_1.AnnotationKind.PureAbstract));
|
|
24
|
+
context.diagnostics.push((0, diagnostics_1.annotationRemoved)(extendsClause, annotations_1.AnnotationKind.PureAbstract));
|
|
25
25
|
}
|
|
26
26
|
return extendsClause.types[0];
|
|
27
27
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.transformIfStatement = exports.transformConditionalExpression = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
|
+
const preceding_statements_1 = require("../utils/preceding-statements");
|
|
6
7
|
const scope_1 = require("../utils/scope");
|
|
7
8
|
const block_1 = require("./block");
|
|
8
9
|
function canBeFalsy(context, type) {
|
|
@@ -27,20 +28,18 @@ function canBeFalsy(context, type) {
|
|
|
27
28
|
return false;
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
|
-
function wrapInFunctionCall(expression) {
|
|
31
|
-
const returnStatement = lua.createReturnStatement([expression]);
|
|
32
|
-
return lua.createFunctionExpression(lua.createBlock([returnStatement]), undefined, undefined, lua.FunctionExpressionFlags.Inline);
|
|
33
|
-
}
|
|
34
31
|
function transformProtectedConditionalExpression(context, expression) {
|
|
32
|
+
const tempVar = context.createTempNameForNode(expression.condition);
|
|
35
33
|
const condition = context.transformExpression(expression.condition);
|
|
36
|
-
const val1 = context.transformExpression(expression.whenTrue);
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
const [trueStatements, val1] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.whenTrue));
|
|
35
|
+
trueStatements.push(lua.createAssignmentStatement(lua.cloneIdentifier(tempVar), val1, expression.whenTrue));
|
|
36
|
+
const [falseStatements, val2] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression.whenFalse));
|
|
37
|
+
falseStatements.push(lua.createAssignmentStatement(lua.cloneIdentifier(tempVar), val2, expression.whenFalse));
|
|
38
|
+
context.addPrecedingStatements([
|
|
39
|
+
lua.createVariableDeclarationStatement(tempVar, undefined, expression.condition),
|
|
40
|
+
lua.createIfStatement(condition, lua.createBlock(trueStatements, expression.whenTrue), lua.createBlock(falseStatements, expression.whenFalse), expression),
|
|
41
|
+
]);
|
|
42
|
+
return lua.cloneIdentifier(tempVar);
|
|
44
43
|
}
|
|
45
44
|
const transformConditionalExpression = (expression, context) => {
|
|
46
45
|
if (canBeFalsy(context, context.checker.getTypeAtLocation(expression.whenTrue))) {
|
|
@@ -55,20 +54,35 @@ const transformConditionalExpression = (expression, context) => {
|
|
|
55
54
|
};
|
|
56
55
|
exports.transformConditionalExpression = transformConditionalExpression;
|
|
57
56
|
function transformIfStatement(statement, context) {
|
|
58
|
-
scope_1.pushScope(context, scope_1.ScopeType.Conditional);
|
|
57
|
+
(0, scope_1.pushScope)(context, scope_1.ScopeType.Conditional);
|
|
59
58
|
const condition = context.transformExpression(statement.expression);
|
|
60
|
-
const statements = scope_1.performHoisting(context, block_1.transformBlockOrStatement(context, statement.thenStatement));
|
|
61
|
-
scope_1.popScope(context);
|
|
59
|
+
const statements = (0, scope_1.performHoisting)(context, (0, block_1.transformBlockOrStatement)(context, statement.thenStatement));
|
|
60
|
+
(0, scope_1.popScope)(context);
|
|
62
61
|
const ifBlock = lua.createBlock(statements);
|
|
63
62
|
if (statement.elseStatement) {
|
|
64
63
|
if (ts.isIfStatement(statement.elseStatement)) {
|
|
65
|
-
const
|
|
66
|
-
|
|
64
|
+
const tsElseStatement = statement.elseStatement;
|
|
65
|
+
const [precedingStatements, elseStatement] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => transformIfStatement(tsElseStatement, context));
|
|
66
|
+
// If else-if condition generates preceding statements, we can't use elseif, we have to break it down:
|
|
67
|
+
// if conditionA then
|
|
68
|
+
// ...
|
|
69
|
+
// else
|
|
70
|
+
// conditionB's preceding statements
|
|
71
|
+
// if conditionB then
|
|
72
|
+
// end
|
|
73
|
+
// end
|
|
74
|
+
if (precedingStatements.length > 0) {
|
|
75
|
+
const elseBlock = lua.createBlock([...precedingStatements, elseStatement]);
|
|
76
|
+
return lua.createIfStatement(condition, ifBlock, elseBlock);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return lua.createIfStatement(condition, ifBlock, elseStatement);
|
|
80
|
+
}
|
|
67
81
|
}
|
|
68
82
|
else {
|
|
69
|
-
scope_1.pushScope(context, scope_1.ScopeType.Conditional);
|
|
70
|
-
const elseStatements = scope_1.performHoisting(context, block_1.transformBlockOrStatement(context, statement.elseStatement));
|
|
71
|
-
scope_1.popScope(context);
|
|
83
|
+
(0, scope_1.pushScope)(context, scope_1.ScopeType.Conditional);
|
|
84
|
+
const elseStatements = (0, scope_1.performHoisting)(context, (0, block_1.transformBlockOrStatement)(context, statement.elseStatement));
|
|
85
|
+
(0, scope_1.popScope)(context);
|
|
72
86
|
const elseBlock = lua.createBlock(elseStatements);
|
|
73
87
|
return lua.createIfStatement(condition, ifBlock, elseBlock);
|
|
74
88
|
}
|
|
@@ -7,7 +7,11 @@ const lualib_1 = require("../utils/lualib");
|
|
|
7
7
|
const diagnostics_1 = require("../utils/diagnostics");
|
|
8
8
|
const typescript_1 = require("../utils/typescript");
|
|
9
9
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
10
|
+
const optional_chaining_1 = require("./optional-chaining");
|
|
10
11
|
const transformDeleteExpression = (node, context) => {
|
|
12
|
+
if (ts.isOptionalChain(node.expression)) {
|
|
13
|
+
return (0, optional_chaining_1.transformOptionalDeleteExpression)(context, node, node.expression);
|
|
14
|
+
}
|
|
11
15
|
let ownerExpression;
|
|
12
16
|
let propertyExpression;
|
|
13
17
|
if (ts.isPropertyAccessExpression(node.expression)) {
|
|
@@ -21,15 +25,15 @@ const transformDeleteExpression = (node, context) => {
|
|
|
21
25
|
propertyExpression = context.transformExpression(node.expression.argumentExpression);
|
|
22
26
|
const type = context.checker.getTypeAtLocation(node.expression.expression);
|
|
23
27
|
const argumentType = context.checker.getTypeAtLocation(node.expression.argumentExpression);
|
|
24
|
-
if (typescript_1.isArrayType(context, type) && typescript_1.isNumberType(context, argumentType)) {
|
|
25
|
-
propertyExpression = lua_ast_1.addToNumericExpression(propertyExpression, 1);
|
|
28
|
+
if ((0, typescript_1.isArrayType)(context, type) && (0, typescript_1.isNumberType)(context, argumentType)) {
|
|
29
|
+
propertyExpression = (0, lua_ast_1.addToNumericExpression)(propertyExpression, 1);
|
|
26
30
|
}
|
|
27
31
|
}
|
|
28
32
|
if (!ownerExpression || !propertyExpression) {
|
|
29
|
-
context.diagnostics.push(diagnostics_1.unsupportedProperty(node, "delete", ts.SyntaxKind[node.kind]));
|
|
33
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedProperty)(node, "delete", ts.SyntaxKind[node.kind]));
|
|
30
34
|
return lua.createNilLiteral();
|
|
31
35
|
}
|
|
32
|
-
return lualib_1.transformLuaLibFunction(context, lualib_1.LuaLibFeature.Delete, node, ownerExpression, propertyExpression);
|
|
36
|
+
return (0, lualib_1.transformLuaLibFunction)(context, lualib_1.LuaLibFeature.Delete, node, ownerExpression, propertyExpression);
|
|
33
37
|
};
|
|
34
38
|
exports.transformDeleteExpression = transformDeleteExpression;
|
|
35
39
|
//# sourceMappingURL=delete.js.map
|
|
@@ -24,16 +24,16 @@ const transformEnumDeclaration = (node, context) => {
|
|
|
24
24
|
return undefined;
|
|
25
25
|
}
|
|
26
26
|
const type = context.checker.getTypeAtLocation(node);
|
|
27
|
-
const membersOnly = annotations_1.getTypeAnnotations(type).has(annotations_1.AnnotationKind.CompileMembersOnly);
|
|
27
|
+
const membersOnly = (0, annotations_1.getTypeAnnotations)(type).has(annotations_1.AnnotationKind.CompileMembersOnly);
|
|
28
28
|
const result = [];
|
|
29
|
-
if (!membersOnly && typescript_1.isFirstDeclaration(context, node)) {
|
|
30
|
-
const name = identifier_1.transformIdentifier(context, node.name);
|
|
29
|
+
if (!membersOnly && (0, typescript_1.isFirstDeclaration)(context, node)) {
|
|
30
|
+
const name = (0, identifier_1.transformIdentifier)(context, node.name);
|
|
31
31
|
const table = lua.createBinaryExpression(lua.cloneIdentifier(name), lua.createTableExpression(), lua.SyntaxKind.OrOperator);
|
|
32
|
-
result.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, name, table, node));
|
|
32
|
+
result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, name, table, node));
|
|
33
33
|
}
|
|
34
34
|
const enumReference = context.transformExpression(node.name);
|
|
35
35
|
for (const member of node.members) {
|
|
36
|
-
const memberName = literal_1.transformPropertyName(context, member.name);
|
|
36
|
+
const memberName = (0, literal_1.transformPropertyName)(context, member.name);
|
|
37
37
|
let valueExpression;
|
|
38
38
|
const constEnumValue = tryGetConstEnumValue(context, member);
|
|
39
39
|
if (constEnumValue) {
|
|
@@ -45,7 +45,7 @@ const transformEnumDeclaration = (node, context) => {
|
|
|
45
45
|
if ((symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) &&
|
|
46
46
|
ts.isEnumMember(symbol.valueDeclaration) &&
|
|
47
47
|
symbol.valueDeclaration.parent === node) {
|
|
48
|
-
const otherMemberName = literal_1.transformPropertyName(context, symbol.valueDeclaration.name);
|
|
48
|
+
const otherMemberName = (0, literal_1.transformPropertyName)(context, symbol.valueDeclaration.name);
|
|
49
49
|
valueExpression = lua.createTableIndexExpression(enumReference, otherMemberName);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
@@ -58,8 +58,8 @@ const transformEnumDeclaration = (node, context) => {
|
|
|
58
58
|
}
|
|
59
59
|
if (membersOnly) {
|
|
60
60
|
const enumSymbol = context.checker.getSymbolAtLocation(node.name);
|
|
61
|
-
const exportScope = enumSymbol ? export_1.getSymbolExportScope(context, enumSymbol) : undefined;
|
|
62
|
-
result.push(...lua_ast_1.createLocalOrExportedOrGlobalDeclaration(context, lua.isIdentifier(memberName)
|
|
61
|
+
const exportScope = enumSymbol ? (0, export_1.getSymbolExportScope)(context, enumSymbol) : undefined;
|
|
62
|
+
result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, lua.isIdentifier(memberName)
|
|
63
63
|
? memberName
|
|
64
64
|
: lua.createIdentifier(member.name.getText(), member.name), valueExpression, node, exportScope));
|
|
65
65
|
}
|
|
@@ -1,31 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.transformThrowStatement = exports.transformTryStatement = void 0;
|
|
4
|
+
const __1 = require("../..");
|
|
4
5
|
const lua = require("../../LuaAST");
|
|
6
|
+
const diagnostics_1 = require("../utils/diagnostics");
|
|
5
7
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
6
8
|
const scope_1 = require("../utils/scope");
|
|
9
|
+
const typescript_1 = require("../utils/typescript");
|
|
7
10
|
const block_1 = require("./block");
|
|
8
11
|
const identifier_1 = require("./identifier");
|
|
9
12
|
const multi_1 = require("./language-extensions/multi");
|
|
13
|
+
const return_1 = require("./return");
|
|
10
14
|
const transformTryStatement = (statement, context) => {
|
|
11
|
-
const [tryBlock, tryScope] = block_1.transformScopeBlock(context, statement.tryBlock, scope_1.ScopeType.Try);
|
|
15
|
+
const [tryBlock, tryScope] = (0, block_1.transformScopeBlock)(context, statement.tryBlock, scope_1.ScopeType.Try);
|
|
16
|
+
if (context.options.luaTarget === __1.LuaTarget.Lua51 && (0, typescript_1.isInAsyncFunction)(statement)) {
|
|
17
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedForTarget)(statement, "try/catch inside async functions", __1.LuaTarget.Lua51));
|
|
18
|
+
return tryBlock.statements;
|
|
19
|
+
}
|
|
20
|
+
if (context.options.luaTarget === __1.LuaTarget.Lua51 && (0, typescript_1.isInGeneratorFunction)(statement)) {
|
|
21
|
+
context.diagnostics.push((0, diagnostics_1.unsupportedForTarget)(statement, "try/catch inside generator functions", __1.LuaTarget.Lua51));
|
|
22
|
+
return tryBlock.statements;
|
|
23
|
+
}
|
|
12
24
|
const tryResultIdentifier = lua.createIdentifier("____try");
|
|
13
25
|
const returnValueIdentifier = lua.createIdentifier("____returnValue");
|
|
14
26
|
const result = [];
|
|
15
|
-
|
|
27
|
+
const returnedIdentifier = lua.createIdentifier("____hasReturned");
|
|
16
28
|
let returnCondition;
|
|
17
29
|
const pCall = lua.createIdentifier("pcall");
|
|
18
30
|
const tryCall = lua.createCallExpression(pCall, [lua.createFunctionExpression(tryBlock)]);
|
|
19
31
|
if (statement.catchClause && statement.catchClause.block.statements.length > 0) {
|
|
20
32
|
// try with catch
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
const [catchBlock, catchScope] = (0, block_1.transformScopeBlock)(context, statement.catchClause.block, scope_1.ScopeType.Catch);
|
|
34
|
+
const catchParameter = statement.catchClause.variableDeclaration
|
|
35
|
+
? (0, identifier_1.transformIdentifier)(context, statement.catchClause.variableDeclaration.name)
|
|
36
|
+
: undefined;
|
|
37
|
+
const catchParameters = () => (catchParameter ? [lua.cloneIdentifier(catchParameter)] : []);
|
|
38
|
+
const catchIdentifier = lua.createIdentifier("____catch");
|
|
39
|
+
const catchFunction = lua.createFunctionExpression(catchBlock, catchParameters());
|
|
40
|
+
result.push(lua.createVariableDeclarationStatement(catchIdentifier, catchFunction));
|
|
29
41
|
const tryReturnIdentifiers = [tryResultIdentifier]; // ____try
|
|
30
42
|
if (returnedIdentifier) {
|
|
31
43
|
tryReturnIdentifiers.push(returnedIdentifier); // ____returned or catch variable
|
|
@@ -35,18 +47,15 @@ const transformTryStatement = (statement, context) => {
|
|
|
35
47
|
}
|
|
36
48
|
}
|
|
37
49
|
result.push(lua.createVariableDeclarationStatement(tryReturnIdentifiers, tryCall));
|
|
38
|
-
if
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const catchAssign = lua.createAssignmentStatement([lua.cloneIdentifier(returnedIdentifier), lua.cloneIdentifier(returnValueIdentifier)], catchCall);
|
|
42
|
-
catchBlock = lua.createBlock([catchAssign]);
|
|
43
|
-
}
|
|
50
|
+
// Wrap catch in function if try or catch has return
|
|
51
|
+
const catchCall = lua.createCallExpression(catchIdentifier, [lua.cloneIdentifier(returnedIdentifier)]);
|
|
52
|
+
const catchAssign = lua.createAssignmentStatement([lua.cloneIdentifier(returnedIdentifier), lua.cloneIdentifier(returnValueIdentifier)], catchCall);
|
|
44
53
|
const notTryCondition = lua.createUnaryExpression(tryResultIdentifier, lua.SyntaxKind.NotOperator);
|
|
45
|
-
result.push(lua.createIfStatement(notTryCondition,
|
|
54
|
+
result.push(lua.createIfStatement(notTryCondition, lua.createBlock([catchAssign])));
|
|
46
55
|
}
|
|
47
56
|
else if (tryScope.functionReturned) {
|
|
48
57
|
// try with return, but no catch
|
|
49
|
-
returnedIdentifier = lua.createIdentifier("____returned");
|
|
58
|
+
// returnedIdentifier = lua.createIdentifier("____returned");
|
|
50
59
|
const returnedVariables = [tryResultIdentifier, returnedIdentifier, returnValueIdentifier];
|
|
51
60
|
result.push(lua.createVariableDeclarationStatement(returnedVariables, tryCall));
|
|
52
61
|
// change return condition from '____returned' to '____try and ____returned'
|
|
@@ -60,23 +69,14 @@ const transformTryStatement = (statement, context) => {
|
|
|
60
69
|
result.push(...context.transformStatements(statement.finallyBlock));
|
|
61
70
|
}
|
|
62
71
|
if (returnCondition && returnedIdentifier) {
|
|
63
|
-
// With catch clause:
|
|
64
|
-
// if ____returned then return ____returnValue end
|
|
65
|
-
// No catch clause:
|
|
66
|
-
// if ____try and ____returned then return ____returnValue end
|
|
67
72
|
const returnValues = [];
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
// Nested try/catch needs to prefix a 'true' return value
|
|
71
|
-
returnValues.push(lua.createBooleanLiteral(true));
|
|
72
|
-
}
|
|
73
|
-
if (multi_1.isInMultiReturnFunction(context, statement)) {
|
|
74
|
-
returnValues.push(lua_ast_1.createUnpackCall(context, lua.cloneIdentifier(returnValueIdentifier)));
|
|
73
|
+
if ((0, multi_1.isInMultiReturnFunction)(context, statement)) {
|
|
74
|
+
returnValues.push((0, lua_ast_1.createUnpackCall)(context, lua.cloneIdentifier(returnValueIdentifier)));
|
|
75
75
|
}
|
|
76
76
|
else {
|
|
77
77
|
returnValues.push(lua.cloneIdentifier(returnValueIdentifier));
|
|
78
78
|
}
|
|
79
|
-
const returnStatement =
|
|
79
|
+
const returnStatement = (0, return_1.createReturnStatement)(context, returnValues, statement);
|
|
80
80
|
const ifReturnedStatement = lua.createIfStatement(returnCondition, lua.createBlock([returnStatement]));
|
|
81
81
|
result.push(ifReturnedStatement);
|
|
82
82
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
import * as lua from "../../LuaAST";
|
|
3
|
+
import { TransformationContext } from "../context";
|
|
4
|
+
export declare function shouldMoveToTemp(context: TransformationContext, expression: lua.Expression, tsOriginal?: ts.Node): boolean;
|
|
5
|
+
export declare function moveToPrecedingTemp(context: TransformationContext, expression: lua.Expression, tsOriginal?: ts.Node): lua.Expression;
|
|
6
|
+
export declare function transformExpressionList(context: TransformationContext, expressions: readonly ts.Expression[]): lua.Expression[];
|
|
7
|
+
export declare function transformOrderedExpressions(context: TransformationContext, expressions: readonly ts.Expression[]): lua.Expression[];
|