typescript-to-lua 1.1.0 → 1.3.1
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 +28 -0
- package/dist/LuaAST.d.ts +1 -0
- package/dist/LuaAST.js +10 -2
- package/dist/LuaLib.d.ts +3 -3
- package/dist/LuaLib.js +5 -5
- package/dist/LuaPrinter.d.ts +11 -4
- package/dist/LuaPrinter.js +58 -15
- package/dist/cli/parse.d.ts +1 -1
- package/dist/cli/parse.js +14 -0
- 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 +21 -13
- package/dist/lualib/CloneDescriptor.lua +7 -7
- 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 +28 -24
- package/dist/lualib/Set.lua +119 -123
- package/dist/lualib/SetDescriptor.lua +7 -1
- package/dist/lualib/SourceMapTraceBack.lua +1 -1
- 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 +7 -1
- 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 +795 -735
- package/dist/transformation/builtins/array.js +1 -2
- package/dist/transformation/builtins/function.js +6 -4
- package/dist/transformation/builtins/index.d.ts +1 -1
- package/dist/transformation/builtins/index.js +41 -6
- package/dist/transformation/builtins/object.d.ts +2 -1
- package/dist/transformation/builtins/object.js +1 -2
- package/dist/transformation/builtins/string.js +1 -2
- package/dist/transformation/context/context.d.ts +12 -0
- package/dist/transformation/context/context.js +102 -3
- package/dist/transformation/utils/diagnostics.d.ts +9 -6
- package/dist/transformation/utils/diagnostics.js +4 -3
- package/dist/transformation/utils/export.d.ts +3 -2
- package/dist/transformation/utils/export.js +10 -3
- package/dist/transformation/utils/language-extensions.d.ts +2 -0
- package/dist/transformation/utils/language-extensions.js +7 -1
- package/dist/transformation/utils/lua-ast.d.ts +0 -1
- package/dist/transformation/utils/lua-ast.js +14 -23
- 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.d.ts +4 -2
- package/dist/transformation/utils/safe-names.js +11 -5
- package/dist/transformation/utils/scope.d.ts +3 -1
- package/dist/transformation/utils/scope.js +9 -1
- package/dist/transformation/utils/typescript/index.d.ts +1 -0
- package/dist/transformation/utils/typescript/index.js +16 -1
- package/dist/transformation/utils/typescript/types.d.ts +3 -1
- package/dist/transformation/utils/typescript/types.js +40 -13
- package/dist/transformation/visitors/access.d.ts +3 -0
- package/dist/transformation/visitors/access.js +63 -36
- package/dist/transformation/visitors/binary-expression/assignments.d.ts +3 -2
- package/dist/transformation/visitors/binary-expression/assignments.js +66 -57
- package/dist/transformation/visitors/binary-expression/compound.d.ts +5 -4
- package/dist/transformation/visitors/binary-expression/compound.js +66 -68
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.d.ts +2 -2
- package/dist/transformation/visitors/binary-expression/destructuring-assignments.js +54 -25
- package/dist/transformation/visitors/binary-expression/index.d.ts +3 -1
- package/dist/transformation/visitors/binary-expression/index.js +65 -23
- package/dist/transformation/visitors/call.d.ts +3 -2
- package/dist/transformation/visitors/call.js +95 -141
- package/dist/transformation/visitors/class/index.js +13 -9
- package/dist/transformation/visitors/class/new.js +4 -6
- package/dist/transformation/visitors/conditional.js +30 -37
- package/dist/transformation/visitors/delete.js +4 -0
- package/dist/transformation/visitors/errors.js +11 -9
- 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.d.ts +3 -1
- package/dist/transformation/visitors/expression-statement.js +21 -19
- package/dist/transformation/visitors/function.d.ts +2 -0
- package/dist/transformation/visitors/function.js +63 -13
- package/dist/transformation/visitors/identifier.js +5 -0
- package/dist/transformation/visitors/language-extensions/index.d.ts +4 -0
- package/dist/transformation/visitors/language-extensions/index.js +17 -0
- package/dist/transformation/visitors/language-extensions/operators.d.ts +1 -1
- package/dist/transformation/visitors/language-extensions/operators.js +7 -2
- package/dist/transformation/visitors/language-extensions/pairsIterable.d.ts +5 -0
- package/dist/transformation/visitors/language-extensions/pairsIterable.js +53 -0
- package/dist/transformation/visitors/language-extensions/table.d.ts +1 -8
- package/dist/transformation/visitors/language-extensions/table.js +55 -53
- package/dist/transformation/visitors/literal.js +69 -16
- package/dist/transformation/visitors/loops/do-while.js +34 -8
- package/dist/transformation/visitors/loops/for-of.js +4 -0
- package/dist/transformation/visitors/loops/for.js +25 -5
- package/dist/transformation/visitors/loops/utils.d.ts +1 -0
- package/dist/transformation/visitors/loops/utils.js +19 -3
- package/dist/transformation/visitors/modules/export.js +4 -5
- package/dist/transformation/visitors/namespace.js +1 -1
- package/dist/transformation/visitors/optional-chaining.d.ts +16 -0
- package/dist/transformation/visitors/optional-chaining.js +172 -0
- package/dist/transformation/visitors/sourceFile.js +4 -1
- package/dist/transformation/visitors/spread.js +14 -7
- package/dist/transformation/visitors/switch.js +29 -9
- package/dist/transformation/visitors/template.js +5 -2
- package/dist/transformation/visitors/typeof.js +3 -1
- package/dist/transformation/visitors/variable-declaration.js +37 -8
- package/dist/transformation/visitors/void.d.ts +1 -4
- package/dist/transformation/visitors/void.js +7 -12
- package/dist/transpilation/resolve.js +1 -1
- package/dist/transpilation/transformers.js +4 -1
- package/dist/transpilation/utils.js +2 -1
- package/language-extensions/index.d.ts +11 -1
- package/package.json +5 -5
- 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
|
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.transformSwitchStatement = 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");
|
|
8
|
+
const binary_expression_1 = require("./binary-expression");
|
|
7
9
|
const containsBreakOrReturn = (nodes) => {
|
|
8
10
|
for (const s of nodes) {
|
|
9
11
|
if (ts.isBreakStatement(s) || ts.isReturnStatement(s)) {
|
|
@@ -18,13 +20,23 @@ const containsBreakOrReturn = (nodes) => {
|
|
|
18
20
|
}
|
|
19
21
|
return false;
|
|
20
22
|
};
|
|
21
|
-
const
|
|
23
|
+
const createOrExpression = (context, left, right, rightPrecedingStatements) => {
|
|
24
|
+
if (rightPrecedingStatements.length > 0) {
|
|
25
|
+
return (0, binary_expression_1.createShortCircuitBinaryExpressionPrecedingStatements)(context, left, right, rightPrecedingStatements, ts.SyntaxKind.BarBarToken);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return [rightPrecedingStatements, lua.createBinaryExpression(left, right, lua.SyntaxKind.OrOperator)];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const coalesceCondition = (condition, conditionPrecedingStatements, switchVariable, expression, context) => {
|
|
32
|
+
const [precedingStatements, transformedExpression] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(expression));
|
|
22
33
|
// Coalesce skipped statements
|
|
34
|
+
const comparison = lua.createBinaryExpression(switchVariable, transformedExpression, lua.SyntaxKind.EqualityOperator);
|
|
23
35
|
if (condition) {
|
|
24
|
-
return
|
|
36
|
+
return createOrExpression(context, condition, comparison, precedingStatements);
|
|
25
37
|
}
|
|
26
38
|
// Next condition
|
|
27
|
-
return
|
|
39
|
+
return [[...conditionPrecedingStatements, ...precedingStatements], comparison];
|
|
28
40
|
};
|
|
29
41
|
const transformSwitchStatement = (statement, context) => {
|
|
30
42
|
const scope = (0, scope_1.pushScope)(context, scope_1.ScopeType.Switch);
|
|
@@ -53,6 +65,7 @@ const transformSwitchStatement = (statement, context) => {
|
|
|
53
65
|
let defaultTransformed = false;
|
|
54
66
|
let isInitialCondition = true;
|
|
55
67
|
let condition = undefined;
|
|
68
|
+
let conditionPrecedingStatements = [];
|
|
56
69
|
for (let i = 0; i < clauses.length; i++) {
|
|
57
70
|
const clause = clauses[i];
|
|
58
71
|
const previousClause = clauses[i - 1];
|
|
@@ -64,29 +77,35 @@ const transformSwitchStatement = (statement, context) => {
|
|
|
64
77
|
}
|
|
65
78
|
// Compute the condition for the if statement
|
|
66
79
|
if (!ts.isDefaultClause(clause)) {
|
|
67
|
-
condition = coalesceCondition(condition, switchVariable, clause.expression, context);
|
|
80
|
+
[conditionPrecedingStatements, condition] = coalesceCondition(condition, conditionPrecedingStatements, switchVariable, clause.expression, context);
|
|
68
81
|
// Skip empty clauses unless final clause (i.e side-effects)
|
|
69
82
|
if (i !== clauses.length - 1 && clause.statements.length === 0)
|
|
70
83
|
continue;
|
|
71
84
|
// Declare or assign condition variable
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
85
|
+
if (isInitialCondition) {
|
|
86
|
+
statements.push(...conditionPrecedingStatements, lua.createVariableDeclarationStatement(conditionVariable, condition));
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
[conditionPrecedingStatements, condition] = createOrExpression(context, conditionVariable, condition, conditionPrecedingStatements);
|
|
90
|
+
statements.push(...conditionPrecedingStatements, lua.createAssignmentStatement(conditionVariable, condition));
|
|
91
|
+
}
|
|
75
92
|
isInitialCondition = false;
|
|
76
93
|
}
|
|
77
94
|
else {
|
|
78
95
|
// If the default is proceeded by empty clauses and will be emitted we may need to initialize the condition
|
|
79
96
|
if (isInitialCondition) {
|
|
80
|
-
statements.push(lua.createVariableDeclarationStatement(conditionVariable, condition !== null && condition !== void 0 ? condition : lua.createBooleanLiteral(false)));
|
|
97
|
+
statements.push(...conditionPrecedingStatements, lua.createVariableDeclarationStatement(conditionVariable, condition !== null && condition !== void 0 ? condition : lua.createBooleanLiteral(false)));
|
|
81
98
|
// Clear condition ot ensure it is not evaluated twice
|
|
82
99
|
condition = undefined;
|
|
100
|
+
conditionPrecedingStatements = [];
|
|
83
101
|
isInitialCondition = false;
|
|
84
102
|
}
|
|
85
103
|
// Allow default to fallthrough to final default clause
|
|
86
104
|
if (i === clauses.length - 1) {
|
|
87
105
|
// Evaluate the final condition that we may be skipping
|
|
88
106
|
if (condition) {
|
|
89
|
-
|
|
107
|
+
[conditionPrecedingStatements, condition] = createOrExpression(context, conditionVariable, condition, conditionPrecedingStatements);
|
|
108
|
+
statements.push(...conditionPrecedingStatements, lua.createAssignmentStatement(conditionVariable, condition));
|
|
90
109
|
}
|
|
91
110
|
continue;
|
|
92
111
|
}
|
|
@@ -106,6 +125,7 @@ const transformSwitchStatement = (statement, context) => {
|
|
|
106
125
|
statements.push(lua.createIfStatement(conditionVariable, lua.createBlock(clauseStatements)));
|
|
107
126
|
// Clear condition for next clause
|
|
108
127
|
condition = undefined;
|
|
128
|
+
conditionPrecedingStatements = [];
|
|
109
129
|
}
|
|
110
130
|
// If no conditions above match, we need to create the final default case code-path,
|
|
111
131
|
// as we only handle fallthrough into defaults in the previous if statement chain
|
|
@@ -7,6 +7,7 @@ const function_context_1 = require("../utils/function-context");
|
|
|
7
7
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
8
8
|
const types_1 = require("../utils/typescript/types");
|
|
9
9
|
const call_1 = require("./call");
|
|
10
|
+
const expression_list_1 = require("./expression-list");
|
|
10
11
|
// TODO: Source positions
|
|
11
12
|
function getRawLiteral(node) {
|
|
12
13
|
let text = node.getText();
|
|
@@ -21,8 +22,10 @@ const transformTemplateExpression = (node, context) => {
|
|
|
21
22
|
if (head.length > 0) {
|
|
22
23
|
parts.push(lua.createStringLiteral(head, node.head));
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
const transformedExpressions = (0, expression_list_1.transformOrderedExpressions)(context, node.templateSpans.map(s => s.expression));
|
|
26
|
+
for (let i = 0; i < node.templateSpans.length; ++i) {
|
|
27
|
+
const span = node.templateSpans[i];
|
|
28
|
+
const expression = transformedExpressions[i];
|
|
26
29
|
const spanType = context.checker.getTypeAtLocation(span.expression);
|
|
27
30
|
if ((0, types_1.isStringType)(context, spanType)) {
|
|
28
31
|
parts.push(expression);
|
|
@@ -43,7 +43,9 @@ function transformTypeOfBinaryExpression(context, node) {
|
|
|
43
43
|
}
|
|
44
44
|
const innerExpression = context.transformExpression(typeOfExpression.expression);
|
|
45
45
|
const typeCall = lua.createCallExpression(lua.createIdentifier("type"), [innerExpression], typeOfExpression);
|
|
46
|
-
|
|
46
|
+
const [precedingStatements, result] = (0, binary_expression_1.transformBinaryOperation)(context, typeCall, comparedExpression, [], operator, node);
|
|
47
|
+
context.addPrecedingStatements(precedingStatements);
|
|
48
|
+
return result;
|
|
47
49
|
}
|
|
48
50
|
exports.transformTypeOfBinaryExpression = transformTypeOfBinaryExpression;
|
|
49
51
|
//# sourceMappingURL=typeof.js.map
|
|
@@ -9,6 +9,8 @@ const diagnostics_1 = require("../utils/diagnostics");
|
|
|
9
9
|
const export_1 = require("../utils/export");
|
|
10
10
|
const lua_ast_1 = require("../utils/lua-ast");
|
|
11
11
|
const lualib_1 = require("../utils/lualib");
|
|
12
|
+
const preceding_statements_1 = require("../utils/preceding-statements");
|
|
13
|
+
const function_1 = require("./function");
|
|
12
14
|
const identifier_1 = require("./identifier");
|
|
13
15
|
const multi_1 = require("./language-extensions/multi");
|
|
14
16
|
const literal_1 = require("./literal");
|
|
@@ -20,10 +22,15 @@ function transformArrayBindingElement(context, name) {
|
|
|
20
22
|
return (0, identifier_1.transformIdentifier)(context, name);
|
|
21
23
|
}
|
|
22
24
|
else if (ts.isBindingElement(name)) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
if (ts.isIdentifier(name.name)) {
|
|
26
|
+
return (0, identifier_1.transformIdentifier)(context, name.name);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// ts.isBindingPattern(name.name)
|
|
30
|
+
const tempName = context.createTempNameForNode(name.name);
|
|
31
|
+
context.addPrecedingStatements(transformBindingPattern(context, name.name, tempName));
|
|
32
|
+
return tempName;
|
|
33
|
+
}
|
|
27
34
|
}
|
|
28
35
|
else {
|
|
29
36
|
(0, utils_1.assertNever)(name);
|
|
@@ -52,7 +59,9 @@ function transformBindingPattern(context, pattern, table, propertyAccessStack =
|
|
|
52
59
|
// The identifier of the new variable
|
|
53
60
|
const variableName = (0, identifier_1.transformIdentifier)(context, element.name);
|
|
54
61
|
// The field to extract
|
|
55
|
-
const
|
|
62
|
+
const elementName = (_a = element.propertyName) !== null && _a !== void 0 ? _a : element.name;
|
|
63
|
+
const [precedingStatements, propertyName] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => (0, literal_1.transformPropertyName)(context, elementName));
|
|
64
|
+
result.push(...precedingStatements); // Keep property's preceding statements in order
|
|
56
65
|
let expression;
|
|
57
66
|
if (element.dotDotDotToken) {
|
|
58
67
|
if (index !== pattern.elements.length - 1) {
|
|
@@ -90,8 +99,11 @@ function transformBindingPattern(context, pattern, table, propertyAccessStack =
|
|
|
90
99
|
result.push(...(0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, variableName, expression));
|
|
91
100
|
if (element.initializer) {
|
|
92
101
|
const identifier = (0, export_1.addExportToIdentifier)(context, variableName);
|
|
102
|
+
const tsInitializer = element.initializer;
|
|
103
|
+
const [initializerPrecedingStatements, initializer] = (0, preceding_statements_1.transformInPrecedingStatementScope)(context, () => context.transformExpression(tsInitializer));
|
|
93
104
|
result.push(lua.createIfStatement(lua.createBinaryExpression(identifier, lua.createNilLiteral(), lua.SyntaxKind.EqualityOperator), lua.createBlock([
|
|
94
|
-
|
|
105
|
+
...initializerPrecedingStatements,
|
|
106
|
+
lua.createAssignmentStatement(identifier, initializer),
|
|
95
107
|
])));
|
|
96
108
|
}
|
|
97
109
|
}
|
|
@@ -110,14 +122,17 @@ function transformBindingVariableDeclaration(context, bindingPattern, initialize
|
|
|
110
122
|
}
|
|
111
123
|
else {
|
|
112
124
|
// Contain the expression in a temporary variable
|
|
113
|
-
table = lua.createAnonymousIdentifier();
|
|
114
125
|
if (initializer) {
|
|
126
|
+
table = context.createTempNameForNode(initializer);
|
|
115
127
|
let expression = context.transformExpression(initializer);
|
|
116
128
|
if ((0, multi_1.isMultiReturnCall)(context, initializer)) {
|
|
117
129
|
expression = (0, lua_ast_1.wrapInTable)(expression);
|
|
118
130
|
}
|
|
119
131
|
statements.push(lua.createVariableDeclarationStatement(table, expression));
|
|
120
132
|
}
|
|
133
|
+
else {
|
|
134
|
+
table = lua.createAnonymousIdentifier();
|
|
135
|
+
}
|
|
121
136
|
}
|
|
122
137
|
statements.push(...transformBindingPattern(context, bindingPattern, table));
|
|
123
138
|
return statements;
|
|
@@ -169,7 +184,10 @@ function transformVariableDeclaration(context, statement) {
|
|
|
169
184
|
// Find variable identifier
|
|
170
185
|
const identifierName = (0, identifier_1.transformIdentifier)(context, statement.name);
|
|
171
186
|
const value = statement.initializer && context.transformExpression(statement.initializer);
|
|
172
|
-
|
|
187
|
+
// Wrap functions being assigned to a type that contains additional properties in a callable table
|
|
188
|
+
// This catches 'const foo = function() {}; foo.bar = "FOOBAR";'
|
|
189
|
+
const wrappedValue = value && shouldWrapInitializerInCallableTable() ? (0, function_1.createCallableTable)(value) : value;
|
|
190
|
+
return (0, lua_ast_1.createLocalOrExportedOrGlobalDeclaration)(context, identifierName, wrappedValue, statement);
|
|
173
191
|
}
|
|
174
192
|
else if (ts.isArrayBindingPattern(statement.name) || ts.isObjectBindingPattern(statement.name)) {
|
|
175
193
|
return transformBindingVariableDeclaration(context, statement.name, statement.initializer);
|
|
@@ -177,6 +195,17 @@ function transformVariableDeclaration(context, statement) {
|
|
|
177
195
|
else {
|
|
178
196
|
return (0, utils_1.assertNever)(statement.name);
|
|
179
197
|
}
|
|
198
|
+
function shouldWrapInitializerInCallableTable() {
|
|
199
|
+
(0, utils_1.assert)(statement.initializer);
|
|
200
|
+
const initializer = ts.skipOuterExpressions(statement.initializer);
|
|
201
|
+
// do not wrap if not a function expression
|
|
202
|
+
if (!ts.isFunctionExpression(initializer) && !ts.isArrowFunction(initializer))
|
|
203
|
+
return false;
|
|
204
|
+
// Skip named function expressions because they will have been wrapped already
|
|
205
|
+
if (ts.isFunctionExpression(initializer) && initializer.name)
|
|
206
|
+
return false;
|
|
207
|
+
return (0, function_1.isFunctionTypeWithProperties)(context.checker.getTypeAtLocation(statement.name));
|
|
208
|
+
}
|
|
180
209
|
}
|
|
181
210
|
exports.transformVariableDeclaration = transformVariableDeclaration;
|
|
182
211
|
function checkVariableDeclarationList(context, node) {
|
|
@@ -1,6 +1,3 @@
|
|
|
1
1
|
import * as ts from "typescript";
|
|
2
|
-
import
|
|
3
|
-
import { TransformationContext } from "../context";
|
|
4
|
-
import { FunctionVisitor } from "../context/visitors";
|
|
2
|
+
import { FunctionVisitor } from "../context";
|
|
5
3
|
export declare const transformVoidExpression: FunctionVisitor<ts.VoidExpression>;
|
|
6
|
-
export declare const transformVoidExpressionStatement: (node: ts.VoidExpression, context: TransformationContext) => lua.VariableDeclarationStatement;
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.transformVoidExpression = void 0;
|
|
4
4
|
const ts = require("typescript");
|
|
5
5
|
const lua = require("../../LuaAST");
|
|
6
|
-
const
|
|
6
|
+
const expression_statement_1 = require("./expression-statement");
|
|
7
7
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
|
|
8
8
|
const transformVoidExpression = (node, context) => {
|
|
9
9
|
// If content is a literal it is safe to replace the entire expression with nil
|
|
10
|
-
if (ts.isLiteralExpression(node.expression)) {
|
|
11
|
-
|
|
10
|
+
if (!ts.isLiteralExpression(node.expression)) {
|
|
11
|
+
const statements = (0, expression_statement_1.transformExpressionToStatement)(context, node.expression);
|
|
12
|
+
if (statements)
|
|
13
|
+
context.addPrecedingStatements(statements);
|
|
12
14
|
}
|
|
13
|
-
|
|
14
|
-
return (0, lua_ast_1.createImmediatelyInvokedFunctionExpression)([
|
|
15
|
-
lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), context.transformExpression(node.expression)),
|
|
16
|
-
], [], node);
|
|
15
|
+
return lua.createNilLiteral();
|
|
17
16
|
};
|
|
18
17
|
exports.transformVoidExpression = transformVoidExpression;
|
|
19
|
-
const transformVoidExpressionStatement = (node, context) =>
|
|
20
|
-
// In case of a void expression statement we can omit the IIFE
|
|
21
|
-
lua.createVariableDeclarationStatement(lua.createAnonymousIdentifier(), context.transformExpression(node.expression), node);
|
|
22
|
-
exports.transformVoidExpressionStatement = transformVoidExpressionStatement;
|
|
23
18
|
//# sourceMappingURL=void.js.map
|
|
@@ -244,7 +244,7 @@ function isBuildModeLibrary(program) {
|
|
|
244
244
|
function findRequiredPaths(code) {
|
|
245
245
|
// Find all require("<path>") paths in a lua code string
|
|
246
246
|
const paths = [];
|
|
247
|
-
const pattern = /(^|\s|;|=)require\("(
|
|
247
|
+
const pattern = /(^|\s|;|=)require\("(.+?)"\)/g;
|
|
248
248
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
249
249
|
let match;
|
|
250
250
|
while ((match = pattern.exec(code))) {
|
|
@@ -48,7 +48,7 @@ exports.noImplicitSelfTransformer = noImplicitSelfTransformer;
|
|
|
48
48
|
const stripParenthesisExpressionsTransformer = context => sourceFile => {
|
|
49
49
|
// Remove parenthesis expressions before transforming to Lua, so transpiler is not hindered by extra ParenthesizedExpression nodes
|
|
50
50
|
function unwrapParentheses(node) {
|
|
51
|
-
while (ts.isParenthesizedExpression(node)) {
|
|
51
|
+
while (ts.isParenthesizedExpression(node) && !ts.isOptionalChain(node.expression)) {
|
|
52
52
|
node = node.expression;
|
|
53
53
|
}
|
|
54
54
|
return node;
|
|
@@ -61,6 +61,9 @@ const stripParenthesisExpressionsTransformer = context => sourceFile => {
|
|
|
61
61
|
else if (ts.isVoidExpression(node)) {
|
|
62
62
|
return ts.factory.updateVoidExpression(node, unwrapParentheses(node.expression));
|
|
63
63
|
}
|
|
64
|
+
else if (ts.isDeleteExpression(node)) {
|
|
65
|
+
return ts.factory.updateDeleteExpression(node, unwrapParentheses(node.expression));
|
|
66
|
+
}
|
|
64
67
|
return ts.visitEachChild(node, visit, context);
|
|
65
68
|
}
|
|
66
69
|
return ts.visitNode(sourceFile, visit);
|
|
@@ -8,6 +8,7 @@ const cliDiagnostics = require("../cli/diagnostics");
|
|
|
8
8
|
const diagnosticFactories = require("./diagnostics");
|
|
9
9
|
const getConfigDirectory = (options) => options.configFilePath ? path.dirname(options.configFilePath) : process.cwd();
|
|
10
10
|
exports.getConfigDirectory = getConfigDirectory;
|
|
11
|
+
const getTstlDirectory = () => path.dirname(__dirname);
|
|
11
12
|
function resolvePlugin(kind, optionName, basedir, query, importName = "default") {
|
|
12
13
|
if (typeof query !== "string") {
|
|
13
14
|
return { error: cliDiagnostics.compilerOptionRequiresAValueOfType(optionName, "string") };
|
|
@@ -25,7 +26,7 @@ function resolvePlugin(kind, optionName, basedir, query, importName = "default")
|
|
|
25
26
|
const hasNoRequireHook = require.extensions[".ts"] === undefined;
|
|
26
27
|
if (hasNoRequireHook && (resolved.endsWith(".ts") || resolved.endsWith(".tsx"))) {
|
|
27
28
|
try {
|
|
28
|
-
const tsNodePath = resolve.sync("ts-node", { basedir });
|
|
29
|
+
const tsNodePath = resolve.sync("ts-node", { basedir: getTstlDirectory() });
|
|
29
30
|
const tsNode = require(tsNodePath);
|
|
30
31
|
tsNode.register({ transpileOnly: true });
|
|
31
32
|
}
|
|
@@ -78,6 +78,16 @@ declare type LuaIterable<TValue, TState = undefined> = Iterable<TValue> &
|
|
|
78
78
|
LuaIterator<TValue, TState> &
|
|
79
79
|
LuaExtension<"__luaIterableBrand">;
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Represents an object that can be iterated with pairs()
|
|
83
|
+
* For more information see: https://typescripttolua.github.io/docs/advanced/language-extensions
|
|
84
|
+
*
|
|
85
|
+
* @param TKey The type of the key returned each iteration.
|
|
86
|
+
* @param TValue The type of the value returned each iteration.
|
|
87
|
+
*/
|
|
88
|
+
declare type LuaPairsIterable<TKey extends AnyNotNil, TValue> = Iterable<[TKey, TValue]> &
|
|
89
|
+
LuaExtension<"__luaPairsIterableBrand">;
|
|
90
|
+
|
|
81
91
|
/**
|
|
82
92
|
* Calls to functions with this type are translated to `left + right`.
|
|
83
93
|
* For more information see: https://typescripttolua.github.io/docs/advanced/language-extensions
|
|
@@ -535,7 +545,7 @@ declare type LuaTableDeleteMethod<TKey extends AnyNotNil> = ((key: TKey) => bool
|
|
|
535
545
|
* @param TKey The type of the keys used to access the table.
|
|
536
546
|
* @param TValue The type of the values stored in the table.
|
|
537
547
|
*/
|
|
538
|
-
declare interface LuaTable<TKey extends AnyNotNil = AnyNotNil, TValue = any> {
|
|
548
|
+
declare interface LuaTable<TKey extends AnyNotNil = AnyNotNil, TValue = any> extends LuaPairsIterable<TKey, TValue> {
|
|
539
549
|
length: LuaLengthMethod<number>;
|
|
540
550
|
get: LuaTableGetMethod<TKey, TValue>;
|
|
541
551
|
set: LuaTableSetMethod<TKey, TValue>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-to-lua",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.3.1",
|
|
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/",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"node": ">=12.13.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"typescript": "~4.
|
|
45
|
+
"typescript": "~4.5.2"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"enhanced-resolve": "^5.8.2",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"@types/jest": "^25.1.3",
|
|
56
56
|
"@types/node": "^13.7.7",
|
|
57
57
|
"@types/resolve": "1.14.0",
|
|
58
|
-
"@typescript-eslint/eslint-plugin": "^4.
|
|
59
|
-
"@typescript-eslint/parser": "^4.
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.4.0",
|
|
59
|
+
"@typescript-eslint/parser": "^5.4.0",
|
|
60
60
|
"eslint": "^7.32.0",
|
|
61
61
|
"eslint-plugin-import": "^2.24.2",
|
|
62
62
|
"eslint-plugin-jest": "^24.4.0",
|
|
@@ -69,6 +69,6 @@
|
|
|
69
69
|
"prettier": "^2.3.2",
|
|
70
70
|
"ts-jest": "^27.0.7",
|
|
71
71
|
"ts-node": "^10.3.0",
|
|
72
|
-
"typescript": "~4.
|
|
72
|
+
"typescript": "~4.5.2"
|
|
73
73
|
}
|
|
74
74
|
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
function __TS__OptionalMethodCall(____table, methodName, isMethodOptional, ...)
|
|
2
|
-
if ____table then
|
|
3
|
-
local method = ____table[methodName]
|
|
4
|
-
if method then
|
|
5
|
-
return method(____table, ...)
|
|
6
|
-
elseif not isMethodOptional then
|
|
7
|
-
error(methodName .. " is not a function", 0)
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
return nil
|
|
11
|
-
end
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import * as ts from "typescript";
|
|
2
|
-
import * as lua from "../../LuaAST";
|
|
3
|
-
import { TransformationContext } from "../context";
|
|
4
|
-
export interface ImmediatelyInvokedFunctionParameters {
|
|
5
|
-
statements: lua.Statement | lua.Statement[];
|
|
6
|
-
result: lua.Expression | lua.Expression[];
|
|
7
|
-
}
|
|
8
|
-
export declare function transformToImmediatelyInvokedFunctionExpression(context: TransformationContext, transformFunction: () => ImmediatelyInvokedFunctionParameters, tsOriginal?: ts.Node): lua.CallExpression;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.transformToImmediatelyInvokedFunctionExpression = void 0;
|
|
4
|
-
const utils_1 = require("../../utils");
|
|
5
|
-
const lua_ast_1 = require("./lua-ast");
|
|
6
|
-
const scope_1 = require("./scope");
|
|
7
|
-
function transformToImmediatelyInvokedFunctionExpression(context, transformFunction, tsOriginal) {
|
|
8
|
-
(0, scope_1.pushScope)(context, scope_1.ScopeType.Function);
|
|
9
|
-
const { statements, result } = transformFunction();
|
|
10
|
-
(0, scope_1.popScope)(context);
|
|
11
|
-
return (0, lua_ast_1.createImmediatelyInvokedFunctionExpression)((0, utils_1.castArray)(statements), result, tsOriginal);
|
|
12
|
-
}
|
|
13
|
-
exports.transformToImmediatelyInvokedFunctionExpression = transformToImmediatelyInvokedFunctionExpression;
|
|
14
|
-
//# sourceMappingURL=transform.js.map
|